peddler 2.4.5 → 3.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +406 -134
  3. data/lib/peddler/access_token.rb +76 -0
  4. data/lib/peddler/api/amazon_warehousing_and_distribution_2024_05_09.rb +80 -0
  5. data/lib/peddler/api/aplus_content_2020_11_01.rb +208 -0
  6. data/lib/peddler/api/application_management_2023_11_30.rb +25 -0
  7. data/lib/peddler/api/catalog_items_2020_12_01.rb +70 -0
  8. data/lib/peddler/api/catalog_items_2022_04_01.rb +83 -0
  9. data/lib/peddler/api/catalog_items_v0.rb +90 -0
  10. data/lib/peddler/api/data_kiosk_2023_11_15.rb +92 -0
  11. data/lib/peddler/api/easy_ship_2022_03_23.rb +108 -0
  12. data/lib/peddler/api/fba_inbound_eligibility_v1.rb +36 -0
  13. data/lib/peddler/api/fba_inventory_v1.rb +106 -0
  14. data/lib/peddler/api/feeds_2021_06_30.rb +104 -0
  15. data/lib/peddler/api/finances_v0.rb +121 -0
  16. data/lib/peddler/api/fulfillment_inbound_2024_03_20.rb +660 -0
  17. data/lib/peddler/api/fulfillment_inbound_v0.rb +314 -0
  18. data/lib/peddler/api/fulfillment_outbound_2020_07_01.rb +222 -0
  19. data/lib/peddler/api/listings_items_2020_09_01.rb +84 -0
  20. data/lib/peddler/api/listings_items_2021_08_01.rb +154 -0
  21. data/lib/peddler/api/listings_restrictions_2021_08_01.rb +37 -0
  22. data/lib/peddler/api/merchant_fulfillment_v0.rb +64 -0
  23. data/lib/peddler/api/messaging_v1.rb +241 -0
  24. data/lib/peddler/api/notifications_v1.rb +114 -0
  25. data/lib/peddler/api/orders_v0.rb +264 -0
  26. data/lib/peddler/api/product_fees_v0.rb +69 -0
  27. data/lib/peddler/api/product_pricing_2022_05_01.rb +43 -0
  28. data/lib/peddler/api/product_pricing_v0.rb +144 -0
  29. data/lib/peddler/api/product_type_definitions_2020_09_01.rb +75 -0
  30. data/lib/peddler/api/replenishment_2022_11_07.rb +45 -0
  31. data/lib/peddler/api/reports_2021_06_30.rb +139 -0
  32. data/lib/peddler/api/sales_v1.rb +69 -0
  33. data/lib/peddler/api/sellers_v1.rb +33 -0
  34. data/lib/peddler/api/services_v1.rb +308 -0
  35. data/lib/peddler/api/shipment_invoicing_v0.rb +46 -0
  36. data/lib/peddler/api/shipping_v1.rb +107 -0
  37. data/lib/peddler/api/shipping_v2.rb +267 -0
  38. data/lib/peddler/api/solicitations_v1.rb +53 -0
  39. data/lib/peddler/api/supply_sources_2020_07_01.rb +82 -0
  40. data/lib/peddler/api/tokens_2021_03_01.rb +30 -0
  41. data/lib/peddler/api/uploads_2020_11_01.rb +40 -0
  42. data/lib/peddler/api/vendor_direct_fulfillment_inventory_v1.rb +24 -0
  43. data/lib/peddler/api/vendor_direct_fulfillment_orders_2021_12_28.rb +72 -0
  44. data/lib/peddler/api/vendor_direct_fulfillment_orders_v1.rb +72 -0
  45. data/lib/peddler/api/vendor_direct_fulfillment_payments_v1.rb +23 -0
  46. data/lib/peddler/api/vendor_direct_fulfillment_sandbox_test_data_2021_10_28.rb +35 -0
  47. data/lib/peddler/api/vendor_direct_fulfillment_shipping_2021_12_28.rb +177 -0
  48. data/lib/peddler/api/vendor_direct_fulfillment_shipping_v1.rb +165 -0
  49. data/lib/peddler/api/vendor_direct_fulfillment_transactions_2021_12_28.rb +24 -0
  50. data/lib/peddler/api/vendor_direct_fulfillment_transactions_v1.rb +24 -0
  51. data/lib/peddler/api/vendor_invoices_v1.rb +22 -0
  52. data/lib/peddler/api/vendor_orders_v1.rb +148 -0
  53. data/lib/peddler/api/vendor_shipments_v1.rb +120 -0
  54. data/lib/peddler/api/vendor_transaction_status_v1.rb +24 -0
  55. data/lib/peddler/api.rb +115 -0
  56. data/lib/peddler/error.rb +30 -0
  57. data/lib/peddler/region.rb +30 -0
  58. data/lib/peddler/version.rb +1 -1
  59. data/lib/peddler.rb +52 -1
  60. metadata +70 -80
  61. data/lib/mws/easy_ship/client.rb +0 -93
  62. data/lib/mws/easy_ship.rb +0 -3
  63. data/lib/mws/feeds/client.rb +0 -122
  64. data/lib/mws/feeds.rb +0 -3
  65. data/lib/mws/finances/client.rb +0 -82
  66. data/lib/mws/finances.rb +0 -3
  67. data/lib/mws/fulfillment_inbound_shipment/client.rb +0 -388
  68. data/lib/mws/fulfillment_inbound_shipment.rb +0 -3
  69. data/lib/mws/fulfillment_inventory/client.rb +0 -57
  70. data/lib/mws/fulfillment_inventory.rb +0 -3
  71. data/lib/mws/fulfillment_outbound_shipment/client.rb +0 -211
  72. data/lib/mws/fulfillment_outbound_shipment.rb +0 -3
  73. data/lib/mws/merchant_fulfillment/client.rb +0 -87
  74. data/lib/mws/merchant_fulfillment.rb +0 -3
  75. data/lib/mws/off_amazon_payments.rb +0 -3
  76. data/lib/mws/orders/client.rb +0 -120
  77. data/lib/mws/orders.rb +0 -3
  78. data/lib/mws/products/client.rb +0 -268
  79. data/lib/mws/products.rb +0 -3
  80. data/lib/mws/recommendations/client.rb +0 -72
  81. data/lib/mws/recommendations.rb +0 -3
  82. data/lib/mws/reports/client.rb +0 -236
  83. data/lib/mws/reports.rb +0 -3
  84. data/lib/mws/sellers/client.rb +0 -44
  85. data/lib/mws/sellers.rb +0 -3
  86. data/lib/mws/shipment_invoicing/client.rb +0 -74
  87. data/lib/mws/shipment_invoicing.rb +0 -3
  88. data/lib/mws/subscriptions/client.rb +0 -179
  89. data/lib/mws/subscriptions.rb +0 -3
  90. data/lib/mws.rb +0 -29
  91. data/lib/peddler/client.rb +0 -159
  92. data/lib/peddler/errors/builder.rb +0 -64
  93. data/lib/peddler/errors/class_generator.rb +0 -40
  94. data/lib/peddler/errors/error.rb +0 -45
  95. data/lib/peddler/errors/parser.rb +0 -28
  96. data/lib/peddler/flat_file_parser.rb +0 -56
  97. data/lib/peddler/headers.rb +0 -102
  98. data/lib/peddler/marketplace.rb +0 -79
  99. data/lib/peddler/operation.rb +0 -78
  100. data/lib/peddler/parser.rb +0 -33
  101. data/lib/peddler/structured_list.rb +0 -23
  102. data/lib/peddler/vcr_matcher.rb +0 -97
  103. data/lib/peddler/xml_parser.rb +0 -38
  104. data/lib/peddler/xml_response_parser.rb +0 -25
