adash 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 396638a8ef38f6b9167e149fa1fc627fccac9e61
4
- data.tar.gz: a643cde5a6649f266ada88ebdc7f4f9530d75569
3
+ metadata.gz: 349b18112b944763dbcf5ba78c67a39a800a56f1
4
+ data.tar.gz: d83c1b8f1b127287f96012a98f8c3cc1ef66c2e1
5
5
  SHA512:
6
- metadata.gz: a4b4451e6cc62c73845cb62d694eb282bdc4f5d16a83e68a89cfa6e6fdffad16e4bc25f1b6aad19d87d7901d162dad57d4421d63f30722bd964e4203c42cc8e0
7
- data.tar.gz: 8c54f4d2e1640c0da553f6aa381051a224dc572e25b61da71d42368cf66fdba54e97a1de3075d6dbee5fe04ba777ec0f52b33945e239a41ed6a9981dd00fb099
6
+ metadata.gz: f43f1d1a311a55a1fd8bcdfebf3137b555801e7a5174376db3e40039f4b17ed26855d92cce1e35556f8e36bc8c58a5e841ae502462fae5745b1103151fcf5195
7
+ data.tar.gz: 67506f20e45900253a26bf85dd1634c158b97cb7fafa1a602c42061071af175e01787fc3133c4fce8615bae7fca4a6fb22a4db8bddf2bffd750ab762035a44e2
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'launchy'
4
+
3
5
  # Specify your gem's dependencies in adash.gemspec
4
6
  gemspec
@@ -21,6 +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 "amazon-drs"
25
- spec.add_development_dependency "launchy", "~> 2.5"
24
+ spec.add_dependency "amazon-drs"
25
+ spec.add_dependency "launchy"
26
26
  end
data/exe/adash CHANGED
@@ -3,130 +3,15 @@
3
3
  $: << File.expand_path("../../lib/", __FILE__)
4
4
 
5
5
  require 'adash'
6
- require 'adash/config'
7
- require 'adash/wait_indefinitely'
8
6
  require 'optparse'
9
- require 'readline'
10
- require 'fileutils'
11
- require 'yaml'
12
- require 'amazon-drs'
13
7
 
14
- opt = OptionParser.new
15
-
16
- opt.on('-h', '--help') { puts 'help' }
17
-
18
- def generate_serial(device_model)
19
- orig = [('a'..'z'), ('A'..'Z'), ('0'..'9')].map { |i| i.to_a }.flatten
20
- random_suffix = (0...16).map { orig[rand(orig.size)] }.join
21
- "#{device_model}_#{Time.now.to_i}_#{random_suffix}"
22
- end
23
-
24
- def get_credentials
25
- if File.exist?(Adash::Config.credentials_path)
26
- credentials = YAML.load_file(Adash::Config.credentials_path)
27
- else
28
- { 'authorized_devices' => [] }
29
- end
30
- end
31
-
32
- def save_credentials(credentials)
33
- open(Adash::Config.credentials_path, 'w') do |f|
34
- f.write(credentials.to_yaml)
35
- end
36
- end
37
-
38
- def get_device_from_credentials(credentials, device_model)
39
- i = credentials['authorized_devices'].find_index { |d| d['device_model'] == device_model }
40
- if i
41
- credentials['authorized_devices'][i]
42
- else
43
- nil
44
- end
45
- end
46
-
47
- def save_credentials_with_device(credentials, device)
48
- i = credentials['authorized_devices'].find_index { |d| d['device_model'] == device['device_model'] }
49
- if i
50
- credentials['authorized_devices'][i] = device
51
- else
52
- credentials['authorized_devices'] << device
53
- end
54
- save_credentials(credentials)
55
- end
56
-
57
- def save_credentials_without_device_model(device_model)
58
- credentials = get_credentials
59
- credentials['authorized_devices'] = credentials['authorized_devices'].delete_if { |d| d['device_model'] == device_model }
60
- save_credentials(credentials)
61
- end
62
-
63
- def create_client_from_device(device)
64
- AmazonDrs::Client.new(device['device_model']) do |c|
65
- c.authorization_code = device['authorization_code']
66
- c.serial = device['serial']
67
- c.redirect_uri = device['redirect_uri']
68
- c.access_token = device['access_token']
69
- c.refresh_token = device['refresh_token']
70
- c.client_id = Adash::Config.client_id
71
- c.client_secret = Adash::Config.client_secret
72
- c.redirect_uri = "http://localhost:#{Adash::Config.redirect_port}/"
73
- c.on_new_token = proc { |access_token, refresh_token|
74
- credentials = get_credentials
75
- updated_device = get_device_from_credentials(credentials, device['device_model'])
76
- updated_device['access_token'] = access_token
77
- updated_device['refresh_token'] = refresh_token
78
- save_credentials_with_device(credentials, updated_device)
79
- device['device_model']
80
- }
81
- end
82
- end
83
-
84
- def get_device_by_name(name)
85
- credentials = get_credentials
86
- hit = credentials['authorized_devices'].find_index { |d| d['name'] == name }
87
- if hit
88
- device = credentials['authorized_devices'][hit]
89
- else
90
- nil
91
- end
92
- end
93
-
94
- def show_slots(slots)
95
- index = 0
96
- slots.each do |slot_id, available|
97
- puts
98
- puts "---- number: #{index}"
99
- puts "* slot_id: #{slot_id}"
100
- puts " available: #{available}"
101
- puts
102
- index =+ 1
103
- end
104
- end
105
-
106
- def select_slot_prompt(client)
107
- resp = client.subscription_info
108
- slots = resp.json['slotsSubscriptionStatus'].select{ |k, v| v }
109
- if slots.size == 1
110
- return slots.keys.first
111
- end
112
- loop do
113
- show_slots(slots)
114
- slot_num = Readline.readline('Select slot number> ')
115
- if (0..(slots.size - 1)).member?(slot_num.to_i)
116
- break slots.keys[slot_num.to_i]
117
- else
118
- puts "ERROR: #{slot_num} is out of Range"
119
- end
120
- end
121
- end
122
-
123
- argv = opt.order(ARGV)
124
- unless argv.empty?
125
- subcmd = argv.shift
8
+ def run_sub_command(adash, subcmd, argv)
126
9
  case subcmd
