class Adash::WaitIndefinitely
Attributes
redirect_uri[R]
Public Class Methods
new(device_model, serial, is_test: false)
click to toggle source
# File lib/adash/wait_indefinitely.rb, line 10 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 @start_box = Queue.new @start_cv = ConditionVariable.new @start_mutex = Mutex.new @server = WEBrick::HTTPServer.new({ :BindAddress => '127.0.0.1', :Port => Adash::Config.redirect_port, :StartCallback => proc { @start_mutex.synchronize { @start_box.push(true) @start_cv.signal } } }) @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(amazon_authorization_url(@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
Public Instance Methods
get_code()
click to toggle source
# File lib/adash/wait_indefinitely.rb, line 52 def get_code t = Thread.new do @code_mutex.synchronize { @start_mutex.synchronize { while @start_box.size == 0 @start_cv.wait(@start_mutex) end 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
shutdown()
click to toggle source
# File lib/adash/wait_indefinitely.rb, line 72 def shutdown @server.shutdown end
Private Instance Methods
render(content)
click to toggle source
# File lib/adash/wait_indefinitely.rb, line 76 def render(content) <<~EOH <html> <head> <meta name="referrer" content="no-referrer" /> </head> <body> #{content} </body> </html> EOH end