mws-orders 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +16 -25
  4. data/lib/mws-orders.rb +2 -29
  5. data/lib/mws/orders/collection.rb +29 -0
  6. data/lib/mws/orders/document.rb +28 -0
  7. data/lib/mws/orders/entity.rb +39 -0
  8. data/lib/mws/orders/invoice_data.rb +23 -0
  9. data/lib/mws/orders/order.rb +113 -0
  10. data/lib/mws/orders/order_item.rb +108 -0
  11. data/lib/mws/orders/order_items.rb +15 -0
  12. data/lib/mws/orders/orders.rb +15 -0
  13. data/lib/mws/orders/parser.rb +52 -0
  14. data/lib/mws/orders/payment_execution_detail.rb +12 -0
  15. data/lib/mws/orders/payment_execution_detail_item.rb +15 -0
  16. data/lib/mws/orders/service_status.rb +24 -0
  17. data/lib/mws/orders/service_status_message.rb +15 -0
  18. data/lib/mws/orders/shipping_address.rb +35 -0
  19. data/lib/mws/orders/tokenable.rb +10 -0
  20. data/lib/mws/orders/version.rb +1 -1
  21. data/test/integration/test_integration.rb +26 -0
  22. data/test/mws/orders/test_entity.rb +88 -0
  23. data/test/{parsers/order_test.rb → mws/orders/test_order.rb} +11 -6
  24. data/test/{parsers/order_item_test.rb → mws/orders/test_order_item.rb} +4 -4
  25. data/test/mws/orders/test_order_items.rb +19 -0
  26. data/test/mws/orders/test_orders.rb +19 -0
  27. data/test/mws/orders/test_parser.rb +7 -0
  28. data/test/mws/orders/test_payment_execution_detail.rb +15 -0
  29. data/test/mws/orders/test_payment_execution_detail_item.rb +16 -0
  30. data/test/mws/orders/test_service_status.rb +24 -0
  31. data/test/mws/orders/test_service_status_message.rb +16 -0
  32. data/test/{parsers/shipping_address_test.rb → mws/orders/test_shipping_address.rb} +4 -4
  33. data/test/test_helper.rb +15 -6
  34. metadata +67 -134
  35. data/lib/mws/orders/client.rb +0 -88
  36. data/lib/mws/orders/helpers/parameters.rb +0 -53
  37. data/lib/mws/orders/helpers/structured_list.rb +0 -40
  38. data/lib/mws/orders/parsers/base.rb +0 -27
  39. data/lib/mws/orders/parsers/collection.rb +0 -22
  40. data/lib/mws/orders/parsers/invoice_data.rb +0 -23
  41. data/lib/mws/orders/parsers/model.rb +0 -38
  42. data/lib/mws/orders/parsers/order.rb +0 -108
  43. data/lib/mws/orders/parsers/order_item.rb +0 -107
  44. data/lib/mws/orders/parsers/order_items.rb +0 -17
  45. data/lib/mws/orders/parsers/orders.rb +0 -17
  46. data/lib/mws/orders/parsers/payment_execution_detail.rb +0 -17
  47. data/lib/mws/orders/parsers/payment_execution_detail_item.rb +0 -15
  48. data/lib/mws/orders/parsers/service_status.rb +0 -32
  49. data/lib/mws/orders/parsers/service_status_message.rb +0 -15
  50. data/lib/mws/orders/parsers/shipping_address.rb +0 -35
  51. data/lib/mws/orders/requests/base.rb +0 -55
  52. data/lib/mws/orders/requests/order_items.rb +0 -23
  53. data/lib/mws/orders/requests/orders.rb +0 -36
  54. data/lib/mws/orders/requests/service_status.rb +0 -18
  55. data/test/fixtures/mws.yml +0 -12
  56. data/test/helpers/parameters_test.rb +0 -34
  57. data/test/helpers/structured_list_test.rb +0 -17
  58. data/test/integration/order_items_test.rb +0 -16
  59. data/test/integration/orders_test.rb +0 -17
  60. data/test/integration/service_status_test.rb +0 -9
  61. data/test/integration/test_helper.rb +0 -12
  62. data/test/parsers/collection_test.rb +0 -7
  63. data/test/parsers/model_test.rb +0 -27
  64. data/test/parsers/order_items_test.rb +0 -13
  65. data/test/parsers/orders_test.rb +0 -13
  66. data/test/parsers/payment_execution_detail_item_test.rb +0 -16
  67. data/test/parsers/payment_execution_detail_test.rb +0 -13
  68. data/test/parsers/service_status_message_test.rb +0 -16
  69. data/test/parsers/service_status_test.rb +0 -38
  70. data/test/parsers/test_helper.rb +0 -10
  71. data/test/requests/order_items_test.rb +0 -36
  72. data/test/requests/orders_test.rb +0 -46
  73. data/test/requests/service_status_test.rb +0 -22
  74. data/test/requests/test_helper.rb +0 -18