127
10
  when 'init'
128
11
  init_opt = OptionParser.new
129
- init_opt.on('-h', '--help') { puts 'init help' }
12
+ init_opt.on('-h', '--help') {
13
+ puts 'init help'
14
+ }
130
15
  is_test = false
131
16
  init_opt.on('-t', '--test') {
132
17
  is_test = true
@@ -135,108 +20,53 @@ unless argv.empty?
135
20
  argv = init_opt.parse(argv)
136
21
  if argv.size < 2
137
22
  puts 'Usage: adash init name device_model'
138
- exit 2
139
- end
140
- name = argv.shift
141
- device_model = argv.shift
142
- serial = generate_serial(device_model)
143
- credentials = get_credentials
144
- authorized_devices = credentials['authorized_devices']
145
- hit = authorized_devices.find_index { |d| d['device_model'] == device_model }
146
- if hit
147
- puts "Adash knows device what is #{device_model}."
148
- exit 3
149
- end
150
- hit = authorized_devices.find_index { |d| d['name'] == name }
151
- if hit
152
- puts "Adash knows device what is named #{name}."
153
- exit 3
23
+ 1
24
+ else
25
+ name = argv.shift
26
+ device_model = argv.shift
27
+ adash.sub_init(name, device_model, is_test)
154
28
  end
155
- wi = Adash::WaitIndefinitely.new(device_model, serial)
156
- Signal.trap(:INT){ wi.shutdown }
157
- code = wi.get_code
158
- FileUtils.mkdir_p(File.expand_path('..', Adash::Config.credentials_path))
159
- new_device = {
160
- 'name' => name,
161
- 'device_model' => device_model,
162
- 'serial' => serial,
163
- 'authorization_code' => code,
164
- 'redirect_uri' => wi.redirect_uri
165
- }
166
- new_device['is_test'] = true if is_test
167
- credentials['authorized_devices'] << new_device
168
- save_credentials(credentials)
169
- client = create_client_from_device(new_device)
170
- client.get_token
171
29
  when 'list'
172
- credentials = get_credentials
173
- credentials['authorized_devices'].each do |device|
174
- puts "---- name: #{device['name']}"
175
- puts "* device_model: #{device['device_model']}"
176
- puts " serial: #{device['serial']}"
177
- puts ' THIS DEVICE IS TEST PURCHASE MODE' if device['is_test']
178
- end
30
+ adash.sub_list
179
31
  when 'deregistrate'
180
32
  if argv.size < 1
181
33
  puts 'Usage: adash deregistrate name'
182
- exit 4
183
- end
184
- name = argv.shift
185
- device = get_device_by_name(name)
186
- unless device
187
- puts "Device #{name} not found"
188
- exit 5
34
+ 1
35
+ else
36
+ name = argv.shift
37
+ adash.sub_deregistrate(name)
189
38
  end
190
- client = create_client_from_device(device)
191
- resp = client.deregistrate_device
192
- save_credentials_without_device_model(device['device_model'])
193
39
  when 'list-slot'
194
40
  if argv.size < 1
195
41
  puts 'Usage: adash list-slot name'
196
- exit 5
197
- end
198
- name = argv.shift
199
- device = get_device_by_name(name)
200
- unless device
201
- puts "Device #{name} not found"
202
- exit 5
203
- end
204
- client = create_client_from_device(device)
205
- resp = client.subscription_info
206
- index = 0
207
- resp.json['slotsSubscriptionStatus'].each do |slot_id, available|
208
- puts "---- #{index}"
209
- puts "* slot_id: #{slot_id}"
210
- puts " available: #{available}"
211
- index =+ 1
42
+ 1
43
+ else
44
+ name = argv.shift
45
+ adash.sub_list_slot(name)
212
46
  end
213
47
  when 'replenish'
214
48
  if argv.size < 1
215
49
  puts 'Usage: adash replenish name [slot_id]'
216
- exit 6
217
- end
218
- name = argv.shift
219
- slot_id = argv.first&.start_with?('-') ? nil : argv.shift
220
- device = get_device_by_name(name)
221
- unless device
222
- puts "Device #{name} not found"
223
- exit 5
224
- end
225
- client = create_client_from_device(device)
226
- slot_id = select_slot_prompt(client) unless slot_id
227
- resp = client.replenish(slot_id)
228
- if resp.json['message']
229
- puts "ERROR: #{resp.json['message']}"
50
+ 1
230
51
  else
231
- case resp.json['detailCode']
232
- when 'STANDARD_ORDER_PLACED'
233
- puts 'Succeeded to order.'
234
- when 'ORDER_INPROGRESS'
235
- puts 'The order is in progress.'
236
- end
52
+ name = argv.shift
53
+ slot_id = argv.first&.start_with?('-') ? nil : argv.shift
54
+ adash.sub_replenish(name, slot_id)
237
55
  end
238
56
  else
239
57
  $stderr.puts "no such subcommand: #{k}"
240
- exit 1
58
+ 2
241
59
  end
242
60
  end
61
+
62
+ opt = OptionParser.new
63
+ opt.on('-h', '--help') { puts 'help' }
64
+
65
+ adash = Adash::Access.new
66
+
67
+ argv = opt.order(ARGV)
68
+ unless argv.empty?
69
+ subcmd = argv.shift
70
+ ret = run_sub_command(adash, subcmd, argv)
71
+ exit ret if ret != 0
72
+ end
@@ -1,5 +1,226 @@
1
1
  require 'adash/version'
2
+ require 'adash/config'
3
+ require 'adash/wait_indefinitely'
4
+ require 'readline'
5
+ require 'fileutils'
6
+ require 'yaml'
7
+ require 'amazon-drs'
2
8
 
9
+ # TODO: Use each class for return
3
10
  module Adash
4
- # Your code goes here...
11
+ class Access
12
+ def initialize
13
+ end
14
+
15
+ def sub_init(name, device_model, is_test)
16
+ serial = generate_serial(device_model)
17
+ credentials = get_credentials
18
+ authorized_devices = credentials['authorized_devices']
19
+ hit = authorized_devices.find_index { |d| d['device_model'] == device_model }
20
+ if hit
21
+ puts "Adash knows device what is #{device_model}."
22
+ return 3
23
+ end
24
+ hit = authorized_devices.find_index { |d| d['name'] == name }
25
+ if hit
26
+ puts "Adash knows device what is named #{name}."
27
+ return 4
28
+ end
29
+ wi = Adash::WaitIndefinitely.new(device_model, serial)
30
+ Signal.trap(:INT){ wi.shutdown }
31
+ code = wi.get_code
32
+ FileUtils.mkdir_p(File.expand_path('..', Adash::Config.credentials_path))
33
+ new_device = {
34
+ 'name' => name,
35
+ 'device_model' => device_model,
36
+ 'serial' => serial,
37
+ 'authorization_code' => code,
38
+ 'redirect_uri' => wi.redirect_uri
39
+ }
40
+ new_device['is_test'] = true if is_test
41
+ credentials['authorized_devices'] << new_device
42
+ save_credentials(credentials)
43
+ client = create_client_from_device(new_device)
44
+ client.get_token
45
+ 0
46
+ end
47
+
48
+ def sub_list
49
+ credentials = get_credentials
50
+ credentials['authorized_devices'].each do |device|
51
+ puts "---- name: #{device['name']}"
52
+ puts "* device_model: #{device['device_model']}"
53
+ puts " serial: #{device['serial']}"
54
+ puts ' THIS DEVICE IS TEST PURCHASE MODE' if device['is_test']
55
+ end
56
+ 0
57
+ end
58
+
59
+ def sub_deregistrate(name)
60
+ device = get_device_by_name(name)
61
+ unless device
62
+ puts "Device #{name} not found"
63
+ return 5
64
+ end
65
+ client = create_client_from_device(device)
66
+ resp = client.deregistrate_device
67
+ save_credentials_without_device_model(device['device_model'])
68
+ 0
69
+ end
70
+
71
+ def sub_list_slot(name)
72
+ device = get_device_by_name(name)
73
+ unless device
74
+ puts "Device #{name} not found"
75
+ return 5
76
+ end
77
+ client = create_client_from_device(device)
78
+ resp = client.subscription_info
79
+ index = 0
80
+ resp.json['slotsSubscriptionStatus'].each do |slot_id, available|
81
+ puts "---- #{index}"
82
+ puts "* slot_id: #{slot_id}"
83
+ puts " available: #{available}"
84
+ index =+ 1
85
+ end
86
+ 0
87
+ end
88
+
89
+ def sub_replenish(name, slot_id)
90
+ device = get_device_by_name(name)
91
+ unless device
92
+ puts "Device #{name} not found"
93
+ return 5
94
+ end
95
+ client = create_client_from_device(device)
96
+ slot_id = select_slot_prompt(client) unless slot_id
97
+ resp = client.replenish(slot_id)
98
+ if resp.json['message']
99
+ puts "ERROR: #{resp.json['message']}"
100
+ else
101
+ case resp.json['detailCode']
102
+ when 'STANDARD_ORDER_PLACED'
103
+ puts 'Succeeded to order.'
104
+ when 'ORDER_INPROGRESS'
105
+ puts 'The order is in progress.'
106
+ end
107
+ end
108
+ 0
109
+ end
110
+
111
+ def generate_serial(device_model)
112
+ orig = [('a'..'z'), ('A'..'Z'), ('0'..'9')].map { |i| i.to_a }.flatten
113
+ random_suffix = (0...16).map { orig[rand(orig.size)] }.join
114
+ "#{device_model}_#{Time.now.to_i}_#{random_suffix}"
115
+ end
116
+ private :generate_serial
117
+
118
+ def get_credentials
119
+ if File.exist?(Adash::Config.credentials_path)
120
+ credentials = YAML.load_file(Adash::Config.credentials_path)
121
+ else
122
+ { 'authorized_devices' => [] }
123
+ end
124
+ end
125
+ private :get_credentials
126
+
127
+ def save_credentials(credentials)
128
+ open(Adash::Config.credentials_path, 'w') do |f|
129
+ f.write(credentials.to_yaml)
130
+ end
131
+ end
132
+ private :save_credentials
133
+
134
+ def get_device_from_credentials(credentials, device_model)
135
+ i = credentials['authorized_devices'].find_index { |d| d['device_model'] == device_model }
136
+ if i
137
+ credentials['authorized_devices'][i]
138
+ else
139
+ nil
140
+ end
141
+ end
142
+ private :get_device_from_credentials
143
+
144
+ def save_credentials_with_device(credentials, device)
145
+ i = credentials['authorized_devices'].find_index { |d| d['device_model'] == device['device_model'] }
146
+ if i
147
+ credentials['authorized_devices'][i] = device
148
+ else
149
+ credentials['authorized_devices'] << device
150
+ end
151
+ save_credentials(credentials)
152
+ end
153
+ private :save_credentials_with_device
154
+
155
+ def save_credentials_without_device_model(device_model)
156
+ credentials = get_credentials
157
+ credentials['authorized_devices'] = credentials['authorized_devices'].delete_if { |d| d['device_model'] == device_model }
158
+ save_credentials(credentials)
159
+ end
160
+ private :save_credentials_without_device_model
161
+
162
+ def create_client_from_device(device)
163
+ AmazonDrs::Client.new(device['device_model']) do |c|
164
+ c.authorization_code = device['authorization_code']
165
+ c.serial = device['serial']
166
+ c.redirect_uri = device['redirect_uri']
167
+ c.access_token = device['access_token']
168
+ c.refresh_token = device['refresh_token']
169
+ c.client_id = Adash::Config.client_id
170
+ c.client_secret = Adash::Config.client_secret
171
+ c.redirect_uri = "http://localhost:#{Adash::Config.redirect_port}/"
172
+ c.on_new_token = proc { |access_token, refresh_token|
173
+ credentials = get_credentials
174
+ updated_device = get_device_from_credentials(credentials, device['device_model'])
175
+ updated_device['access_token'] = access_token
176
+ updated_device['refresh_token'] = refresh_token
177
+ save_credentials_with_device(credentials, updated_device)
178
+ device['device_model']
179
+ }
180
+ end
181
+ end
182
+ private :create_client_from_device
183
+
184
+ def get_device_by_name(name)
185
+ credentials = get_credentials
186
+ hit = credentials['authorized_devices'].find_index { |d| d['name'] == name }
187
+ if hit
188
+ device = credentials['authorized_devices'][hit]
189
+ else
190
+ nil
191
+ end
192
+ end
193
+ private :get_device_by_name
194
+
195
+ def show_slots(slots)
196
+ index = 0
197
+ slots.each do |slot_id, available|
198
+ puts
199
+ puts "---- number: #{index}"
200
+ puts "* slot_id: #{slot_id}"
201
+ puts " available: #{available}"
202
+ puts
203
+ index =+ 1
204
+ end
205
+ end
206
+ private :show_slots
207
+
208
+ def select_slot_prompt(client)
209
+ resp = client.subscription_info
210
+ slots = resp.json['slotsSubscriptionStatus'].select{ |k, v| v }
211
+ if slots.size == 1
212
+ return slots.keys.first
213
+ end
214
+ loop do
215
+ show_slots(slots)
216
+ slot_num = Readline.readline('Select slot number> ')
217
+ if (0..(slots.size - 1)).member?(slot_num.to_i)
218
+ break slots.keys[slot_num.to_i]
219
+ else
220
+ puts "ERROR: #{slot_num} is out of Range"
221
+ end
222
+ end
223
+ end
224
+ private :select_slot_prompt
225
+ end
5
226
  end
@@ -1,3 +1,3 @@
1
1
  module Adash
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -1,7 +1,6 @@
1
1
  require 'uri'
2
2
  require 'erb'
3
3
  require 'launchy'
4
- require 'adash/client'
5
4
  require 'adash/config'
6
5
 
7
6
  module Adash
@@ -38,6 +37,26 @@ module Adash
38
37
  })
