mws-orders 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +73 -0
  4. data/lib/mws/orders/client.rb +88 -0
  5. data/lib/mws/orders/helpers/parameters.rb +53 -0
  6. data/lib/mws/orders/helpers/structured_list.rb +40 -0
  7. data/lib/mws/orders/parsers/base.rb +27 -0
  8. data/lib/mws/orders/parsers/collection.rb +22 -0
  9. data/lib/mws/orders/parsers/invoice_data.rb +23 -0
  10. data/lib/mws/orders/parsers/model.rb +38 -0
  11. data/lib/mws/orders/parsers/order.rb +108 -0
  12. data/lib/mws/orders/parsers/order_item.rb +107 -0
  13. data/lib/mws/orders/parsers/order_items.rb +17 -0
  14. data/lib/mws/orders/parsers/orders.rb +17 -0
  15. data/lib/mws/orders/parsers/payment_execution_detail.rb +17 -0
  16. data/lib/mws/orders/parsers/payment_execution_detail_item.rb +15 -0
  17. data/lib/mws/orders/parsers/service_status.rb +32 -0
  18. data/lib/mws/orders/parsers/service_status_message.rb +15 -0
  19. data/lib/mws/orders/parsers/shipping_address.rb +35 -0
  20. data/lib/mws/orders/requests/base.rb +55 -0
  21. data/lib/mws/orders/requests/order_items.rb +23 -0
  22. data/lib/mws/orders/requests/orders.rb +36 -0
  23. data/lib/mws/orders/requests/service_status.rb +18 -0
  24. data/lib/mws/orders/version.rb +5 -0
  25. data/lib/mws-orders.rb +29 -0
  26. data/test/fixtures/mws.yml +12 -0
  27. data/test/fixtures/mws.yml.example +4 -0
  28. data/test/fixtures/order_items.xml +83 -0
  29. data/test/fixtures/orders.xml +109 -0
  30. data/test/fixtures/service_status.xml +19 -0
  31. data/test/helpers/parameters_test.rb +34 -0
  32. data/test/helpers/structured_list_test.rb +17 -0
  33. data/test/integration/order_items_test.rb +16 -0
  34. data/test/integration/orders_test.rb +17 -0
  35. data/test/integration/service_status_test.rb +9 -0
  36. data/test/integration/test_helper.rb +12 -0
  37. data/test/parsers/collection_test.rb +7 -0
  38. data/test/parsers/model_test.rb +27 -0
  39. data/test/parsers/order_item_test.rb +68 -0
  40. data/test/parsers/order_items_test.rb +13 -0
  41. data/test/parsers/order_test.rb +80 -0
  42. data/test/parsers/orders_test.rb +13 -0
  43. data/test/parsers/payment_execution_detail_item_test.rb +16 -0
  44. data/test/parsers/payment_execution_detail_test.rb +13 -0
  45. data/test/parsers/service_status_message_test.rb +16 -0
  46. data/test/parsers/service_status_test.rb +38 -0
  47. data/test/parsers/shipping_address_test.rb +35 -0
  48. data/test/parsers/test_helper.rb +10 -0
  49. data/test/requests/order_items_test.rb +36 -0
  50. data/test/requests/orders_test.rb +46 -0
  51. data/test/requests/service_status_test.rb +22 -0
  52. data/test/requests/test_helper.rb +18 -0
  53. data/test/test_helper.rb +7 -0
  54. metadata +221 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 161b00e5e2956a4798a37cd121baea48c58031f7