@@ -0,0 +1,15 @@
1
+ require "mws/orders/collection"
2
+ require "mws/orders/order_item"
3
+ require "mws/orders/tokenable"
4
+
5
+ module MWS
6
+ module Orders
7
+ class OrderItems < Collection
8
+ include Tokenable
9
+
10
+ def each(&blk)
11
+ xpath("OrderItems/OrderItem").each { |node| yield OrderItem.new(node) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require "mws/orders/collection"
2
+ require "mws/orders/order"
3
+ require "mws/orders/tokenable"
4
+
5
+ module MWS
6
+ module Orders
7
+ class Orders < Collection
8
+ include Tokenable
9
+
10
+ def each(&blk)
11
+ xpath("Orders/Order").map { |node| yield Order.new(node) }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,52 @@
1
+ require "nokogiri"
2
+ require "mws/orders/orders"
3
+ require "mws/orders/order_items"
4
+ require "mws/orders/service_status"
5
+
6
+ module MWS
7
+ module Orders
8
+ class Parser
9
+ SERVICE_STATUS = /GetServiceStatus/
10
+ ORDER = /GetOrder/
11
+ ORDERS = /ListOrders/
12
+ ORDER_ITEMS = /ListOrderItems/
13
+
14
+ def self.parse(response, _)
15
+ new(response).parse
16
+ end
17
+
18
+ def initialize(response)
19
+ @response = response
20
+ end
21
+
22
+ def parse
23
+ node = find_result_node
24
+
25
+ case node.name
26
+ when SERVICE_STATUS
27
+ ServiceStatus.new(node)
28
+ when ORDER
29
+ Orders.new(node).first
30
+ when ORDERS
31
+ Orders.new(node)
32
+ when ORDER_ITEMS
33
+ OrderItems.new(node)
34
+ else
35
+ raise NotImplementedError
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def find_result_node
42
+ xml = Nokogiri(@response.body)
43
+ root = xml.children.first
44
+
45
+ root.children.find { |node| node.name.include?("Result") }
46
+ end
47
+ end
48
+
49
+ # Override Peddler's default Parser.
50
+ Client.parser = Parser
51
+ end
52
+ end
@@ -0,0 +1,12 @@
1
+ require "mws/orders/collection"
2
+ require "mws/orders/payment_execution_detail_item"
3
+
4
+ module MWS
5
+ module Orders
6
+ class PaymentExecutionDetail < Collection
7
+ def each(&blk)
8
+ xpath("PaymentExecutionDetailItem").each { |node| yield PaymentExecutionDetailItem.new(node) }
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ require "mws/orders/entity"
2
+
3
+ module MWS
4
+ module Orders
5
+ class PaymentExecutionDetailItem < Entity
6
+ attribute(:payment) do
7
+ money_at_xpath("Payment")
8
+ end
9
+
10
+ attribute(:payment_method) do
11
+ text_at_xpath("PaymentMethod")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ require "mws/orders/entity"
2
+ require "mws/orders/service_status_message"
3
+
4
+ module MWS
5
+ module Orders
6
+ class ServiceStatus < Entity
7
+ attribute(:status) do
8
+ text_at_xpath("Status")
9
+ end
10
+
11
+ attribute(:timestamp) do
12
+ time_at_xpath("Timestamp")
13
+ end
14
+
15
+ attribute(:message_id) do
16
+ text_at_xpath("MessageId")
17
+ end
18
+
19
+ attribute(:messages) do
20
+ xpath("Messages/Message").map { |node| ServiceStatusMessage.new(node) }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ require "mws/orders/entity"
2
+
3
+ module MWS
4
+ module Orders
5
+ class ServiceStatusMessage < Entity
6
+ attribute(:locale) do
7
+ text_at_xpath("Locale")
8
+ end
9
+
10
+ attribute(:text) do
11
+ text_at_xpath("Text")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ require "mws/orders/entity"
2
+
3
+ module MWS
4
+ module Orders
5
+ class ShippingAddress < Entity
6
+ attribute(:name) do
7
+ text_at_xpath("Name")
8
+ end
9
+
10
+ attribute(:address_lines) do
11
+ 1.upto(3).map { |i| text_at_xpath("AddressLine#{i}") }.compact
12
+ end
13
+
14
+ attribute(:city) do
15
+ text_at_xpath("City")
16
+ end
17
+
18
+ attribute(:state_or_region) do
19
+ text_at_xpath("StateOrRegion")
20
+ end
21
+
22
+ attribute(:postal_code) do
23
+ text_at_xpath("PostalCode")
24
+ end
25
+
26
+ attribute(:country_code) do
27
+ text_at_xpath("CountryCode")
28
+ end
29
+
30
+ attribute(:phone) do
31
+ text_at_xpath("Phone")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,10 @@
1
+ module MWS
2
+ module Orders
3
+ module Tokenable
4
+ def next_token
5
+ node = xpath('NextToken')
6
+ node.text if node
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,5 @@
1
1
  module MWS
2
2
  module Orders
3
- VERSION = '0.0.2'
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -0,0 +1,26 @@
1
+ require "test_helper"
2
+
3
+ class TestIntegration < MiniTest::Test
4
+ def setup
5
+ skip if ENV['CI']
6
+ @client = MWS.orders
7
+ end
8
+
9
+ def test_gets_service_status
10
+ assert_kind_of ServiceStatus, @client.get_service_status
11
+ end
12
+
13
+ def test_lists_orders_and_order_items
14
+ orders = @client.list_orders(created_after: Date.today - 30)
15
+ refute_empty orders
16
+
17
+ next_orders = @client.list_orders_by_next_token(orders.next_token)
18
+ refute_empty orders
19
+
20
+ amazon_order_id = next_orders.to_a.sample.amazon_order_id
21
+ assert_kind_of MWS::Orders::Order, @client.get_order(amazon_order_id)
22
+
23
+ order_items = @client.list_order_items(amazon_order_id)
24
+ refute_empty order_items
25
+ end
26
+ end
@@ -0,0 +1,88 @@
1
+ require "test_helper"
2
+
3
+ class TestEntity < MiniTest::Test
4
+ def test_parses_floats
5
+ xml = <<-EOF
6
+ <Float xmlns="example">10.0</Float>
7
+ EOF
8
+ entity = Entity.new(Nokogiri(xml))
9
+ float = entity.float_at_xpath("Float")
10
+ assert_equal 10.0, float
11
+ end
12
+
13
+ def test_parses_integers
14
+ xml = <<-EOF
15
+ <Integer xmlns="example">10</Integer>
16
+ EOF
17
+ entity = Entity.new(Nokogiri(xml))
18
+ integer = entity.integer_at_xpath("Integer")
19
+ assert_equal 10, integer
20
+ end
21
+
22
+ def test_parses_money
23
+ xml = <<-EOF
24
+ <Price xmlns="example">
25
+ <CurrencyCode>USD</CurrencyCode>
26
+ <Amount>10.00</Amount>
27
+ </Price>
28
+ EOF
29
+ entity = Entity.new(Nokogiri(xml))
30
+ money = entity.money_at_xpath("Price")
31
+ assert_equal "$10.00", money.format
32
+ end
33
+
34
+ def test_parses_japanese_yen
35
+ xml = <<-EOF
36
+ <Price xmlns="example">
37
+ <CurrencyCode>JPY</CurrencyCode>
38
+ <Amount>1000.00</Amount>
39
+ </Price>
40
+ EOF
41
+ entity = Entity.new(Nokogiri(xml))
42
+ money = entity.money_at_xpath("Price")
43
+ assert_equal "¥1,000", money.format
44
+ end
45
+
46
+ def test_parses_text
47
+ xml = <<-EOF
48
+ <Text xmlns="example">Foo</Text>
49
+ EOF
50
+ entity = Entity.new(Nokogiri(xml))
51
+ text = entity.text_at_xpath("Text")
52
+ assert_equal "Foo", text
53
+ end
54
+
55
+ def test_strips_text
56
+ xml = <<-EOF
57
+ <Text xmlns="example">Foo
58
+
59
+ </Text>
60
+ EOF
61
+ entity = Entity.new(Nokogiri(xml))
62
+ text = entity.text_at_xpath("Text")
63
+ assert_equal "Foo", text
64
+ end
65
+
66
+ def test_parses_time
67
+ xml = <<-EOF
68
+ <Time xmlns="example">2013-01-01T01:30:00.000-06:00</Time>
69
+ EOF
70
+ entity = Entity.new(Nokogiri(xml))
71
+ time = entity.time_at_xpath("Time")
72
+ assert_kind_of Time, time
73
+ end
74
+
75
+ def test_handles_nil_values
76
+ xml = <<-EOF
77
+ <Foo xmlns="example">
78
+ </Foo>
79
+ EOF
80
+ entity = Entity.new(Nokogiri(xml))
81
+
82
+ assert_nil entity.float_at_xpath("Bar")
83
+ assert_nil entity.integer_at_xpath("Bar")
84
+ assert_nil entity.money_at_xpath("Bar")
85
+ assert_nil entity.time_at_xpath("Bar")
86
+ assert_nil entity.text_at_xpath("Bar")
87
+ end
88
+ end
@@ -1,9 +1,9 @@
1
- require_relative 'test_helper'
1
+ require "test_helper"
2
2
 
3
- class OrderParserTest < ParserTest
3
+ class TestOrder < MiniTest::Test
4
4
  def setup
5
- node = fixture('orders').xpath('//xmlns:Order').first
6
- @order = Parser::Order.new(node)
5
+ node = load_xml_fixture("orders").at_xpath("//xmlns:Order")
6
+ @order = Order.new(node)
7
7
  end
8
8
 
9
9
  def test_amazon_order_id
@@ -43,7 +43,7 @@ class OrderParserTest < ParserTest
43
43
  end
44
44
 
45
45
  def test_shipping_address
46
- assert_kind_of Parser::ShippingAddress, @order.shipping_address
46
+ assert_kind_of ShippingAddress, @order.shipping_address
47
47
  end
48
48
 
49
49
  def test_number_of_items_shipped
@@ -55,7 +55,7 @@ class OrderParserTest < ParserTest
55
55
  end
56
56
 
57
57
  def test_payment_execution_detail
58
- assert_kind_of Parser::PaymentExecutionDetail, @order.payment_execution_detail
58
+ assert_kind_of PaymentExecutionDetail, @order.payment_execution_detail
59
59
  end
60
60
 
61
61
  def test_payment_method
@@ -77,4 +77,9 @@ class OrderParserTest < ParserTest
77
77
  def test_shipment_service_level_category
78
78
  assert_kind_of String, @order.shipment_service_level_category
79
79
  end
80
+
81
+ def test_no_shipping_adress
82
+ @order.xpath('ShippingAddress').remove
83
+ assert_nil @order.shipping_address
84
+ end
80
85
  end
@@ -1,9 +1,9 @@
1
- require_relative 'test_helper'
1
+ require "test_helper"
2
2
 
3
- class OrderItemParserTest < ParserTest
3
+ class TestOrderItemTest < MiniTest::Test
4
4
  def setup
5
- node = fixture('order_items').xpath('//xmlns:OrderItem').first
6
- @order_item = Parser::OrderItem.new(node)
5
+ node = load_xml_fixture("order_items").at_xpath("//xmlns:OrderItem")
6
+ @order_item = OrderItem.new(node)
7
7
  end
8
8
 
9
9
  def test_asin
@@ -0,0 +1,19 @@
1
+ require "test_helper"
2
+
3
+ class TestOrderItems < MiniTest::Test
4
+ def setup
5
+ node = load_xml_fixture("order_items").xpath("//xmlns:ListOrderItemsResult")
6
+ @order_items = OrderItems.new(node)
7
+ end
8
+
9
+ def test_has_order_items
10
+ refute_empty @order_items.to_a
11
+ @order_items.each do |order_item|
12
+ assert_kind_of OrderItem, order_item
13
+ end
14
+ end
15
+
16
+ def test_has_next_token
17
+ refute_empty @order_items.next_token
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require "test_helper"
2
+
3
+ class TestOrders < MiniTest::Test
4
+ def setup
5
+ node = load_xml_fixture("orders").xpath("//xmlns:ListOrdersResult")
6
+ @orders = Orders.new(node)
7
+ end
8
+
9
+ def test_has_orders
10
+ refute_empty @orders.to_a
11
+ @orders.each do |order|
12
+ assert_kind_of Order, order
13
+ end
14
+ end
15
+
16
+ def test_has_next_token
17
+ refute_empty @orders.next_token
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ require "test_helper"
2
+
3
+ class TestParser < MiniTest::Test
4
+ def test_overrides_peddlers_parser
5
+ assert_equal Parser, Client.parser
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ require "test_helper"
2
+
3
+ class TestPaymentExecutionDetail < MiniTest::Test
4
+ def setup
5
+ node = load_xml_fixture("orders").xpath("//xmlns:PaymentExecutionDetail").first
6
+ @ped = PaymentExecutionDetail.new(node)
7
+ end
8
+
9
+ def test_has_payment_execution_detail_items
10
+ refute_empty @ped.to_a
11
+ @ped.each do |pedi|
12
+ assert_kind_of PaymentExecutionDetailItem, pedi
13
+ end
14
+ end
15
+ end