active_fulfillment 3.0.0.pre6 → 3.0.0.pre7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 048de9cecf98e3a3410309c3eea0847f9407ce1b
4
- data.tar.gz: d09dedc9505102085fc104cb2545e16b2277ee26
3
+ metadata.gz: 29a4fec3de142f490e9532546920f1badabb155f
4
+ data.tar.gz: 3a603f15f239767db8ca8bb3749f0501a64ab44d
5
5
  SHA512:
6
- metadata.gz: 4ff0aeecfc643da03b6893470db07423d99bb5fae8c20f3c1e100299fc1730883fbf7d208f6695bac48b5bd9a70001e113893b7eb449f89ee08f7f0c6aea8272
7
- data.tar.gz: 673fdf411195edfe813a294750bb561c07f4cc06792898d591a587bb8aff3b7a26e8fa78cea378c874591e8810d8b6b3da1e0e5f02c61998d7d5a2bc49b0389b
6
+ metadata.gz: 7775f6ec5b95a3750097587b34a73fba8ec83905167826aea18d355f099e717f7fb31eeb610ab8895d487e8d626c1c148f792e06e8baf331d2ba087a19ec6ee2
7
+ data.tar.gz: f3d6f781d3b23b7fb81a093c586855ed4f4bdd0a6a24b42fa4f88c6fffe4017e8bd388fdfed31927f0c0892984c86ea1a73f82dd65439859438d1b0c8b167454
@@ -16,25 +16,6 @@ module ActiveFulfillment
16
16
 
17
17
  SUCCESS, FAILURE, ERROR = 'Accepted', 'Failure', 'Error'
18
18
 
19
- MESSAGES = {
20
- :status => {
21
- 'Accepted' => 'Success',
22
- 'Failure' => 'Failed',
23
- 'Error' => 'An error occurred'
24
- },
25
- :create => {
26
- 'Accepted' => 'Successfully submitted the order',
27
- 'Failure' => 'Failed to submit the order',
28
- 'Error' => 'An error occurred while submitting the order'
29
- },
30
- :list => {
31
- 'Accepted' => 'Successfully submitted request',
32
- 'Failure' => 'Failed to submit request',
33
- 'Error' => 'An error occurred while submitting request'
34
-
35
- }
36
- }
37
-
38
19
  ENDPOINTS = {
39
20
  :ca => 'mws.amazonservices.ca',
40
21
  :cn => 'mws.amazonservices.com.cn',
@@ -74,25 +55,6 @@ module ActiveFulfillment
74
55
  }
75
56
  }
76
57
 
77
- ACTIONS = {
78
- :outbound => "FulfillmentOutboundShipment",
79
- :inventory => "FulfillmentInventory"
80
- }
81
-
82
- OPERATIONS = {
83
- :outbound => {
84
- :status => 'GetServiceStatus',
85
- :create => 'CreateFulfillmentOrder',
86
- :list => 'ListAllFulfillmentOrders',
87
- :tracking => 'GetFulfillmentOrder'
88
- },
89
- :inventory => {
90
- :get => 'ListInventorySupply',
91
- :list => 'ListInventorySupply',
92
- :list_next => 'ListInventorySupplyByNextToken'
93
- }
94
- }
95
-
96
58
  # The first is the label, and the last is the code
97
59
  # Standard: 3-5 business days
98
60
  # Expedited: 2 business days
@@ -122,23 +84,37 @@ module ActiveFulfillment
122
84
 
123
85
  def fulfill(order_id, shipping_address, line_items, options = {})
124
86
  requires!(options, :order_date, :shipping_method)
125
- commit :post, :outbound, :create, build_fulfillment_request(order_id, shipping_address, line_items, options)
87
+ with_error_handling do
88
+ data = commit :post, 'CreateFulfillmentOrder', build_fulfillment_request(order_id, shipping_address, line_items, options)
89
+ parse_fulfillment_response(parse_document(data), 'Successfully submitted the order')
90
+ end
126
91
  end
127
92
 
128
93
  def status
129
- commit :post, :outbound, :status, build_status_request
94
+ with_error_handling do
95
+ data = commit :post, 'GetServiceStatus', build_basic_api_query({ :Action => 'GetServiceStatus' })
96
+ parse_tracking_response(parse_document(data))
97
+ end
130
98
  end
131
99
 
132
100
  def fetch_current_orders