@@ -1,159 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
- require 'jeff'
5
- require 'peddler/errors/builder'
6
- require 'peddler/marketplace'
7
- require 'peddler/operation'
8
- require 'peddler/parser'
9
-
10
- module Peddler
11
- # An abstract client
12
- #
13
- # Subclass this to implement an MWS API section.
14
- class Client
15
- extend Forwardable
16
- include Jeff
17
-
18
- class << self
19
- # @!visibility private
20
- attr_accessor :parser, :path, :version
21
-
22
- private
23
-
24
- def inherited(base)
25
- super
26
- base.parser = parser
27
- base.params params
28
- end
29
- end
30
-
31
- params 'SellerId' => -> { merchant_id },
32
- 'MWSAuthToken' => -> { auth_token },
33
- 'Version' => -> { version }
34
- self.parser = Parser
35
-
36
- def_delegators :marketplace, :host, :encoding
37
- def_delegators :'self.class', :parser, :version
38
-
39
- # Creates a new client
40
- # @param [Hash] opts
41
- # @option opts [String] :aws_access_key_id
42
- # @option opts [String] :aws_secret_access_key
43
- # @option opts [String, Peddler::Marketplace] :marketplace
44
- # @option opts [String] :merchant_id
45
- # @option opts [String] :auth_token
46
- def initialize(opts = {})
47
- opts.each { |k, v| send("#{k}=", v) }
48
- end
49
-
50
- # The MWS Auth Token for a seller's account
51
- # @note You can omit this if you are accessing your own seller account
52
- # @return [String]
53
- attr_accessor :auth_token
54
-
55
- # The seller's Merchant ID
56
- # @note Amazon also refers to this as Seller ID or Merchant Token
57
- # @return [String]
58
- attr_accessor :merchant_id
59
-
60
- # The marketplace where you signed up as application developer
61
- # @note You can pass the two-letter country code of the marketplace as
62
- # shorthand when setting
63
- # @return [Peddler::Marketplace]
64
- attr_reader :marketplace
65
-
66
- # @!parse attr_writer :marketplace
67
- def marketplace=(marketplace)
68
- @marketplace =
69
- if marketplace.is_a?(Marketplace)
70
- marketplace
71
- else
72
- Marketplace.find(marketplace)
73
- end
74
- end
75
-
76
- # The body of the HTTP request
77
- # @return [String]
78
- attr_reader :body
79
-
80
- # @!parse attr_writer :body
81
- def body=(str)
82
- str ? add_content(str) : clear_content!
83
- end
84
-
85
- # @!visibility private
86
- attr_writer :path
87
-
88
- # @!visibility private
89
- def path
90
- @path ||= self.class.path
91
- end
92
-
93
- # @!visibility private
94
- def defaults
95
- @defaults ||= { expects: 200 }
96
- end
97
-
98
- # @!visibility private
99
- def headers
100
- @headers ||= {}
101
- end
102
-
103
- # @!visibility private
104
- def aws_endpoint
105
- "https://#{host}#{path}"
106
- end
107
-
108
- # @!visibility private
109
- def operation(action = nil)
110
- action ? @operation = Operation.new(action) : @operation
111
- end
112
-
113
- # @!visibility private
114
- def run(&block)
115
- opts = build_options
116
- opts.store(:response_block, block) if block
117
- res = post(opts)
118
- self.body = nil if res.status == 200
119
-
120
- parser.new(res, encoding)
121
- rescue ::Excon::Error::HTTPStatus => e
122
- handle_http_status_error(e)
123
- end
124
-
125
- private
126
-
127
- def clear_content!
128
- headers.delete('Content-Type')
129
- @body = nil
130
- end
131
-
132
- def add_content(content)
133
- @body = content
134
- if content.encoding.names.include?('BINARY')
135
- headers['Content-Type'] = 'application/octet-stream'
136
- elsif content.start_with?('<?xml')
137
- headers['Content-Type'] = 'text/xml'
138
- else
139
- headers['Content-Type'] =
140
- "text/tab-separated-values; charset=#{encoding}"
141
- @body = @body.encode(encoding)
142
- end
143
- end
144
-
145
- def extract_options(args)
146
- args.last.is_a?(Hash) ? args.pop : {}
147
- end
148
-
149
- def build_options
150
- opts = defaults.merge(query: operation, headers: headers)
151
- body ? opts.update(body: body) : opts
152
- end
153
-
154
- def handle_http_status_error(error)
155
- new_error = Errors::Builder.call(error)
156
- raise new_error || error
157
- end
158
- end
159
- end
@@ -1,64 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'excon'
4
- require 'forwardable'
5
- require 'peddler/errors/class_generator'
6
- require 'peddler/errors/parser'
7
-
8
- module Peddler
9
- module Errors
10
- # @!visibility private
11
- class Builder
12
- extend Forwardable
13
-
14
- DIGIT_AS_FIRST_CHAR = /^\d/.freeze
15
- private_constant :DIGIT_AS_FIRST_CHAR
16
-
17
- def_delegator :error, :response
18
-
19
- def self.call(error)
20
- new(error).build
21
- end
22
-
23
- attr_reader :error
24
-
25
- def initialize(error)
26
- @error = error
27
- end
28
-
29
- def build
30
- parse_original_response
31
- return if no_error_response?
32
- return if bad_class_name?
33
-
34
- error_class.new(error_message, error)
35
- end
36
-
37
- private
38
-
39
- def bad_class_name?
40
- error_name.match?(DIGIT_AS_FIRST_CHAR)
41
- end
42
-
43
- def no_error_response?
44
- response.parse.nil?
45
- end
46
-
47
- def error_class
48
- ClassGenerator.call(error_name)
49
- end
50
-
51
- def error_name
52
- response.code
53
- end
54
-
55
- def error_message
56
- response.message
57
- end
58
-
59
- def parse_original_response
60
- error.instance_variable_set :@response, Parser.new(error.response)
61
- end
62
- end
63
- end
64
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'singleton'
4
- require 'peddler/errors/error'
5
-
6
- module Peddler
7
- module Errors
8
- # @!visibility private
9
- class ClassGenerator
10
- include Singleton
11
-
12
- def self.call(name)
13
- instance.generate(name)
14
- end
15
-
16
- def initialize
17
- @mutex = Mutex.new
18
- end
19
-
20
- def generate(name)
21
- with_mutex do
22
- if Errors.const_defined?(name)
23
- error = Errors.const_get(name)
24
- return error if error <= Error
25
-
26
- raise TypeError, "#{name} must be a Peddler::Errors::Error"
27
- end
28
-
29
- Errors.const_set(name, Class.new(Error))
30
- end
31
- end
32
-
33
- private
34
-
35
- def with_mutex(&block)
36
- @mutex.synchronize(&block)
37
- end
38
- end
39
- end
40
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'forwardable'
4
-
5
- module Peddler
6
- # @!visibility private
7
- module Errors
8
- # These error codes are common to all Amazon MWS API sections.
9
- #
10
- # @see https://docs.developer.amazonservices.com/en_US/dev_guide/DG_Errors.html
11
- #
12
- # There are quite a few other error types they do not explicitly document
13
- # above. I decided not to define any for the sake of consistency and future
14
- # maintenance.
15
- CODES = %w[
16
- AccessDenied
17
- InputStreamDisconnected
18
- InternalError
19
- InvalidAccessKeyId
20
- InvalidAddress
21
- InvalidParameterValue
22
- QuotaExceeded
23
- RequestThrottled
24
- SignatureDoesNotMatch
25
- ].freeze
26
-
27
- # @!visibility private
28
- class Error < StandardError
29
- extend Forwardable
30
-
31
- attr_reader :cause
32
-
33
- def initialize(msg = nil, cause = nil)
34
- @cause = cause
35
- super msg
36
- end
37
-
38
- def_delegator :cause, :response
39
- end
40
-
41
- CODES.each do |name|
42
- const_set name, Class.new(Error)
43
- end
44
- end
45
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'peddler/xml_parser'
4
-
5
- module Peddler
6
- module Errors
7
- # @!visibility private
8
- class Parser < XMLParser
9
- def message
10
- parse['Message']
11
- end
12
-
13
- def type
14
- parse['Type']
15
- end
16
-
17
- def code
18
- parse['Code']
19
- end
20
-
21
- private
22
-
23
- def find_data
24
- xml.dig('ErrorResponse', 'Error')
25
- end
26
- end
27
- end
28
- end
@@ -1,56 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'delegate'
4
- require 'csv'
5
- require 'digest/md5'
6
- require 'peddler/headers'
7
-
8
- module Peddler
9
- # @!visibility private
10
- class FlatFileParser < SimpleDelegator
11
- include Headers
12
-
13
- # http://stackoverflow.com/questions/8073920/importing-csv-quoting-error-is-driving-me-nuts
14
- OPTIONS = { col_sep: "\t", quote_char: "\x00", headers: true }.freeze
15
- private_constant :OPTIONS
16
-
17
- attr_reader :content, :summary
18
-
19
- def initialize(res, encoding)
20
- super(res)
21
- scrub_body!(encoding)
22
- extract_content_and_summary
23
- end
24
-
25
- def parse(&blk)
26
- CSV.parse(content, **OPTIONS, &blk) unless content.empty?
27
- end
28
-
29
- def records_count
30
- summarize if summary
31
- end
32
-
33
- def valid?
34
- headers['Content-MD5'] == Digest::MD5.base64digest(body)
35
- end
36
-
37
- private
38
-
39
- def scrub_body!(encoding)
40
- return if body.encoding == Encoding::UTF_8
41
-
42
- self.body = body.dup.force_encoding(content_charset || encoding)
43
- end
44
-
45
- def extract_content_and_summary
46
- @content = body.encode('UTF-8', invalid: :replace, undef: :replace)
47
- return unless @content.match?(/\t\t.*\n\n/)
48
-
49
- @summary, @content = @content.split("\n\n", 2)
50
- end
51
-
52
- def summarize
53
- summary.split("\n\t")[1, 2].map { |line| line.split("\t\t") }.to_h
54
- end
55
- end
56
- end
@@ -1,102 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'time'
4
-
5
- module Peddler
6
- # Parses useful metadata returned in response headers
7
- module Headers
8
- # The size of the response body in bytes
9
- # @return [String, nil]
10
- def content_length
11
- return unless headers['Content-Length']
12
-
13
- headers['Content-Length'].to_i
14
- end
15
-
16
- # The MD5 digest of the response body
17
- # @return [String, nil]
18
- def content_md5
19
- headers['Content-MD5']
20
- end
21
-
22
- # The MIME type of the response
23
- # @return [String, nil]
24
- def content_media_type
25
- return unless headers['Content-Type']
26
-
27
- headers['Content-Type'].split(';').first
28
- end
29
-
30
- # The general category into which the MIME type falls
31
- # @return [String, nil]
32
- def content_type
33
- return unless content_media_type
34
-
35
- content_media_type.split('/').first
36
- end
37
-
38
- # The exact kind of data of the specified type the MIME type represents
39
- # @return [String, nil]
40
- def content_subtype
41
- return unless content_media_type
42
-
43
- content_media_type.split('/').last
44
- end
45
-
46
- # The character encoding of the response
47
- # @return [Encoding, nil]
48
- def content_charset
49
- match_data = headers['Content-Type']&.match(/charset=(.*);?/)
50
- return unless match_data
51
-
52
- string = match_data[1].upcase
53
- string = 'UTF-8' if string == 'UTF8'
54
-
55
- Encoding.find(string)
56
- end
57
-
58
- # The max hourly request quota for the requested operation
59
- # @return [Integer, nil]
60
- def mws_quota_max
61
- return unless headers['x-mws-quota-max']
62
-
63
- headers['x-mws-quota-max'].to_i
64
- end
65
-
66
- # The remaining hourly request quota for the requested operation
67
- # @return [Integer, nil]
68
- def mws_quota_remaining
69
- return unless headers['x-mws-quota-remaining']
70
-
71
- headers['x-mws-quota-remaining'].to_i
72
- end
73
-
74
- # When the hourly request quota for the requested operation resets
75
- # @return [Time, nil]
76
- def mws_quota_resets_on
77
- return unless headers['x-mws-quota-resetsOn']
78
-
79
- Time.parse(headers['x-mws-quota-resetsOn'])
80
- end
81
-
82
- # The ID of the request
83
- # @return [String, nil]
84
- def mws_request_id
85
- headers['x-mws-request-id']
86
- end
87
-
88
- # The timestamp of the request
89
- # @return [Time, nil]
90
- def mws_timestamp
91
- return unless headers['x-mws-timestamp']
92
-
93
- Time.parse(headers['x-mws-timestamp'])
94
- end
95
-
96
- # The context of the response
97
- # @return [String, nil]
98
- def mws_response_context
99
- headers['x-mws-response-context']
100
- end
101
- end
102
- end
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Peddler
4
- # @!visibility private
5
- # @see https://docs.developer.amazonservices.com/en_US/dev_guide/DG_Endpoints.html
6
- class Marketplace
7
- class << self
8
- attr_reader :all
9
-
10
- def find(key)
11
- missing_key! unless key
12
- marketplace = key.size == 2 ? find_by_country(key) : find_by_id(key)
13
-
14
- marketplace || not_found!(key)
15
- end
16
-
17
- private
18
-
19
- def find_by_country(code)
20
- code = 'GB' if code == 'UK'
21
- all.find { |marketplace| marketplace.country_code == code }
22
- end
23
-
24
- def find_by_id(id)
25
- all.find { |marketplace| marketplace.id == id }
26
- end
27
-
28
- def missing_key!
29
- raise ArgumentError, 'missing marketplace'
30
- end
31
-
32
- def not_found!(country_code)
33
- raise ArgumentError, %("#{country_code}" is not a valid marketplace)
34
- end
35
- end
36
-
37
- attr_reader :id, :country_code, :host
38
-
39
- def initialize(id, country_code, host)
40
- @id = id
41
- @country_code = country_code
42
- @host = host
43
- end
44
-
45
- def encoding
46
- case country_code
47
- when 'JP'
48
- 'Windows-31J'
49
- else
50
- 'CP1252'
51
- end
52
- end
53
-
54
- @all = [
55
- ['A2Q3Y263D00KWC', 'BR', 'mws.amazonservices.com'],
56
- ['A2EUQ1WTGCTBG2', 'CA', 'mws.amazonservices.com'],
57
- ['A1AM78C64UM0Y8', 'MX', 'mws.amazonservices.com'],
58
- ['ATVPDKIKX0DER', 'US', 'mws.amazonservices.com'],
59
- ['A2VIGQ35RCS4UG', 'AE', 'mws.amazonservices.ae'],
60
- ['A1PA6795UKMFR9', 'DE', 'mws-eu.amazonservices.com'],
61
- ['ARBP9OOSHTCHU', 'EG', 'mws-eu.amazonservices.com'],
62
- ['A1RKKUPIHCS9HS', 'ES', 'mws-eu.amazonservices.com'],
63
- ['A13V1IB3VIYZZH', 'FR', 'mws-eu.amazonservices.com'],
64
- ['A1F83G8C2ARO7P', 'GB', 'mws-eu.amazonservices.com'],
65
- ['A21TJRUUN4KGV', 'IN', 'mws.amazonservices.in'],
66
- ['APJ6JRA9NG5V4', 'IT', 'mws-eu.amazonservices.com'],
67
- ['A1805IZSGTT6HS', 'NL', 'mws-eu.amazonservices.com'],
68
- ['A17E79C6D8DWNP', 'SA', 'mws-eu.amazonservices.com'],
69
- ['A33AVAJ2PDY3EV', 'TR', 'mws-eu.amazonservices.com'],
70
- ['A2NODRKZP88ZB9', 'SE', 'mws-eu.amazonservices.com'],
71
- ['A19VAU5U5O7RUS', 'SG', 'mws-fe.amazonservices.com'],
72
- ['A39IBJ37TRP1C6', 'AU', 'mws.amazonservices.com.au'],
73
- ['A1VC38T7YXB528', 'JP', 'mws.amazonservices.jp'],
74
- ['A1C3SOZRARQ6R3', 'PL', 'mws-eu.amazonservices.com']
75
- ].map do |values|
76
- new(*values)
77
- end
78
- end
79
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'delegate'
4
- require 'time'
5
- require 'peddler/structured_list'
6
-
7
- module Peddler
8
- # @!visibility private
9
- class Operation < SimpleDelegator
10
- CAPITAL_LETTERS = /[A-Z]/.freeze
11
- ALL_CAPS = %w[sku cod].freeze
12
- private_constant :CAPITAL_LETTERS, :ALL_CAPS
13
-
14
- def initialize(action)
15
- super('Action' => action)
16
- end
17
-
18
- def structure!(*list_keys)
19
- list_key = list_keys.shift
20
- keys
21
- .find_all { |key| key.end_with?(list_key) }
22
- .each do |found_key|
23
- builder = StructuredList.new(found_key, *list_keys)
24
- vals = delete(found_key)
25
- update(builder.build(vals))
26
- end
27
-
28
- self
29
- end
30
-
31
- def store(key, val, parent = nil)
32
- key = [parent, camelize(key)].compact.join('.')
33
- val = format_known_types(val)
34
-
35
- if val.is_a?(Hash)
36
- val.each { |keyval| store(*keyval, key) }
37
- else
38
- __getobj__.store(key, val)
39
- end
40
- end
41
-
42
- def update(hsh)
43
- hsh.each { |key, val| store(key, val) }
44
- self
45
- end
46
-
47
- alias add update
48
-
49
- private
50
-
51
- def camelize(key)
52
- return key unless key.is_a?(Symbol)
53
- return key.to_s if key.match?(CAPITAL_LETTERS)
54
-
55
- key
56
- .to_s
57
- .split('_')
58
- .map { |token| capitalize(token) }
59
- .join
60
- end
61
-
62
- def capitalize(word)
63
- if ALL_CAPS.any?(word)
64
- word.upcase
65
- else
66
- word.capitalize
67
- end
68
- end
69
-
70
- def format_known_types(val)
71
- val = val.utc.iso8601(2) if val.is_a?(Time)
72
- val = val.iso8601 if val.is_a?(Date)
73
- val = val.to_h if val.is_a?(Struct)
74
-
75
- val
76
- end
77
- end
78
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'peddler/flat_file_parser'
4
- require 'peddler/xml_response_parser'
5
-
6
- module Peddler
7
- # @!visibility private
8
- module Parser
9
- class << self
10
- # We're massaging data produced by a motley army of developers. It's
11
- # messy.
12
- def new(res, encoding)
13
- # Don't parse if there's no body
14
- return res unless res.body
15
-
16
- if xml?(res)
17
- XMLResponseParser.new(res)
18
- else
19
- # Amazon returns a variety of content types for flat files. I simply
20
- # assume anything not XML is a flat file.
21
- FlatFileParser.new(res, encoding)
22
- end
23
- end
24
-
25
- def xml?(res)
26
- return true if res.headers['Content-Type'].start_with?('text/xml')
27
- return true if res.body.start_with?('<?xml')
28
-
29
- false
30
- end
31
- end
32
- end
33
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Peddler
4
- # @!visibility private
5
- class StructuredList
6
- def initialize(*keys)
7
- @keys = keys
8
- end
9
-
10
- def build(vals)
11
- Array(vals)
12
- .flatten
13
- .each_with_index
14
- .reduce({}) { |hsh, (v, i)| hsh.merge(compose_key(i + 1) => v) }
15
- end
16
-
17
- private
18
-
19
- def compose_key(index)
20
- (@keys.dup << index).join('.')
21
- end
22
- end
23
- end