Class: Fulfillment::OrderItem

Inherits:
ModelBase show all
Defined in:
lib/fulfillment/order_item.rb

Constant Summary collapse

GENERIC_REJECTION =

un-categorized rejection reason

1
OUT_OF_STOCK_REJECTION =

fulfillment provider is temporarily out of stock

2
ITEM_NOT_STOCKED_REJECTION =

fulfillment provider does not know about this item at all

3
RESERVE_EXHAUSTED_REJECTION =
4
INVALID_QUANTITY_REJECTION =
5
REJECT_CODES =
[GENERIC_REJECTION, OUT_OF_STOCK_REJECTION,
ITEM_NOT_STOCKED_REJECTION, RESERVE_EXHAUSTED_REJECTION,
INVALID_QUANTITY_REJECTION]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data) ⇒ OrderItem

Returns a new instance of OrderItem.



15
16
17
18
# File 'lib/fulfillment/order_item.rb', line 15

def initialize(client, data)
  @client = client
  make_getter_methods(data)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/fulfillment/order_item.rb', line 4

def client
  @client
end

#order_public_idObject

Returns the value of attribute order_public_id.



4
5
6
# File 'lib/fulfillment/order_item.rb', line 4

def order_public_id
  @order_public_id
end

Class Method Details

.acknowledge(client, order_public_id, order_item_public_id, acknowledgements) ⇒ Object

Acknowledge quantites accepted / rejected of the given FulfillmentOrderItem based on the FulfillmentOrder public ID and the FulfillmentOrderItem public ID. Accepted and Rejected quantities must be present in the acknowledgements hash



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fulfillment/order_item.rb', line 38

def acknowledge(client, order_public_id, order_item_public_id, acknowledgements)
  if acknowledgements["quantity_accepted"].nil? || acknowledgements["quantity_rejected"].nil?
    raise ArgumentError.new("Accepted and Rejected quantities must be present in the acknowledgements hash.")
  end

  curl = Curl::Easy.http_put(client.build_auth_url("/orders/#{order_public_id}/items/#{order_item_public_id}/acknowledge"), acknowledgements.to_json) do |curl|
    client.configure_http(curl)
  end

  if curl.response_code != 200
    raise Fulfillment::CreationException.new("Could not acknowledge item #{order_item_public_id} from order #{order_public_id}:\n\n Response Body:\n #{curl.body_str}")
  end

  new(client, JSON.parse(curl.body_str))
end

.list(client, order_public_id, first_page_num = 1) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fulfillment/order_item.rb', line 83

def list(client, order_public_id, first_page_num = 1)
  Fulfillment::PagedResult.construct(first_page_num) do |page_num|
    curl = Curl::Easy.http_get(client.build_auth_url("/orders/#{order_public_id}/items")) do |curl|
      client.configure_http(curl)
      client.set_request_page(curl, page_num)
    end
    
    raise Fulfillment::ClientException.new("Could not load index of items for order #{order_public_id}: \n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200
    
    order_item_result_array = JSON.parse(curl.body_str)
    order_items = []
    order_item_result_array.each do |ira| 
      order_item = new(client, ira)
      order_item.order_public_id = order_public_id
      order_items << order_item
    end

    Fulfillment::PagingEnvelope.envelop(curl, order_items)
  end
end

.process(client, order_public_id, order_item_public_id) ⇒ Object

Process a given FulfillmentOrderItem based on the FulfillmentOrder public_id and the FulfillmentOrderItem public_id. The client must be the named FulfillmentProvider in order for the ‘process’ to be successful.



73
74
75
76
77
78
79
80
81
# File 'lib/fulfillment/order_item.rb', line 73

def process(client, order_public_id, order_item_public_id)
  curl = Curl::Easy.http_put(client.build_auth_url("/orders/#{order_public_id}/items/#{order_item_public_id}/process"), {}) do |curl|
    client.configure_http(curl)
  end

  raise Fulfillment::CreationException.new("Could not process item #{order_item_public_id} from order #{order_public_id}:\n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200

  new(client, JSON.parse(curl.body_str))
end

.reject(client, order_public_id, order_item_public_id, rejected_code) ⇒ Object

Reject the given FulfillmentOrderItem based on the FulfillmentOrder public ID and the FulfillmentOrderItem public ID. The client must be the named FulfillmentProvider in order for the “rejection” to be successful.

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fulfillment/order_item.rb', line 57

def reject(client, order_public_id, order_item_public_id, rejected_code)
  raise ArgumentError.new("Invalid Reject Code. The following are valid reject codes #{REJECT_CODES.join(",")}") unless REJECT_CODES.include?(rejected_code)
  error_payload = {"rejected_code" => rejected_code}

  curl = Curl::Easy.http_put(client.build_auth_url("/orders/#{order_public_id}/items/#{order_item_public_id}/reject"), error_payload.to_json) do |curl|
    client.configure_http(curl)
  end

  raise Fulfillment::CreationException.new("Could not reject item #{order_item_public_id} from order #{order_public_id}:\n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200

  new(client, JSON.parse(curl.body_str))
end

.show(client, order_public_id, order_item_public_id) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/fulfillment/order_item.rb', line 104

def show(client, order_public_id, order_item_public_id)
  curl = Curl::Easy.http_get(client.build_auth_url("/orders/#{order_public_id}/items/#{order_item_public_id}")) do |curl|
    client.configure_http(curl)
  end

  raise Fulfillment::ClientException.new("Could not get order item #{order_item_public_id} for order #{order_public_id}:\n\n Response Body:\n #{curl.body_str}") unless curl.response_code == 200

  order_item = new(client, JSON.parse(curl.body_str))
  order_item.order_public_id = order_public_id
  order_item
end

Instance Method Details

#acknowledge(acknowledgements) ⇒ Object



24
25
26
# File 'lib/fulfillment/order_item.rb', line 24

def acknowledge(acknowledgements)
  Fulfillment::OrderItem.acknowledge(self.client, self.order_public_id, self.public_id, acknowledgements)
end

#processObject



28
29
30
# File 'lib/fulfillment/order_item.rb', line 28

def process
  Fulfillment::OrderItem.process(self.client, self.order_public_id, self.public_id)
end

#reject(rejected_code) ⇒ Object



20
21
22
# File 'lib/fulfillment/order_item.rb', line 20

def reject(rejected_code)
  Fulfillment::OrderItem.reject(self.client, self.order_public_id, self.public_id, rejected_code)
end