133
- commit :post, :outbound, :status, build_get_current_fulfillment_orders_request
101
+ with_error_handling do
102
+ data = commit :post, 'GetServiceStatus', build_get_current_fulfillment_orders_request
103
+ parse_tracking_response(parse_document(data))
104
+ end
134
105
  end
135
106
 
136
107
  def fetch_stock_levels(options = {})
137
108
  options[:skus] = [options.delete(:sku)] if options.include?(:sku)
138
- response = commit :post, :inventory, :list, build_inventory_list_request(options)
139
-
109
+ response = with_error_handling do
110
+ data = commit :post, 'ListInventorySupply', build_inventory_list_request(options)
111
+ parse_inventory_response(parse_document(data))
112
+ end
140
113
  while token = response.params['next_token'] do
141
- next_page = commit :post, :inventory, :list_next, build_next_inventory_list_request(token)
114
+ next_page = with_error_handling do
115
+ data = commit :post, 'ListInventorySupplyByNextToken', build_next_inventory_list_request(token)
116
+ parse_inventory_response(parse_document(data))
117
+ end
142
118
 
143
119
  # if we fail during the stock-level-via-token gathering, fail the whole request
144
120
  return next_page if next_page.params['response_status'] != SUCCESS
@@ -153,7 +129,11 @@ module ActiveFulfillment
153
129
  index = 0
154
130
  order_ids.reduce(nil) do |previous, order_id|
155
131
  index += 1
156
- response = commit :post, :outbound, :tracking, build_tracking_request(order_id, options)
132
+ response = with_error_handling do
133
+ data = commit :post, 'GetFulfillmentOrder', build_tracking_request(order_id, options)
134
+ parse_tracking_response(parse_document(data))
135
+ end
136
+
157
137
  return response if !response.success?
158
138
 
159
139
  if previous
@@ -180,21 +160,17 @@ module ActiveFulfillment
180
160
  build_query(params) + "&Signature=#{signature}"
181
161
  end
182
162
 
183
- def commit(verb, service, op, params)
184
- uri = URI.parse("https://#{endpoint}/#{ACTIONS[service]}/#{VERSION}")
163
+ def commit(verb, action, params)
164
+ uri = URI.parse("https://#{endpoint}/#{action}/#{VERSION}")
185
165
  query = build_full_query(verb, uri, params)
186
166
  headers = build_headers(query)
187
167
  log_query = query.dup