4
+ data.tar.gz: 02981d3f4fb416ebe23f89fd4975d7daae325810
5
+ SHA512:
6
+ metadata.gz: c4caab1a5269758e84ffd649d8c54b119b0de5b609eedf97d4ac2dd91a7cf00dd38bf0f458777e7a8c72c5667e62b4254955a5a618adc8aa687265d14d2936a0
7
+ data.tar.gz: 28365116614a1e9d35826cbf767c24328688af70b64ccdd49ace25be268e63ade05f3e02c9f9055af785335fd8082a1737baad3d275a4e999ffe703efee16d54
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2013 Paper Cavalier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # MWS Orders
2
+
3
+ **MWS Orders** is a fully-featured Ruby interface to the [Amazon Marketplace
4
+ Web Service (MWS) Orders API][1].
5
+
6
+ ## Usage
7
+
8
+ Instantiate a client:
9
+
10
+ ```ruby
11
+ client = MWS::Orders::Client.new('GB', 'aws_key', 'aws_secret', 'seller_id')
12
+ ```
13
+
14
+ [API methods are available on the client][2].
15
+
16
+ ### Orders
17
+
18
+ List orders created or updated during a time frame you specify:
19
+
20
+ ```ruby
21
+ # See the API for all available parameters.
22
+ client.list_orders(
23
+ created_after: 1.week.ago,
24
+ order_status: %w(Pending Unshipped)
25
+ )
26
+ ```
27
+
28
+ List the next page of orders:
29
+
30
+ ```ruby
31
+ client.list_orders_by_next_token
32
+ ```
33
+
34
+ Get one or more orders based on their order numbers:
35
+
36
+ ```ruby
37
+ client.get_order('123-1234567-1234567')
38
+ ```
39
+
40
+ All above queries will return an enumerable list of orders.
41
+
42
+ ### Order Items
43
+
44
+ List order items based on an order number you specify:
45
+
46
+ ```ruby
47
+ client.list_order_items('123-1234567-1234567')
48
+ ```
49
+
50
+ List the next page of order items:
51
+
52
+ ```ruby
53
+ client.list_order_items_by_next_token
54
+ ```
55
+
56
+ All above queries will return an enumerable list of order items.
57
+
58
+ ### Service Status
59
+
60
+ Check the operational status of the API:
61
+
62
+ ```ruby
63
+ client.get_service_status
64
+ ```
65
+
66
+ ### Naming Conventions
67
+
68
+ Request and response attribute names follow Amazon's naming conventions with a
69
+ few exceptions, where some Railsism has insidiuously crept in—e.g.
70
+ `shipped_at` instead of `ShipDate`.
71
+
72
+ [1]: http://docs.developer.amazonservices.com/en_UK/orders/index.html
73
+ [2]: https://github.com/papercavalier/mws-orders/blob/master/lib/mws/orders/client.rb
@@ -0,0 +1,88 @@
1
+ module MWS
2
+ module Orders
3
+ class Client < ::Peddler::Orders
4
+ extend Forwardable
5
+
6
+ # Public: Get one or more orders.
7
+ #
8
+ # amazon_order_ids - One or more String Amazon order ids.
9
+ #
10
+ # Examples
11
+ #
12
+ # client.get_order('123-1234567-1234567')
13
+ #
14
+ # Returns an enumerable list of Orders.
15
+ def_delegator :orders, :get, :get_order
16
+
17
+ # Public: List orders created or updated during a specified time frame.
18
+ #
19
+ # params - The Hash request parameters used to narrow the orders list.
20
+ # Refer to the MWS Orders API for available parameters.
21
+ #
22
+ # Examples
23
+ #
24
+ # client.list_orders(created_after: 1.week.ago)
25
+ #
26
+ # Returns an enumerable list of Orders.
27
+ def_delegator :orders, :list, :list_orders
28
+
29
+ # Public: List the next page of orders using the NextToken parameter.
30
+ #
31
+ # Examples
32
+ #
33
+ # client.list_orders_by_next_token
34
+ #
35
+ # Returns an enumerable list of Orders.
36
+ def_delegator :orders, :list_by_next_token, :list_orders_by_next_token
37
+
38
+ # Public: List order items for an amazon order.
39
+ #
40
+ # amazon_order_id - The String Amazon order id.
41
+ #
42
+ # Examples
43
+ #
44
+ # client.list_order_items('123-1234567-1234567')
45
+ #
46
+ # Returns an enaumerable list of Order Items.
47
+ def_delegator :order_items, :list, :list_order_items
48
+
49
+ # Public: List the next page of order items using the NextToken
50
+ # parameter.
51
+ #
52
+ # Examples
53
+ #
54
+ # client.list_order_items_by_next_token
55
+ #
56
+ # Returns an enaumerable list of Order Items.
57
+ def_delegator :order_items, :list_by_next_token, :list_order_items_by_next_token
58
+
59
+ # Public: Get the service status of the API.
60
+ #
61
+ # Examples
62
+ #
63
+ # client.get_service_status
64
+ #
65
+ # Returns the Service Status.
66
+ def_delegator :service_status, :get, :get_service_status
67
+
68
+ # Internal: Returns the String home marketplace id of the client.
69
+ def home_marketplace_id
70
+ marketplace_id(country)
71
+ end
72
+
73
+ private
74
+
75
+ def orders
76
+ @orders ||= Request::Orders.new(self)
77
+ end
78
+
79
+ def order_items
80
+ @order_items ||= Request::OrderItems.new(self)
81
+ end
82
+
83
+ def service_status
84
+ @service_status ||= Request::ServiceStatus.new(self)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,53 @@
1
+ module MWS
2
+ module Orders
3
+ module Helper
4
+ class Parameters < SimpleDelegator
5
+ def initialize(action)
6
+ super({ 'Action' => camelize(action) })
7
+ end
8
+
9
+ def timestamp!
10
+ each do |key, value|
11
+ store(key, value.iso8601) if value.respond_to?(:iso8601)
12
+ end
13
+
14
+ self
15
+ end
16
+
17
+ def format_structured_lists!
18
+ lists = {}
19
+
20
+ each do |key, value|
21
+ if StructuredList.handle?(key)
22
+ lists.update(StructuredList.new(key).build(delete(key)))
23
+ end
24
+ end
25
+
26
+ update(lists)
27
+ end
28
+
29
+ def camelize_keys!
30
+ keys.each do |key|
31
+ if key.is_a?(Symbol)
32
+ new_key = camelize(key)
33
+ store(new_key, delete(key))
34
+ end
35
+ end
36
+
37
+ self
38
+ end
39
+
40
+ def update(hsh)
41
+ __getobj__.update(hsh)
42
+ self
43
+ end
44
+
45
+ private
46
+
47
+ def camelize(sym)
48
+ sym.to_s.split('_').map(&:capitalize).join
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,40 @@
1
+ module MWS
2
+ module Orders
3
+ module Helper
4
+ class StructuredList
5
+ KEYS = {
6
+ order_status: %w(OrderStatus Status),
7
+ marketplace_id: %w(MarketplaceId Id),
8
+ fulfillment_channel: %w(FulfillmentChannel Channel),
9
+ payment_method: %w(PaymentMethod),
10
+ tfm_shipment_status: %w(TFMShipmentStatus Status),
11
+ amazon_order_id: %w(AmazonOrderId Id)
12
+ }
13
+
14
+ attr :keys
15
+
16
+ def self.handle?(key)
17
+ KEYS.has_key?(key)
18
+ end
19
+
20
+ def initialize(key)
21
+ @keys = KEYS.fetch(key)
22
+ end
23
+
24
+ def build(values)
25
+ Array(values)
26
+ .each_with_index
27
+ .reduce(Hash.new) { |hsh, (v, i)|
28
+ hsh.merge(composite_key(i + 1) => v)
29
+ }
30
+ end
31
+
32
+ private
33
+
34
+ def composite_key(index)
35
+ (keys.dup << index).join('.')
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class Base
5
+ attr :document
6
+
7
+ def initialize(document)
8
+ @document = document
9
+ end
10
+
11
+ private
12
+
13
+ def at_xpath(path)
14
+ document.at_xpath(with_namespace(path))
15
+ end
16
+
17
+ def xpath(path)
18
+ document.xpath(with_namespace(path))
19
+ end
20
+
21
+ def with_namespace(path)
22
+ path.split('/').map { |attr| "xmlns:#{attr}" }.join('/')
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class Collection < Base
5
+ include Enumerable
6
+
7
+ def each
8
+ raise NotImplementedError
9
+ end
10
+
11
+ def empty?
12
+ count == 0
13
+ end
14
+
15
+ def inspect
16
+ "#<#{self.class}>"
17
+ end
18
+ alias :to_s :inspect
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class InvoiceData < Model
5
+ value :invoice_requirement do
6
+ text_at_xpath('InvoiceRequirement')
7
+ end
8
+
9
+ value :buyer_selected_invoice_category do
10
+ text_at_xpath('BuyerSelectedInvoiceCategory')
11
+ end
12
+
13
+ value :invoice_title do
14
+ text_at_xpath('InvoiceTitle')
15
+ end
16
+
17
+ value :invoice_information do
18
+ text_at_xpath('InvoiceInformation')
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class Model < Base
5
+ include Structure
6
+
7
+ def float_at_xpath(path)
8
+ str = text_at_xpath(path)
9
+ str.to_f if str
10
+ end
11
+
12
+ def integer_at_xpath(path)
13
+ str = text_at_xpath(path)
14
+ str.to_i if str
15
+ end
16
+
17
+ def money_at_xpath(path)
18
+ return unless amount = float_at_xpath("#{path}/Amount")
19
+
20
+ currency_code = text_at_xpath("#{path}/CurrencyCode")
21
+ amount = amount * 100 unless currency_code == 'JPY'
22
+
23
+ Money.new(amount, currency_code)
24
+ end
25
+
26
+ def text_at_xpath(path)
27
+ node = at_xpath(path)
28
+ node.text if node
29
+ end
30
+
31
+ def time_at_xpath(path)
32
+ str = text_at_xpath(path)
33
+ Time.parse(CGI.unescape(str)) if str
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,108 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class Order < Model
5
+ value :amazon_order_id do
6
+ text_at_xpath('AmazonOrderId')
7
+ end
8
+
9
+ value :seller_order_id do
10
+ text_at_xpath('SellerOrderId')
11
+ end
12
+
13
+ value :purchased_at do
14
+ time_at_xpath('PurchaseDate')
15
+ end
16
+
17
+ value :last_updated_at do
18
+ time_at_xpath('LastUpdateDate')
19
+ end
20
+
21
+ value :status do
22
+ text_at_xpath('OrderStatus')
23
+ end
24
+
25
+ value :fulfillment_channel do
26
+ text_at_xpath('FulfillmentChannel')
27
+ end
28
+
29
+ value :sales_channel do
30
+ text_at_xpath('SalesChannel')
31
+ end
32
+
33
+ value :order_channel do
34
+ text_at_xpath('OrderChannel')
35
+ end
36
+
37
+ value :ship_service_level do
38
+ text_at_xpath('ShipServiceLevel')
39
+ end
40
+
41
+ value :shipping_address do
42
+ ShippingAddress.new(at_xpath('ShippingAddress'))
43
+ end
44
+
45
+ value :total do
46
+ money_at_xpath('OrderTotal')
47
+ end
48
+
49
+ value :number_of_items_shipped do
50
+ integer_at_xpath('NumberOfItemsShipped')
51
+ end
52
+
53
+ value :number_of_items_unshipped do
54
+ integer_at_xpath('NumberOfItemsUnshipped')
55
+ end
56
+
57
+ value :payment_execution_detail do
58
+ node = at_xpath('PaymentExecutionDetail')
59
+ PaymentExecutionDetail.new(node) if node
60
+ end
61
+
62
+ value :payment_method do
63
+ text_at_xpath('PaymentMethod')
64
+ end
65
+
66
+ value :marketplace_id do
67
+ text_at_xpath('MarketplaceId')
68
+ end
69
+
70
+ value :buyer_name do
71
+ text_at_xpath('BuyerName')
72
+ end
73
+
74
+ value :buyer_email do
75
+ text_at_xpath('BuyerEmail')
76
+ end
77
+
78
+ value :shipment_service_level_category do
79
+ text_at_xpath('ShipmentServiceLevelCategory')
80
+ end
81
+
82
+ value :cba_displayable_shipping_label do
83
+ text_at_xpath('CbaDisplayableShippingLabel')
84
+ end
85
+
86
+ value :shipped_by_amazon_tfm do
87
+ text_at_xpath('ShippedByAmazonTFM')
88
+ end
89
+
90
+ value :tfm_shipment_status do
91
+ text_at_xpath('TFMShipmentStatus')
92
+ end
93
+
94
+ value :type do
95
+ text_at_xpath('OrderType')
96
+ end
97
+
98
+ value :earliest_shipped_at do
99
+ time_at_xpath('EarliestShipDate')
100
+ end
101
+
102
+ value :latest_shipped_at do
103
+ time_at_xpath('LatestShipDate')
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,107 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class OrderItem < Model
5
+ value :asin do
6
+ text_at_xpath('ASIN')
7
+ end
8
+
9
+ value :seller_sku do
10
+ text_at_xpath('SellerSKU')
11
+ end
12
+
13
+ value :order_item_id do
14
+ text_at_xpath('OrderItemId')
15
+ end
16
+
17
+ value :title do
18
+ text_at_xpath('Title')
19
+ end
20
+
21
+ value :quantity_ordered do
22
+ integer_at_xpath('QuantityOrdered')
23
+ end
24
+
25
+ value :quantity_shipped do
26
+ integer_at_xpath('QuantityShipped')
27
+ end
28
+
29
+ value :gift_message_text do
30
+ text_at_xpath('GiftMessageText')
31
+ end
32
+
33
+ value :gift_wrap_level do
34
+ text_at_xpath('GiftWrapLevel')
35
+ end
36
+
37
+ value :item_price do
38
+ money_at_xpath('ItemPrice')
39
+ end
40
+
41
+ value :shipping_price do
42
+ money_at_xpath('ShippingPrice')
43
+ end
44
+
45
+ value :gift_wrap_price do
46
+ money_at_xpath('GiftWrapPrice')
47
+ end
48
+
49
+ value :item_tax do
50
+ money_at_xpath('ItemTax')
51
+ end
52
+
53
+ value :shipping_tax do
54
+ money_at_xpath('ShippingTax')
55
+ end
56
+
57
+ value :gift_wrap_tax do
58
+ money_at_xpath('GiftWrapPrice')
59
+ end
60
+
61
+ value :shipping_discount do
62
+ money_at_xpath('ShippingDiscount')
63
+ end
64
+
65
+ value :promotion_discount do
66
+ money_at_xpath('PromotionDiscount')
67
+ end
68
+
69
+ value :promotion_ids do
70
+ xpath('PromotionId').map(&:text)
71
+ end
72
+
73
+ value :cod_fee do
74
+ money_at_xpath('CODFee')
75
+ end
76
+
77
+ value :cod_fee_discount do
78
+ money_at_xpath('CODFeeDiscount')
79
+ end
80
+
81
+ value :invoice_data do
82
+ xpath('InvoiceData').map { |node| InvoiceData.new(node) }
83
+ end
84
+
85
+ value :condition_id do
86
+ text_at_xpath('ConditionId')
87
+ end
88
+
89
+ value :condition_subtype_id do
90
+ text_at_xpath('ConditionSubtypeId')
91
+ end
92
+
93
+ value :condition_note do
94
+ text_at_xpath('ConditionNote')
95
+ end
96
+
97
+ value :scheduled_delivery_ends_at do
98
+ time_at_xpath('ScheduledDeliveryEndDate')
99
+ end
100
+
101
+ value :scheduled_delivery_starts_at do
102
+ time_at_xpath('ScheduledDeliveryStartDate')
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,17 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class OrderItems < Collection
5
+ def each(&blk)
6
+ order_item_nodes.each { |node| yield OrderItem.new(node) }
7
+ end
8
+
9
+ private
10
+
11
+ def order_item_nodes
12
+ xpath('OrderItem')
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class Orders < Collection
5
+ extend Forwardable
6
+
7
+ def_delegator :order_nodes, :each
8
+
9
+ private
10
+
11
+ def order_nodes
12
+ xpath('Order').map { |node| Order.new(node) }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class PaymentExecutionDetail < Collection
5
+ def each(&blk)
6
+ payment_execution_detail_item_nodes.each { |node| yield PaymentExecutionDetailItem.new(node) }
7
+ end
8
+
9
+ private
10
+
11
+ def payment_execution_detail_item_nodes
12
+ xpath('PaymentExecutionDetailItem')
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module MWS
2
+ module Orders
3
+ module Parser
4
+ class PaymentExecutionDetailItem < Model
5
+ value :payment do
6
+ money_at_xpath('Payment')
7
+ end
8
+
9
+ value :payment_method do
10
+ text_at_xpath('PaymentMethod')
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end