39
38
  end
40
39
 
40
+ def get_code
41
+ t = Thread.new do
42
+ @code_mutex.synchronize {
43
+ # TODO: wait for WEBrick launch
44
+ Launchy.open("http://localhost:#{Adash::Config.redirect_port}/getting_started")
45
+ while @code_box.size == 0
46
+ @code_cv.wait(@code_mutex)
47
+ sleep 1
48
+ @server.shutdown
49
+ end
50
+ }
51
+ end
52
+ @server.start
53
+ @code_box.pop
54
+ end
55
+
56
+ def shutdown
57
+ @server.shutdown
58
+ end
59
+
41
60
  def render(content)
42
61
  <<~EOH
43
62
  <html>
@@ -50,6 +69,7 @@ module Adash
50
69
  </html>
51
70
  EOH
52
71
  end
72
+ private :render
53
73
 
54
74
  def amazon_authorization_url(device_model, serial)
55
75
  base = 'https://www.amazon.com/ap/oa?'
@@ -63,25 +83,6 @@ module Adash
63
83
  }
64
84
  "#{base}#{params.map{ |k, v| "#{k}=#{v}" }.join(?&)}"
65
85
  end
66
-
67
- def get_code
68
- t = Thread.new do
69
- @code_mutex.synchronize {
70
- # TODO: wait for WEBrick launch
71
- Launchy.open("http://localhost:#{Adash::Config.redirect_port}/getting_started")
72
- while @code_box.size == 0
73
- @code_cv.wait(@code_mutex)
74
- sleep 1
75
- @server.shutdown
76
- end
77
- }
78
- end
79
- @server.start
80
- @code_box.pop
81
- end
82
-
83
- def shutdown
84
- @server.shutdown
85
- end
86
+ private :amazon_authorization_url
86
87
  end
87
88
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
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: 2016-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -45,7 +45,7 @@ dependencies:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- type: :development
48
+ type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: launchy
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '2.5'
62
- type: :development
61
+ version: '0'
62
+ type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '2.5'
68
+ version: '0'
69
69
  description: |-
70
70
  Adash is a Dash Replenishment Service CLI client.
71
71
  You will login with OAuth (Login with Amazon) and replenish items.
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  version: '0'
112
112
  requirements: []
113
113
  rubyforge_project:
114
- rubygems_version: 2.5.2
114
+ rubygems_version: 2.6.8
115
115
  signing_key:
116
116
  specification_version: 4
117
117
  summary: Adash is a Dash Replenishment Service CLI client