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

amazon_authorization_url(device_model, serial) click to toggle source
# File lib/adash/wait_indefinitely.rb, line 90
def amazon_authorization_url(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
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