Class: Hilarity::API

Inherits:
Object
  • Object
show all
Defined in:
lib/hilarity/api.rb,
lib/hilarity/api/apps.rb,
lib/hilarity/api/keys.rb,
lib/hilarity/api/user.rb,
lib/hilarity/api/login.rb,
lib/hilarity/api/errors.rb,
lib/hilarity/api/domains.rb,
lib/hilarity/api/version.rb,
lib/hilarity/api/config_vars.rb

Defined Under Namespace

Modules: Errors

Constant Summary collapse

HEADERS =
{
  'Accept'               => 'application/json',
  'Accept-Encoding'       => 'gzip',
  #'Accept-Language'      => 'en-US, en;q=0.8',
  'User-Agent'           => "hilarity-rb/#{Hilarity::API::VERSION}",
  'X-Ruby-Version'       => RUBY_VERSION,
  'X-Ruby-Platform'       => RUBY_PLATFORM
}
OPTIONS =
{
  :headers => {},
  :host     => 'hilarity.io',
  :nonblock => false,
  :scheme   => 'https'
}
VERSION =
"0.0.1"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hilarity/api.rb', line 45

def initialize(options={})
  options = OPTIONS.merge(options)

  @api_key = options.delete(:api_key) || ENV['HILARITY_API_KEY']
  if !@api_key && options.has_key?(:username) && options.has_key?(:password)
    username = options.delete(:username)
    password = options.delete(:password)
    @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options.merge(:headers => HEADERS))
    @api_key = self.(username, password).body["api_key"]
  end

  user_pass = ":#{@api_key}"
  options[:headers] = HEADERS.merge({
    'Authorization' => "Basic #{Base64.encode64(user_pass).gsub("\n", '')}",
  }).merge(options[:headers])

  @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options)
end

Instance Method Details

#delete_app(app) ⇒ Object

DELETE /apps/:app



5
6
7
8
9
10
11
# File 'lib/hilarity/api/apps.rb', line 5

def delete_app(app)
  request(
    :expects  => 200,
    :method  => :delete,
    :path    => "/api/v1/apps/#{app}"
  )
end

#delete_config_var(app, key) ⇒ Object

DELETE /apps/:app/config/:key



5
6
7
8
9
10
11
# File 'lib/hilarity/api/config_vars.rb', line 5

def delete_config_var(app, key)
  request(
    :expects  => 200,
    :method  => :delete,
    :path    => "/api/v1/apps/#{app}/config/#{escape(key)}"
  )
end

#delete_domain(app, domain) ⇒ Object

DELETE /apps/:app/domains/:domain



5
6
7
8
9
10
11
# File 'lib/hilarity/api/domains.rb', line 5

def delete_domain(app, domain)
  request(
    :expects  => 200,
    :method  => :delete,
    :path    => "/api/v1/apps/#{app}/domains/#{escape(domain)}"
  )
end

#delete_domains(app) ⇒ Object

DELETE /apps/:app/domains



14
15
16
17
18
19
20
# File 'lib/hilarity/api/domains.rb', line 14

def delete_domains(app)
  request(
    :expects  => 200,
    :method  => :delete,
    :path    => "/api/v1/apps/#{app}/domains"
  )
end

#delete_key(key) ⇒ Object

DELETE /ssh_keys/:key



5
6
7
8
9
10
11
# File 'lib/hilarity/api/keys.rb', line 5

def delete_key(key)
  request(
    :expects  => 200,
    :method  => :delete,
    :path    => "/api/v1/ssh_keys/#{escape(key)}"
  )
end

#delete_keysObject

DELETE /ssh_keys



14
15
16
17
18
19
20
# File 'lib/hilarity/api/keys.rb', line 14

def delete_keys
  request(
    :expects  => 200,
    :method  => :delete,
    :path    => "/api/v1/ssh_keys"
  )
end

#get_app(app) ⇒ Object

GET /apps/:app



23
24
25
26
27
28
29
# File 'lib/hilarity/api/apps.rb', line 23

def get_app(app)
  request(
    :expects  => 200,
    :method  => :get,
    :path    => "/api/v1/apps/#{app}"
  )
end

#get_app_maintenance(app) ⇒ Object

GET /apps/:app/server/maintenance



32
33
34
35
36
37
38
# File 'lib/hilarity/api/apps.rb', line 32

def get_app_maintenance(app)
  request(
    :expects  => 200,
    :method  => :get,
    :path    => "/api/v1/apps/#{app}/server/maintenance"
  )
end

#get_appsObject

GET /apps



14
15
16
17
18
19
20
# File 'lib/hilarity/api/apps.rb', line 14

def get_apps
  request(
    :expects  => 200,
    :method  => :get,
    :path    => "/api/v1/apps"
  )
end

#get_config_vars(app) ⇒ Object

GET /apps/:app/config



14
15
16
17
18
19
20
# File 'lib/hilarity/api/config_vars.rb', line 14

