Class: Adash::WaitIndefinitely
- Inherits:
-
Object
- Object
- Adash::WaitIndefinitely
- Defined in:
- lib/adash/wait_indefinitely.rb
Instance Attribute Summary collapse
-
#redirect_uri ⇒ Object
readonly
Returns the value of attribute redirect_uri.
Instance Method Summary collapse
- #amazon_authorization_url(device_model, serial) ⇒ Object
- #get_code ⇒ Object
-
#initialize(device_model, serial, is_test: false) ⇒ WaitIndefinitely
constructor
A new instance of WaitIndefinitely.
- #render(content) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(device_model, serial, is_test: false) ⇒ WaitIndefinitely
Returns a new instance of WaitIndefinitely.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/adash/wait_indefinitely.rb', line 11 def initialize(device_model, serial, is_test: false) require 'webrick' @device_model = device_model @serial = serial @is_test = is_test @redirect_uri = "http://localhost:#{Adash::Config.redirect_port}/" @code_box = Queue.new @code_cv = ConditionVariable.new @code_mutex = Mutex.new @server = WEBrick::HTTPServer.new({ :BindAddress => '127.0.0.1', :Port => Adash::Config.redirect_port }) @server.mount_proc('/getting_started', proc { |req, res| res.content_type = 'text/html' content = %Q`<p>Please go to <a href="#{ERB::Util.html_escape((@device_model, @serial))}">initial tour</a>.</p>` res.body = render(content) }) @server.mount_proc('/', proc { |req, res| res.content_type = 'text/html' if req.query.include?('code') content = '<p>Done. Please close this tab.</p>' @code_mutex.synchronize { @code_box.push(req.query['code'].to_s) @code_cv.signal } else content = "<dl>\n" + req.query.map { |k, v| "<dt>#{k}</dt><dd>#{v}</dd>" }.join("\n") + "\n</dl>" end res.body = render(content) }) end |
Instance Attribute Details
#redirect_uri ⇒ Object (readonly)
Returns the value of attribute redirect_uri.
9 10 11 |
# File 'lib/adash/wait_indefinitely.rb', line 9 def redirect_uri @redirect_uri end |
Instance Method Details
#amazon_authorization_url(device_model, serial) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/adash/wait_indefinitely.rb', line 54 def (device_model, serial) base = 'https://www.amazon.com/ap/oa?' params = { client_id: Adash::Config.client_id, scope: 'dash:replenish', response_type: 'code', # redirect_uri must exact-match with escaped it when access_token is requested redirect_uri: URI.encode_www_form_component(@redirect_uri), scope_data: %Q`{"dash:replenish":{"device_model":"#{device_model}","serial":"#{serial}"#{ ',"is_test_device":true' if @is_test }}}` } "#{base}#{params.map{ |k, v| "#{k}=#{v}" }.join(?&)}" end |
#get_code ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/adash/wait_indefinitely.rb', line 67 def get_code t = Thread.new do @code_mutex.synchronize { # TODO: wait for WEBrick launch Launchy.open("http://localhost:#{Adash::Config.redirect_port}/getting_started") while @code_box.size == 0 @code_cv.wait(@code_mutex) sleep 1 @server.shutdown end } end @server.start @code_box.pop end |
#render(content) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/adash/wait_indefinitely.rb', line 41 def render(content) <<~EOH <html> <head> <meta name="referrer" content="no-referrer" /> </head> <body> #{content} </body> </html> EOH end |
#shutdown ⇒ Object
83 84 85 |
# File 'lib/adash/wait_indefinitely.rb', line 83 def shutdown @server.shutdown end |