class DeployGate::BrowserLogin

Constants

CREDENTIAL_URL
DEFAULT_PORT
LOGIN_URL
NOTIFY_URL

Public Class Methods

new(port = nil) click to toggle source

@param [Fixnum] port

# File lib/deploygate/browser_login.rb, line 9
def initialize(port = nil)
  @port = port || DEFAULT_PORT
  @login_uri = URI(LOGIN_URL)
  @login_uri.query = {port: @port, client: 'dg'}.to_query

  @credential_uri = URI(CREDENTIAL_URL)
  @notify_uri = URI(NOTIFY_URL)
end

Public Instance Methods

start() click to toggle source
# File lib/deploygate/browser_login.rb, line 18
def start
  server = WEBrick::HTTPServer.new(
      :Port => @port,
      :BindAddress =>"localhost",
      :Logger => WEBrick::Log.new(STDOUT, 0),
      :AccessLog => []
  )

  begin
    Signal.trap("INT") { server.shutdown }

    server.mount_proc '/' do |req, res|
      res.status = WEBrick::HTTPStatus::RC_NO_CONTENT

      cancel = req.query['cancel']
      notify_key = req.query['key']

      unless cancel
        credential = get_credential(notify_key)
        DeployGate::Session.save(credential['name'], credential['token'])
        notify_finish(notify_key)

        DeployGate::Commands::Login.login_success()
      end

      server.stop
    end

    Launchy.open(@login_uri.to_s)
    server.start
  ensure
    server.shutdown
  end
end

Private Instance Methods

get_credential(notify_key) click to toggle source

@param [String] notify_key @return [Hash]

# File lib/deploygate/browser_login.rb, line 57
def get_credential(notify_key)
  res = HTTPClient.new(:agent_name => "dg/#{DeployGate::VERSION}").get(@credential_uri.to_s, {key: notify_key})
  JSON.parse(res.body)
end
notify_finish(notify_key) click to toggle source

@param [String] notify_key

# File lib/deploygate/browser_login.rb, line 63
def notify_finish(notify_key)
  notify_post(notify_key, 'credential_saved')
  notify_post(notify_key, 'finished')
end
notify_post(notify_key, action) click to toggle source
# File lib/deploygate/browser_login.rb, line 68
def notify_post(notify_key, action)
  HTTPClient.new(:agent_name => "dg/#{DeployGate::VERSION}").post(@notify_uri.to_s, {key: notify_key, command_action: action})
end