Class: CatalogAPI::Order

Inherits:
Object
  • Object
show all
Defined in:
lib/catalogapi/order.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Order

Returns a new instance of Order.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/catalogapi/order.rb', line 11

def initialize(opts)
  @date_placed = nil
  if opts[:date_placed] && !opts[:date_placed].empty?
    @date_placed = Time.parse(opts[:date_placed]).to_time
  end
  @external_user_id = opts[:external_user_id]
  @order_number = opts[:order_number]

  @external_user_id = opts[:external_user_id]
  @external_order_number = opts[:external_order_number]
  @first_name = opts[:first_name]
  @last_name = opts[:last_name]
  @address_1 = opts[:address_1]
  @address_2 = opts[:address_2]
  @address_3 = opts[:address_3]
  @city = opts[:city]
  @state_province = opts[:state_province]
  @postal_code = opts[:postal_code]
  @country = opts[:country]
  @email = opts[:email]
  @phone_number = opts[:phone_number]
  @fulfillments = opts[:fulfillments].to_a
  @items = opts[:items].to_a
end

Instance Attribute Details

#address_1Object (readonly)

Returns the value of attribute address_1.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def address_1
  @address_1
end

#address_2Object (readonly)

Returns the value of attribute address_2.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def address_2
  @address_2
end

#address_3Object (readonly)

Returns the value of attribute address_3.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def address_3
  @address_3
end

#cityObject (readonly)

Returns the value of attribute city.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def city
  @city
end

#countryObject (readonly)

Returns the value of attribute country.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def country
  @country
end

#date_placedObject (readonly)

Returns the value of attribute date_placed.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def date_placed
  @date_placed
end

#emailObject (readonly)

Returns the value of attribute email.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def email
  @email
end

#external_order_numberObject (readonly)

Returns the value of attribute external_order_number.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def external_order_number
  @external_order_number
end

#external_user_idObject (readonly)

Returns the value of attribute external_user_id.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def external_user_id
  @external_user_id
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def first_name
  @first_name
end

#fulfillmentsObject (readonly)

Returns the value of attribute fulfillments.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def fulfillments
  @fulfillments
end

#itemsObject (readonly)

Returns the value of attribute items.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def items
  @items
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def last_name
  @last_name
end

#order_numberObject (readonly)

Returns the value of attribute order_number.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def order_number
  @order_number
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def phone_number
  @phone_number
end

#postal_codeObject (readonly)

Returns the value of attribute postal_code.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def postal_code
  @postal_code
end

#state_provinceObject (readonly)

Returns the value of attribute state_province.



5
6
7
# File 'lib/catalogapi/order.rb', line 5

def state_province
  @state_province
end

Class Method Details

.list(options = {}, request = nil) ⇒ Array[CatalogAPI::Order]

Return all orders placed by that user.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :external_user_id (String)

    external user id used placing the order

  • :page (String)

    page number of results to return when there are more than per_page results.

  • :per_page (String)

    number of orders to return. Defaults to 10. Can be increased to a maximum of 50.

  • :paginated (String)

    return all orders by paginating over all pages

Returns:

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/catalogapi/order.rb', line 42

def list(options = {}, request = nil)
  external_user_id = options[:external_user_id]
  raise CatalogAPI::Error, 'No External User ID' if external_user_id.nil?

  request ||= CatalogAPI.request.new(:order_list)
  request = request.get(options.merge(external_user_id: external_user_id))
  orders = request.json.dig(
    :order_list_response, :order_list_result, :orders, :OrderSummary
  ).to_a
  request.data += orders.map { |item| CatalogAPI::Order.new(item.merge(external_user_id: external_user_id)) }
  # Pagination
  next_page = options[:paginated] ? request.next_page : nil
  request = list(options.merge(page: next_page), request) if next_page
  request
end

Instance Method Details

#placeCatalogAPI::Order

Place an order without requiring a cart.

Returns:

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/catalogapi/order.rb', line 61

def place
  raise CatalogAPI::Error, 'No Items' if items.nil? || items.length.zero?
  raise CatalogAPI::Error, 'No Socket ID' if items.first.socket_id.nil?
  raise CatalogAPI::Error, 'No First Name' if first_name.nil?
  raise CatalogAPI::Error, 'No Last Name' if last_name.nil?
  raise CatalogAPI::Error, 'No Adress1' if address_1.nil?
  raise CatalogAPI::Error, 'No City' if city.nil?
  raise CatalogAPI::Error, 'No State' if state_province.nil?
  raise CatalogAPI::Error, 'No Postal Code' if postal_code.nil?
  raise CatalogAPI::Error, 'No Country' if country.nil?
  raise CatalogAPI::Error, 'No Items' if items.nil?

  request = CatalogAPI.request.new(:order_place)
  request.post(place_params(request))
  json = request.json.dig(:order_place_response, :order_place_result)
  request.data = CatalogAPI::Order.new(json)
  request
end

#trackCatalogAPI::Order

Return the details of a previously placed order, including any shipment information.

Returns:

  • (CatalogAPI::Order)

    Return the details of a previously placed order, including any shipment information.

Raises:



81
82
83
84
85
86
87
88
89
90
# File 'lib/catalogapi/order.rb', line 81

def track
  raise CatalogAPI::Error, 'No Order Number' if order_number.nil?

  request = CatalogAPI.request.new(:order_track).get(order_number: order_number)
  json = request.json.dig(:order_track_response, :order_track_result, :order)
  items = json.dig(:items, :OrderItem).to_a.map { |i| CatalogAPI::OrderItem.new(i) }
  fulfillments = json.dig(:fulfillments, :Fulfillment).to_a.map { |i| CatalogAPI::Fulfillment.new(i) }
  request.data = CatalogAPI::Order.new(json.merge(items: items, fulfillments: fulfillments))
  request
end