Class: DeployGate::BrowserLogin

Inherits:
Object
  • Object
show all
Defined in:
lib/deploygate/browser_login.rb

Constant Summary collapse

DEFAULT_PORT =
64126
LOGIN_URL =
"#{DeployGate::API::V1::Base::BASE_URL}/cli/login"
CREDENTIAL_URL =
"#{DeployGate::API::V1::Base::BASE_URL}/cli/credential"
NOTIFY_URL =
"#{DeployGate::API::V1::Base::BASE_URL}/cli/notify"

Instance Method Summary collapse

Constructor Details

#initialize(port = nil) ⇒ BrowserLogin

Returns a new instance of BrowserLogin.

Parameters:

  • port (Fixnum) (defaults to: nil)


9
10
11
12
13
14
15
16
# 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

Instance Method Details

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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.()
      end

      server.stop
    end

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