Module: Dawn

Defined in:
lib/dawn/api/app.rb,
lib/dawn/api/key.rb,
lib/dawn/api/gear.rb,
lib/dawn/api/drain.rb,
lib/dawn/api/hosts.rb,
lib/dawn/api/login.rb,
lib/dawn/api/domain.rb,
lib/dawn/api/app/env.rb,
lib/dawn/api/version.rb,
lib/dawn/api/base_api.rb,
lib/dawn/api/app/gears.rb,
lib/dawn/api/app/drains.rb,
lib/dawn/api/app/domains.rb,
lib/dawn/api/authenticate.rb

Defined Under Namespace

Modules: API, BaseApi Classes: App, AuthenticationError, Domain, Drain, Gear, Key, User

Constant Summary collapse

HEADERS =
{
  'Accept'                => 'application/json',
  'Content-Type'          => 'application/json',
  'Accept-Encoding'       => 'gzip',
  'User-Agent'            => "dawn/#{API::VERSION}",
  'X-Ruby-Version'        => RUBY_VERSION,
  'X-Ruby-Platform'       => RUBY_PLATFORM
}
OPTIONS =
{
  headers:  {},
  host:     'api.dawn.dev',
  nonblock: false,
  scheme:   'http'
}

Class Method Summary collapse

Class Method Details

.authenticate(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dawn/api/authenticate.rb', line 31

def self.authenticate(options={})
  options = OPTIONS.merge options
  options[:headers] = options[:headers].merge(HEADERS)
  @api_key = options.delete(:api_key) || ENV['DAWN_API_KEY']

  if !@api_key
    hostname = options[:host]
    url = "#{options[:scheme]}://#{hostname}"

    if options.key?(:username) && options.key?(:password)
      username = options.delete(:username)
      password = options.delete(:password)

      @connection = Excon.new url, headers: HEADERS

      @api_key = User.(username: username, password: password)['api_key']

      netrc = Netrc.read
      netrc[hostname] = username, @api_key
      netrc.save
    else
      netrc = Netrc.read
      username, api_key = netrc[hostname]
      if api_key
        @api_key = api_key
      else
        raise AuthenticationError, "api_key was not found, try dawn login to reset your .netrc"
        # "please provide a DAWN_API_KEY, username and password, or update your .netrc with the dawn details (use dawn login)"
      end
    end
  end
  @headers = HEADERS.merge 'Authorization' => "Token token=\"#{@api_key}\""
  @connection = Excon.new "#{options[:scheme]}://#{options[:host]}", headers: @headers
end

.git_hostObject



3
4
5
# File 'lib/dawn/api/hosts.rb', line 3

def self.git_host
  "dawn.dev"
end

.log_hostObject



7
8
9
# File 'lib/dawn/api/hosts.rb', line 7

def self.log_host
  "dawn.dev:8001"
end

.request(options) ⇒ Object



26
27
28
29
# File 'lib/dawn/api/authenticate.rb', line 26

def self.request(options)
  Dawn.authenticate unless @connection
  @connection.request(options)
end