amazon-drs 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +31 -0
- data/Rakefile +3 -0
- data/amazon-drs.gemspec +24 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/data/client +3 -0
- data/lib/amazon-drs.rb +6 -0
- data/lib/amazon-drs/client.rb +213 -0
- data/lib/amazon-drs/version.rb +3 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d01e06ec1d27306afaa20906dfc49fe080029577
|
4
|
+
data.tar.gz: 657c7ac0322d6ad8a1e227ff22d9e4008ee1e754
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c0dfff466e209595dd8d457bd934b10b7e3c804a0727850e55dc8359b85cdb0845dc3cda50b69ca8fcca479b61a05a8b58d36f8290fdc1ac95c7bd48c21dd00
|
7
|
+
data.tar.gz: 57ea3f5340b3f651b43d6860778c65199adcc693ae42269cc1ac258c0c4d7b6f528a677632e34d71845b7599ac9add4078e51749071b455c2c2696861adbabe6
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Code Ass
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# AmazonDrs
|
2
|
+
|
3
|
+
It's for Amazon Dash Replenishment Service.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'amazon-drs'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install amazon-drs
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/aycabta/amazon-drs.
|
26
|
+
|
27
|
+
|
28
|
+
## License
|
29
|
+
|
30
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
31
|
+
|
data/Rakefile
ADDED
data/amazon-drs.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'amazon-drs/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "amazon-drs"
|
8
|
+
spec.version = AmazonDrs::VERSION
|
9
|
+
spec.authors = ["Code Ass"]
|
10
|
+
spec.email = ["aycabta@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{amazon-drs is for Amazon Dash Replenishment Service}
|
13
|
+
spec.description = %Q{amazon-drs is for Amazon Dash Replenishment Service.\nYou can use this after authorized by Login with Amazon.}
|
14
|
+
spec.homepage = "https://github.com/aycabta/amazon-drs"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f| f.match(%r{^(test|spec|features)/}) end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
23
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
24
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "adash"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/data/client
ADDED
data/lib/amazon-drs.rb
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
require 'yaml'
|
5
|
+
require 'time'
|
6
|
+
require 'date'
|
7
|
+
require 'amazon-drs/config'
|
8
|
+
|
9
|
+
class Net::HTTPResponse
|
10
|
+
attr_accessor :json
|
11
|
+
end
|
12
|
+
|
13
|
+
module AmazonDrs
|
14
|
+
class Client
|
15
|
+
attr_accessor :device_model, :serial, :authorization_code, :redirect_uri, :access_token, :refresh_token
|
16
|
+
attr_accessor :on_new_token
|
17
|
+
attr_writer :user_agent
|
18
|
+
|
19
|
+
def initialize(device_model)
|
20
|
+
@drs_host = 'dash-replenishment-service-na.amazon.com'
|
21
|
+
@amazon_host = 'api.amazon.com'
|
22
|
+
@device_model = device_model
|
23
|
+
@serial = nil
|
24
|
+
@authorization_code = nil
|
25
|
+
@redirect_uri = nil
|
26
|
+
@access_token = nil
|
27
|
+
@refresh_token = nil
|
28
|
+
@on_new_token = nil
|
29
|
+
yield(self) if block_given?
|
30
|
+
end
|
31
|
+
|
32
|
+
def user_agent
|
33
|
+
@user_agent ||= "AdashRubyGem/#{Adash::VERSION}/#{RUBY_DESCRIPTION}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def deregistrate_device
|
37
|
+
headers = {
|
38
|
+
'x-amzn-accept-type': 'com.amazon.dash.replenishment.DrsDeregisterResult@1.0',
|
39
|
+
'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsDeregisterInput@1.0'
|
40
|
+
}
|
41
|
+
path = "/deviceModels/#{@device_model}/devices/#{@serial}/registration"
|
42
|
+
request_drs(:delete, path, headers: headers)
|
43
|
+
end
|
44
|
+
|
45
|
+
def device_status(most_recently_active_date)
|
46
|
+
headers = {
|
47
|
+
'x-amzn-accept-type': 'com.amazon.dash.replenishment.DrsDeviceStatusResult@1.0',
|
48
|
+
'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsDeviceStatusInput@1.0'
|
49
|
+
}
|
50
|
+
path = '/deviceStatus'
|
51
|
+
request_drs(:post, path, headers: headers, params: { 'mostRecentlyActiveDate' => convert_to_iso8601(most_recently_active_date) })
|
52
|
+
end
|
53
|
+
|
54
|
+
def subscription_info
|
55
|
+
headers = {
|
56
|
+
'x-amzn-accept-type': 'com.amazon.dash.replenishment.DrsSubscriptionInfoResult@1.0',
|
57
|
+
'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsSubscriptionInfoInput@1.0'
|
58
|
+
}
|
59
|
+
path = '/subscriptionInfo'
|
60
|
+
request_drs(:get, path, headers: headers)
|
61
|
+
end
|
62
|
+
|
63
|
+
def slot_status(slot_id, expected_replenishment_date, remaining_quantity_in_unit, original_quantity_in_unit, total_quantity_on_hand, last_use_date)
|
64
|
+
headers = {
|
65
|
+
'x-amzn-accept-type': 'com.amazon.dash.replenishment.DrsSlotStatusResult@1.0',
|
66
|
+
'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsSlotStatusInput@1.0'
|
67
|
+
}
|
68
|
+
path = "/slotStatus/#{slot_id}"
|
69
|
+
params = {
|
70
|
+
'expectedReplenishmentDate' => convert_to_iso8601(expected_replenishment_date),
|
71
|
+
'remainingQuantityInUnit' => remaining_quantity_in_unit,
|
72
|
+
'originalQuantityInUnit' => original_quantity_in_unit,
|
73
|
+
'totalQuantityOnHand' => total_quantity_on_hand,
|
74
|
+
'lastUseDate' => convert_to_iso8601(last_use_date)
|
75
|
+
}
|
76
|
+
request_drs(:post, path, headers: headers, params: params)
|
77
|
+
end
|
78
|
+
|
79
|
+
def replenish(slot_id)
|
80
|
+
headers = {
|
81
|
+
'x-amzn-accept-type': 'com.amazon.dash.replenishment.DrsReplenishResult@1.0',
|
82
|
+
'x-amzn-type-version': 'com.amazon.dash.replenishment.DrsReplenishInput@1.0'
|
83
|
+
}
|
84
|
+
path = "/replenish/#{slot_id}"
|
85
|
+
response = request_drs(:post, path, headers: headers)
|
86
|
+
# TODO: imprement response processing
|
87
|
+
# https://developer.amazon.com/public/solutions/devices/dash-replenishment-service/docs/dash-replenish-endpoint
|
88
|
+
# response examples:
|
89
|
+
# {"message":"Unable to place order - slot : <01234567-89ab-cdef-0123-456789abcdef> is unsubscribed."}
|
90
|
+
# {"detailCode":"STANDARD_ORDER_PLACED","eventInstanceId":"amzn1.dash.v2.o.xxxxxx......"}
|
91
|
+
# {"detailCode":"ORDER_INPROGRESS","eventInstanceId":"amzn1.dash.v2.o.xxxxxx......"}
|
92
|
+
end
|
93
|
+
|
94
|
+
def get_token
|
95
|
+
if @access_token
|
96
|
+
@access_token
|
97
|
+
else
|
98
|
+
resp = request_token
|
99
|
+
process_token_response(resp)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def convert_to_iso8601(input)
|
106
|
+
case input
|
107
|
+
when Date, Time
|
108
|
+
input.iso8601
|
109
|
+
when String
|
110
|
+
input
|
111
|
+
else
|
112
|
+
input.to_s
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def process_token_response(resp)
|
117
|
+
if resp.json['error']
|
118
|
+
puts resp.json['error']
|
119
|
+
puts resp.json['error_description']
|
120
|
+
nil
|
121
|
+
else
|
122
|
+
@access_token = resp.json['access_token']
|
123
|
+
@refresh_token = resp.json['refresh_token']
|
124
|
+
@on_new_token.call(@access_token, @refresh_token) if @on_new_token
|
125
|
+
@access_token
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def request_drs(method, path, headers: {}, params: {})
|
130
|
+
url = "https://#{@drs_host}#{path}"
|
131
|
+
if @authorization_code.nil?
|
132
|
+
raise 'Authorization Code is not set'
|
133
|
+
end
|
134
|
+
if @access_token.nil?
|
135
|
+
token = get_token
|
136
|
+
if token.nil?
|
137
|
+
raise 'Failed to get token'
|
138
|
+
end
|
139
|
+
end
|
140
|
+
resp = request(method, url, headers: headers, params: params)
|
141
|
+
if resp.code == '400' && resp.json['message'] == 'Invalid token' && @refresh_token
|
142
|
+
resp = refresh_access_token
|
143
|
+
process_token_response(resp)
|
144
|
+
request(method, url, headers: headers, params: params)
|
145
|
+
else
|
146
|
+
resp
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def refresh_access_token
|
151
|
+
params = {
|
152
|
+
grant_type: 'refresh_token',
|
153
|
+
refresh_token: @refresh_token,
|
154
|
+
client_id: Adash::Config.client_id,
|
155
|
+
client_secret: Adash::Config.client_secret
|
156
|
+
}
|
157
|
+
@access_token = nil
|
158
|
+
request(:post, "https://#{@amazon_host}/auth/o2/token", params: params)
|
159
|
+
end
|
160
|
+
|
161
|
+
def request_token
|
162
|
+
params = {
|
163
|
+
grant_type: 'authorization_code',
|
164
|
+
code: @authorization_code,
|
165
|
+
client_id: Adash::Config.client_id,
|
166
|
+
client_secret: Adash::Config.client_secret,
|
167
|
+
redirect_uri: "http://localhost:#{Adash::Config.redirect_port}/"
|
168
|
+
}
|
169
|
+
request(:post, "https://#{@amazon_host}/auth/o2/token", params: params)
|
170
|
+
end
|
171
|
+
|
172
|
+
def request(method, url, headers: {}, params: {})
|
173
|
+
uri = URI.parse(url)
|
174
|
+
if params.any?{ |key, value| value.is_a?(Enumerable) }
|
175
|
+
converted_params = []
|
176
|
+
params.each do |key, value|
|
177
|
+
if value.is_a?(Enumerable)
|
178
|
+
value.each_index do |i|
|
179
|
+
converted_params << ["#{key}[#{i}]", value[i]]
|
180
|
+
end
|
181
|
+
else
|
182
|
+
converted_params << [key, value]
|
183
|
+
end
|
184
|
+
end
|
185
|
+
params = converted_params
|
186
|
+
end
|
187
|
+
case method
|
188
|
+
when :delete
|
189
|
+
uri.query = URI.encode_www_form(params)
|
190
|
+
request = Net::HTTP::Delete.new(uri)
|
191
|
+
when :get
|
192
|
+
uri.query = URI.encode_www_form(params)
|
193
|
+
request = Net::HTTP::Get.new(uri)
|
194
|
+
when :post
|
195
|
+
request = Net::HTTP::Post.new(uri.path)
|
196
|
+
request.body = URI.encode_www_form(params)
|
197
|
+
end
|
198
|
+
request['Content-Type'] = 'application/x-www-form-urlencoded'
|
199
|
+
request['User-Agent'] = @user_agent
|
200
|
+
headers.each do |key, value|
|
201
|
+
request[key] = value
|
202
|
+
end
|
203
|
+
if not @access_token.nil?
|
204
|
+
request["Authorization"] = "Bearer #{@access_token}"
|
205
|
+
end
|
206
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
207
|
+
http.use_ssl = true
|
208
|
+
response = http.request(request)
|
209
|
+
response.json = JSON.parse(response.body)
|
210
|
+
response
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: amazon-drs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Code Ass
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '12.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '12.0'
|
41
|
+
description: |-
|
42
|
+
amazon-drs is for Amazon Dash Replenishment Service.
|
43
|
+
You can use this after authorized by Login with Amazon.
|
44
|
+
email:
|
45
|
+
- aycabta@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- amazon-drs.gemspec
|
57
|
+
- bin/console
|
58
|
+
- bin/setup
|
59
|
+
- data/client
|
60
|
+
- lib/amazon-drs.rb
|
61
|
+
- lib/amazon-drs/client.rb
|
62
|
+
- lib/amazon-drs/version.rb
|
63
|
+
homepage: https://github.com/aycabta/amazon-drs
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.5.2
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: amazon-drs is for Amazon Dash Replenishment Service
|
87
|
+
test_files: []
|