def get_config_vars(app)
  request(
    :expects  => 200,
    :method  => :get,
    :path    => "/api/v1/apps/#{app}/config"
  )
end

#get_domains(app) ⇒ Object

GET /apps/:app/domains



23
24
25
26
27
28
29
# File 'lib/hilarity/api/domains.rb', line 23

def get_domains(app)
  request(
    :expects  => 200,
    :method  => :get,
    :path    => "/api/v1/apps/#{app}/domains"
  )
end

#get_keysObject

GET /ssh_keys



23
24
25
26
27
28
29
# File 'lib/hilarity/api/keys.rb', line 23

def get_keys
  request(
    :expects  => 200,
    :method  => :get,
    :path    => "/api/v1/ssh_keys"
  )
end

#get_userObject

GET /user/me



5
6
7
8
9
10
11
# File 'lib/hilarity/api/user.rb', line 5

def get_user
  request(
    :expects  => 200,
    :method  => :get,
    :path    => "/api/v1/users/me"
  )
end

#post_app(params = {}) ⇒ Object

POST /apps



41
42
43
44
45
46
47
48
# File 'lib/hilarity/api/apps.rb', line 41

def post_app(params={})
  request(
    :expects  => 202,
    :method  => :post,
    :path    => '/api/v1/apps',
    :query    => app_params(params)
  )
end

#post_app_maintenance(app, maintenance_mode) ⇒ Object

POST /apps/:app/server/maintenance



51
52
53
54
55
56
57
58
# File 'lib/hilarity/api/apps.rb', line 51

def post_app_maintenance(app, maintenance_mode)
  request(
    :expects  => 200,
    :method  => :post,
    :path    => "/api/v1/apps/#{app}/server/maintenance",
    :query    => {'maintenance_mode' => maintenance_mode}
  )
end

#post_domain(app, domain) ⇒ Object

POST /apps/:app/domains



32
33
34
35
36
37
38
39
# File 'lib/hilarity/api/domains.rb', line 32

def post_domain(app, domain)
  request(
    :expects  => 201,
    :method  => :post,
    :path    => "/api/v1/apps/#{app}/domains",
    :query    => {'domain_name[url]' => domain}
  )
end

#post_key(key) ⇒ Object

POST /ssh_keys



32
33
34
35
36
37
38
39
# File 'lib/hilarity/api/keys.rb', line 32

def post_key(key)
  request(
    :body    => key,
    :expects  => 200,
    :method  => :post,
    :path    => "/api/v1/ssh_keys"
  )
end

#post_login(username, password) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/hilarity/api/login.rb', line 4

def (username, password)
  request(
    :expects  => 200,
    :method  => :post,
    :path    => '/api/v1/users/login',
    :query    => { 'username' => username, 'password' => password }
  )
end

#put_app(app, params) ⇒ Object

PUT /apps/:app



61
62
63
64
65
66
67
68
# File 'lib/hilarity/api/apps.rb', line 61

def put_app(app, params)
  request(
    :expects  => 200,
    :method  => :put,
    :path    => "/api/v1/apps/#{app}",
    :query    => app_params(params)
  )
end

#put_config_vars(app, vars) ⇒ Object

PUT /apps/:app/config



23
24
25
26
27
28
29
30
# File 'lib/hilarity/api/config_vars.rb', line 23

def put_config_vars(app, vars)
  request(
    :body    => MultiJson.dump(vars),
    :expects  => 200,
    :method  => :put,
    :path    => "/api/v1/apps/#{app}/config"
  )
end

#request(params, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hilarity/api.rb', line 64

def request(params, &block)
  begin
    response = @connection.request(params, &block)
  rescue Excon::Errors::HTTPStatusError => error
    klass = case error.response.status
      when 401 then Hilarity::API::Errors::Unauthorized
      when 402 then Hilarity::API::Errors::VerificationRequired
      when 403 then Hilarity::API::Errors::Forbidden
      when 404
        if error.request[:path].match /\/apps\/\/.*/
          Hilarity::API::Errors::NilApp
        else
          Hilarity::API::Errors::NotFound
        end
      when 408 then Hilarity::API::Errors::Timeout
      when 422 then Hilarity::API::Errors::RequestFailed
      when 423 then Hilarity::API::Errors::Locked
      when 429 then Hilarity::API::Errors::RateLimitExceeded
      when /50./ then Hilarity::API::Errors::RequestFailed
      else Hilarity::API::Errors::ErrorWithResponse
    end

    decompress_response!(error.response)
    reerror = klass.new(error.message, error.response)
    reerror.set_backtrace(error.backtrace)
    raise(reerror)
  end

  if response.body && !response.body.empty?
    decompress_response!(response)
    begin
      response.body = MultiJson.load(response.body)
    rescue
      # leave non-JSON body as is
    end 
  end

  # reset (non-persistent) connection
  @connection.reset

  response
end