188
168
  [@options[:login], @options[:app_id], @mws_auth_token].each { |key| log_query.gsub!(/#{key}/, '[filtered]') if key.present? }
189
169
 
190
- logger.info "[#{self.class}][#{op.to_s}] query=#{log_query}"
170
+ logger.info "[#{self.class}][#{action}] query=#{log_query}"
191
171
  data = ssl_post(uri.to_s, query, headers)
192
- logger.info "[#{self.class}][#{op.to_s}] response=#{data}"
193
-
194
- response = parse_response(service, op, data)
195
- Response.new(success?(response), message_from(response), response)
196
- rescue ActiveUtils::ResponseError => e
197
- handle_error(e)
172
+ logger.info "[#{self.class}][#{action}] response=#{data}"
173
+ data
198
174
  end
199
175
 
200
176
  def handle_error(e)
@@ -217,26 +193,12 @@ module ActiveFulfillment
217
193
 
218
194
  ## PARSING
219
195
 
220
- def parse_response(service, op, xml)
196
+ def parse_document(xml)
221
197
  begin
222
198
  document = REXML::Document.new(xml)
223
199
  rescue REXML::ParseException
224
200
  return { :success => FAILURE }
225
201
  end
226
-
227
- case service
228
- when :outbound
229
- case op
230
- when :tracking
231
- parse_tracking_response(document)
232
- else
233
- parse_fulfillment_response(op, document)
234
- end
235
- when :inventory
236
- parse_inventory_response(document)
237
- else
238
- raise ArgumentError, "Unknown service #{service}"
239
- end
240
202
  end
241
203
 
242
204
  def parse_tracking_response(document)
@@ -258,11 +220,11 @@ module ActiveFulfillment
258
220
  end
259
221
 
260
222
  response[:response_status] = SUCCESS
261
- response
223
+ Response.new(success?(response), message_from(response), response)
262
224
  end
263
225
 
264
- def parse_fulfillment_response(op, document)
265
- { :response_status => SUCCESS, :response_comment => MESSAGES[op][SUCCESS] }
226
+ def parse_fulfillment_response(document, message)
227
+ Response.new(true, message, { :response_status => SUCCESS, :response_comment => message })
266
228
  end
267
229
 
268
230
  def parse_inventory_response(document)
@@ -280,6 +242,7 @@ module ActiveFulfillment
280
242
 
281
243
  response[:response_status] = SUCCESS
282
244
  response
245
+ Response.new(success?(response), message_from(response), response)
283
246
  end
284
247
 
285
248
  def parse_error(http_response)
@@ -366,7 +329,7 @@ module ActiveFulfillment
366
329
 
367
330
  def build_fulfillment_request(order_id, shipping_address, line_items, options)
368
331
  params = {
369
- :Action => OPERATIONS[:outbound][:create],
332
+ :Action => 'CreateFulfillmentOrder',
370
333
  :SellerFulfillmentOrderId => order_id.to_s,
371
334
  :DisplayableOrderId => order_id.to_s,
372
335
  :DisplayableOrderDateTime => options[:order_date].utc.iso8601,
@@ -384,7 +347,7 @@ module ActiveFulfillment
384
347
  def build_get_current_fulfillment_orders_request(options = {})
385
348
  start_time = options.delete(:start_time) || 1.day.ago.utc
386
349
  params = {
387
- :Action => OPERATIONS[:outbound][:list],
350
+ :Action => 'ListAllFulfillmentOrders',
388
351
  :QueryStartDateTime => start_time.strftime("%Y-%m-%dT%H:%M:%SZ")
389
352
  }
390
353
 
@@ -394,7 +357,7 @@ module ActiveFulfillment
394
357
  def build_inventory_list_request(options = {})
395
358
  response_group = options.delete(:response_group) || "Basic"
396
359
  params = {
397
- :Action => OPERATIONS[:inventory][:list],
360
+ :Action => 'ListInventorySupply',
398
361
  :ResponseGroup => response_group
399
362
  }
400
363
  if skus = options.delete(:skus)
@@ -412,14 +375,14 @@ module ActiveFulfillment
412
375
  def build_next_inventory_list_request(token)
413
376
  params = {
414
377
  :NextToken => token,
415
- :Action => OPERATIONS[:inventory][:list_next]
378
+ :Action => 'ListInventorySupplyByNextToken'
416
379
  }
417
380
 
418
381
  build_basic_api_query(params)
419
382
  end
420
383
 
421
384
  def build_tracking_request(order_id, options)
422
- params = {:Action => OPERATIONS[:outbound][:tracking], :SellerFulfillmentOrderId => order_id}
385
+ params = {:Action => 'GetFulfillmentOrder', :SellerFulfillmentOrderId => order_id}
423
386
 
424
387
  build_basic_api_query(params.merge(options))
425
388
  end
@@ -456,16 +419,18 @@ module ActiveFulfillment
456
419
  end
457
420
  end
458
421
 
459
- def build_status_request
460
- build_basic_api_query({ :Action => OPERATIONS[:outbound][:status] })
461
- end
462
-
463
422
  def escape(str)
464
423
  CGI.escape(str.to_s).gsub('+', '%20')
465
424
  end
466
425
 
467
426
  private
468
427
 
428
+ def with_error_handling
429
+ yield
430
+ rescue ActiveUtils::ResponseError => e
431
+ handle_error(e)
432
+ end
433
+
469
434
  def sleep_for_throttle_options(throttle_options, index)
470
435
  return unless interval = throttle_options.try(:[], :interval)
471
436
  sleep(throttle_options[:sleep_time]) if (index % interval).zero?
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module ActiveFulfillment
3
- VERSION = "3.0.0.pre6"
3
+ VERSION = "3.0.0.pre7"
4
4
  end
@@ -24,7 +24,6 @@
24
24
  require 'active_support'
25
25
  require 'active_support/core_ext/class/attribute'
26
26
  require 'active_support/core_ext/module/attribute_accessors'
27
- require 'active_support/core_ext/class/delegating_attributes'
28
27
  require 'active_support/core_ext/time/calculations'
29
28
  require 'active_support/core_ext/date/calculations'
30
29
  require 'active_support/core_ext/numeric/time'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_fulfillment
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.pre6
4
+ version: 3.0.0.pre7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Fauser
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-11-05 00:00:00.000000000 Z
12
+ date: 2015-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport