amazon-drs 0.0.1 → 0.0.2

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: fd05c5984943ffa279e99ad7505294c457a08d40
4
- data.tar.gz: 9956785a162a929d1762c5ce944fe4a54e3c6281
3
+ metadata.gz: f6dddd1e7901f7b68d205c6c434eaf426b3bd4c1
4
+ data.tar.gz: df15a2b0f9de225a50e87dd1e84833d97862c4b2
5
5
  SHA512:
6
- metadata.gz: e33ea67ba8b0d9f1798870e02839eb764d9f95fbdd5bf4ef5242073884561b0ddca501ca8470a1edde1ec6eaa9a46c28a4501cb77901fd1f4ed125dd063c08b0
7
- data.tar.gz: cd8c1a39f3f9ffabe438bad67c047291c1aa2c5dffde97d377d1d3998cca813c9273b983d6d6e56bd5560a92fed95bc29179ce41a58e98a428a97635cb2a3cd2
6
+ metadata.gz: 6372c95d4bbc790017082fffbb0fb722ec9a37aaa1268085634dde811310e8b77240a5002840286426797566ad7e6fbf1da4412633a9beffed1b00eb1f24f203
7
+ data.tar.gz: b7e54e17b3f5ee8cd6f5928fc9987c54ecff4ad47e1030dcdddf97917fc4e994625afb1c20f73ece70bc70e8e767299c769e9b083399eccf0eb2a129e585c14e
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ group :development do
4
+ gem 'webmock'
5
+ end
6
+
3
7
  # Specify your gem's dependencies in adash.gemspec
4
8
  gemspec
data/Rakefile CHANGED
@@ -1,3 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
2
3
 
3
- task :default => :spec
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = Dir[ 'test/**/*_test.rb' ]
8
+ end
9
+
10
+ task :default => :test
@@ -21,4 +21,6 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_development_dependency "bundler", "~> 1.13"
23
23
  spec.add_development_dependency "rake", "~> 12.0"
24
+ spec.add_development_dependency "test-unit"
25
+ spec.add_development_dependency "webmock"
24
26
  end
@@ -0,0 +1,15 @@
1
+ require 'amazon-drs/base'
2
+
3
+ module AmazonDrs
4
+ class AccessToken < Base
5
+ attr_accessor :access_token, :refresh_token, :token_type, :expires_in
6
+
7
+ def parse_body(body)
8
+ json = JSON.parse(body)
9
+ @access_token = json['access_token']
10
+ @refresh_token = json['refresh_token']
11
+ @token_type = json['token_type']
12
+ @expires_in = json['expires_in']
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,40 @@
1
+ require 'time'
2
+
3
+ module AmazonDrs
4
+ class Base
5
+ attr_accessor :status_code, :request_id, :date, :error_type, :error_description_url
6
+
7
+ def initialize(response)
8
+ @response = response
9
+ @status_code = response.code.to_i
10
+ parse_header(response.header)
11
+ parse_body(response.body)
12
+ end
13
+
14
+ def parse_header(headers)
15
+ headers.each do |key, value|
16
+ case key.downcase
17
+ when 'x-amzn-errortype'
18
+ # Value examples:
19
+ # InvalidTokenException:http://internal.amazon.com/coral/com.amazon.parkeraccioservice/
20
+ # OA2InvalidRequestException:http://internal.amazon.com/coral/com.amazon.panda/
21
+ @error_type, @error_description_url = value.split(':', 2)
22
+ when 'x-amzn-requestid'
23
+ # Example:
24
+ # X-Amzn-Requestid: d296d296-d1d1-1111-8c8c-0b43820b4382
25
+ @request_id = value
26
+ when 'x-amz-date'
27
+ # Example:
28
+ # X-Amz-Date: Mon, 02 Jan 2017 22:35:53 GMT
29
+ @date = Time.rfc2822(value)
30
+ end
31
+ end
32
+ end
33
+ private :parse_header
34
+
35
+ def parse_body(body)
36
+ # abstract
37
+ end
38
+ private :parse_body
39
+ end
40
+ end
@@ -4,9 +4,15 @@ require 'json'
4
4
  require 'yaml'
5
5
  require 'time'
6
6
  require 'date'
7
+ require 'amazon-drs/deregistrate_device'
8
+ require 'amazon-drs/subscription_info'
9
+ require 'amazon-drs/replenish'
10
+ require 'amazon-drs/error'
11
+ require 'amazon-drs/slot_status'
12
+ require 'amazon-drs/access_token'
7
13
 
8
14
  class Net::HTTPResponse
9
- attr_accessor :json
15
+ attr_accessor :json
10
16
  end
11
17
 
12
18
  module AmazonDrs
@@ -40,7 +46,12 @@ module AmazonDrs
40
46
  'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsDeregisterInput@1.0'
41
47
  }
42
48
  path = "/deviceModels/#{@device_model}/devices/#{@serial}/registration"
43
- request_drs(:delete, path, headers: headers)
49
+ response = request_drs(:delete, path, headers: headers)
50
+ if response.code == '200'
51
+ ::AmazonDrs::DeregistrateDevice.new(response)
52
+ else
53
+ ::AmazonDrs::Error.new(response)
54
+ end
44
55
  end
45
56
 
46
57
  def device_status(most_recently_active_date)
@@ -50,6 +61,11 @@ module AmazonDrs
50
61
  }
51
62
  path = '/deviceStatus'
52
63
  request_drs(:post, path, headers: headers, params: { 'mostRecentlyActiveDate' => convert_to_iso8601(most_recently_active_date) })
64
+ if response.code == '200'
65
+ ::AmazonDrs::DeviceStatus.new(response)
66
+ else
67
+ ::AmazonDrs::Error.new(response)
68
+ end
53
69
  end
54
70
 
55
71
  def subscription_info
@@ -58,7 +74,12 @@ module AmazonDrs
58
74
  'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsSubscriptionInfoInput@1.0'
59
75
  }
60
76
  path = '/subscriptionInfo'
61
- request_drs(:get, path, headers: headers)
77
+ response = request_drs(:get, path, headers: headers)
78
+ if response.code == '200'
79
+ ::AmazonDrs::SubscriptionInfo.new(response)
80
+ else
81
+ ::AmazonDrs::Error.new(response)
82
+ end
62
83
  end
63
84
 
64
85
  def slot_status(slot_id, expected_replenishment_date, remaining_quantity_in_unit, original_quantity_in_unit, total_quantity_on_hand, last_use_date)
@@ -74,7 +95,12 @@ module AmazonDrs
74
95
  'totalQuantityOnHand' => total_quantity_on_hand,
75
96
  'lastUseDate' => convert_to_iso8601(last_use_date)
76
97
  }
77
- request_drs(:post, path, headers: headers, params: params)
98
+ response = request_drs(:post, path, headers: headers, params: params)
99
+ if response.code == '200'
100
+ ::AmazonDrs::SlotStatus.new(response)
101
+ else
102
+ ::AmazonDrs::Error.new(response)
103
+ end
78
104
  end
79
105
 
80
106
  def replenish(slot_id)
@@ -84,12 +110,11 @@ module AmazonDrs
84
110
  }
85
111
  path = "/replenish/#{slot_id}"
86
112
  response = request_drs(:post, path, headers: headers)
87
- # TODO: imprement response processing
88
- # https://developer.amazon.com/public/solutions/devices/dash-replenishment-service/docs/dash-replenish-endpoint
89
- # response examples:
90
- # {"message":"Unable to place order - slot : <01234567-89ab-cdef-0123-456789abcdef> is unsubscribed."}
91
- # {"detailCode":"STANDARD_ORDER_PLACED","eventInstanceId":"amzn1.dash.v2.o.xxxxxx......"}
92
- # {"detailCode":"ORDER_INPROGRESS","eventInstanceId":"amzn1.dash.v2.o.xxxxxx......"}
113
+ if response.code == '200'
114
+ ::AmazonDrs::Replenish.new(response)
115
+ else
116
+ ::AmazonDrs::Error.new(response)
117
+ end
93
118
  end
94
119
 
95
120
  def get_token
@@ -98,11 +123,10 @@ module AmazonDrs
98
123
  else
99
124
  resp = request_token
100
125
  process_token_response(resp)
126
+ resp
101
127
  end
102
128
  end
103
129
 
104
- private
105
-
106
130
  def convert_to_iso8601(input)
107
131
  case input
108
132
  when Date, Time
@@ -113,19 +137,19 @@ module AmazonDrs
113
137
  input.to_s
114
138
  end
115
139
  end
140
+ private :convert_to_iso8601
116
141
 
117
142
  def process_token_response(resp)
118
- if resp.json['error']
119
- puts resp.json['error']
120
- puts resp.json['error_description']
143
+ if resp.kind_of?(AmazonDrs::Error)
121
144
  nil
122
145
  else
123
- @access_token = resp.json['access_token']
124
- @refresh_token = resp.json['refresh_token']
146
+ @access_token = resp.access_token
147
+ @refresh_token = resp.refresh_token
125
148
  @on_new_token.call(@access_token, @refresh_token) if @on_new_token
126
149
  @access_token
127
150
  end
128
151
  end
152
+ private :process_token_response
129
153
 
130
154
  def request_drs(method, path, headers: {}, params: {})
131
155
  url = "https://#{@drs_host}#{path}"
@@ -134,7 +158,7 @@ module AmazonDrs
134
158
  end
135
159
  if @access_token.nil?
136
160
  token = get_token
137
- if token.nil?
161
+ if resp.kind_of?(AmazonDrs::Error)
138
162
  raise 'Failed to get token'
139
163
  end
140
164
  end
@@ -147,6 +171,7 @@ module AmazonDrs
147
171
  resp
148
172
  end
149
173
  end
174
+ private :request_drs
150
175
 
151
176
  def refresh_access_token
152
177
  params = {
@@ -158,6 +183,7 @@ module AmazonDrs
158
183
  @access_token = nil
159
184
  request(:post, "https://#{@amazon_host}/auth/o2/token", params: params)
160
185
  end
186
+ private :refresh_access_token
161
187
 
162
188
  def request_token
163
189
  params = {
@@ -167,8 +193,14 @@ module AmazonDrs
167
193
  client_secret: @client_secret,
168
194
  redirect_uri: @redirect_uri
169
195
  }
170
- request(:post, "https://#{@amazon_host}/auth/o2/token", params: params)
196
+ response = request(:post, "https://#{@amazon_host}/auth/o2/token", params: params)
197
+ if response.code == '200'
198
+ ::AmazonDrs::AccessToken.new(response)
199
+ else
200
+ ::AmazonDrs::Error.new(response)
201
+ end
171
202
  end
203
+ private :request_token
172
204
 
173
205
  def request(method, url, headers: {}, params: {})
174
206
  uri = URI.parse(url)
@@ -210,5 +242,6 @@ module AmazonDrs
210
242
  response.json = JSON.parse(response.body)
211
243
  response
212
244
  end
245
+ private :request
213
246
  end
214
247
  end
@@ -0,0 +1,6 @@
1
+ require 'amazon-drs/base'
2
+
3
+ module AmazonDrs
4
+ class DeregistrateDevice < Base
5
+ end
6
+ end
@@ -0,0 +1,7 @@
1
+ require 'amazon-drs/base'
2
+
3
+ module AmazonDrs
4
+ class DeviceStatus < Base
5
+ end
6
+ end
7
+
@@ -0,0 +1,15 @@
1
+ require 'amazon-drs/base'
2
+
3
+ module AmazonDrs
4
+ class Error < Base
5
+ attr_accessor :message, :error_description, :error
6
+
7
+ def parse_body(body)
8
+ json = JSON.parse(body)
9
+ @message = json['message'] if json['message']
10
+ @error = json['error'] if json['error']
11
+ @error_description = json['error_description'] if json['error_description']
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,20 @@
1
+ require 'amazon-drs/base'
2
+
3
+ module AmazonDrs
4
+ class Replenish < Base
5
+ attr_accessor :event_instance_id, :detail_code, :message
6
+
7
+ def parse_body(body)
8
+ json = JSON.parse(body)
9
+ @event_instance_id = json['eventInstanceId'] if json['eventInstanceId']
10
+ if json['detailCode']
11
+ # STANDARD_ORDER_PLACED, TEST_ORDER_PLACED or ORDER_INPROGRESS
12
+ @detail_code = json['detailCode']
13
+ else
14
+ @detail_code = 'ERROR'
15
+ end
16
+ @message = json['message'] if json['message']
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,7 @@
1
+ require 'amazon-drs/base'
2
+
3
+ module AmazonDrs
4
+ class SlotStatus < Base
5
+ end
6
+ end
7
+
@@ -0,0 +1,17 @@
1
+ require 'json'
2
+ require 'amazon-drs/base'
3
+
4
+ module AmazonDrs
5
+ class SubscriptionInfo < Base
6
+ attr_accessor :slots
7
+
8
+ def parse_body(body)
9
+ json = JSON.parse(body)
10
+ @slots = {}
11
+ json['slotsSubscriptionStatus'].each_pair do |slot_id, available|
12
+ @slots[slot_id] = available
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -1,3 +1,3 @@
1
1
  module AmazonDrs
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-drs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code Ass
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-24 00:00:00.000000000 Z
11
+ date: 2017-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description: |-
42
70
  amazon-drs is for Amazon Dash Replenishment Service.
43
71
  You can use this after authorized by Login with Amazon.
@@ -58,7 +86,15 @@ files:
58
86
  - bin/setup
59
87
  - data/client
60
88
  - lib/amazon-drs.rb
89
+ - lib/amazon-drs/access_token.rb
90
+ - lib/amazon-drs/base.rb
61
91
  - lib/amazon-drs/client.rb
92
+ - lib/amazon-drs/deregistrate_device.rb
93
+ - lib/amazon-drs/device_status.rb
94
+ - lib/amazon-drs/error.rb
95
+ - lib/amazon-drs/replenish.rb
96
+ - lib/amazon-drs/slot_status.rb
97
+ - lib/amazon-drs/subscription_info.rb
62
98
  - lib/amazon-drs/version.rb
63
99
  homepage: https://github.com/aycabta/amazon-drs
64
100
  licenses:
@@ -80,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
116
  version: '0'
81
117
  requirements: []
82
118
  rubyforge_project:
83
- rubygems_version: 2.5.2
119
+ rubygems_version: 2.6.8
84
120
  signing_key:
85
121
  specification_version: 4
86
122
  summary: amazon-drs is for Amazon Dash Replenishment Service