Class: Heroku::API

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku/api.rb,
lib/heroku/api/apps.rb,
lib/heroku/api/keys.rb,
lib/heroku/api/logs.rb,
lib/heroku/api/mock.rb,
lib/heroku/api/user.rb,
lib/heroku/api/login.rb,
lib/heroku/api/addons.rb,
lib/heroku/api/errors.rb,
lib/heroku/api/stacks.rb,
lib/heroku/api/domains.rb,
lib/heroku/api/version.rb,
lib/heroku/api/features.rb,
lib/heroku/api/releases.rb,
lib/heroku/api/mock/apps.rb,
lib/heroku/api/mock/keys.rb,
lib/heroku/api/mock/logs.rb,
lib/heroku/api/mock/user.rb,
lib/heroku/api/processes.rb,
lib/heroku/api/mock/login.rb,
lib/heroku/api/attachments.rb,
lib/heroku/api/config_vars.rb,
lib/heroku/api/mock/addons.rb,
lib/heroku/api/mock/stacks.rb,
lib/heroku/api/mock/domains.rb,
lib/heroku/api/collaborators.rb,
lib/heroku/api/mock/features.rb,
lib/heroku/api/mock/releases.rb,
lib/heroku/api/ssl_endpoints.rb,
lib/heroku/api/vendor/okjson.rb,
lib/heroku/api/mock/processes.rb,
lib/heroku/api/mock/attachments.rb,
lib/heroku/api/mock/config_vars.rb,
lib/heroku/api/mock/collaborators.rb

Defined Under Namespace

Modules: Errors, Mock, OkJson

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/heroku/api.rb', line 55

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

  @api_key = options.delete(:api_key) || ENV['HEROKU_API_KEY']
  if !@api_key && options.has_key?(:username) && options.has_key?(:password)
    @connection = Excon.new("#{options[:scheme]}://#{options[:host]}", options.merge(:headers => HEADERS))
    @api_key = self.(options[:username], options[: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_addon(app, addon) ⇒ Object

DELETE /apps/:app/addons/:addon



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

def delete_addon(app, addon)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{escape app}/addons/#{addon}"
  )
end

#delete_app(app) ⇒ Object

DELETE /apps/:app



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

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

#delete_collaborator(app, email) ⇒ Object

DELETE /apps/:app/collaborators/:email



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

def delete_collaborator(app, email)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{escape app}/collaborators/#{email}"
  )
end

#delete_config_var(app, key) ⇒ Object

DELETE /apps/:app/config_vars/:key



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

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

#delete_domain(app, domain) ⇒ Object

DELETE /apps/:app/domains/:domain



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

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

#delete_domains(app) ⇒ Object

DELETE /apps/:app/domains



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

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

#delete_feature(feature, app = nil) ⇒ Object

DELETE /features/:feature



5
6
7
8
9
10
11
12
# File 'lib/heroku/api/features.rb', line 5

def delete_feature(feature, app = nil)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/features/#{feature}",
    :query    => { 'app' => app }
  )
end

#delete_key(key) ⇒ Object

DELETE /user/keys/:key



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

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

#delete_keysObject

DELETE /user/keys



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

def delete_keys
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/user/keys"
  )
end

#delete_ssl_endpoint(app, cname) ⇒ Object

DELETE /apps/:app/ssl-endpoint/:cname



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

def delete_ssl_endpoint(app, cname)
  request(
    :expects  => 200,
    :method   => :delete,
    :path     => "/apps/#{escape app}/ssl-endpoints/#{escape(cname)}"
  )
end

#get_addons(app = nil) ⇒ Object

GET /addons GET /apps/:app/addons



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/heroku/api/addons.rb', line 15

def get_addons(app=nil)
  path = if app
    "/apps/#{escape app}/addons"
  else
    "/addons"
  end
  request(
    :expects  => 200,
    :method   => :get,
    :path     => path
  )
end

#get_app(app) ⇒ Object

GET /apps/:app



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

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

#get_app_maintenance(app) ⇒ Object

GET /apps/:app/server/maintenance



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

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

#get_appsObject

GET /apps



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

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

#get_attachments(app) ⇒ Object

GET /apps/:app/attachments



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

def get_attachments(app)
  request(
    :expects => 200,
    :method => :get,
    :path => "/apps/#{escape app}/attachments"
  )
end

#get_collaborators(app) ⇒ Object

GET /apps/:app/collaborators



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

def get_collaborators(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{escape app}/collaborators"
  )
end

#get_config_vars(app) ⇒ Object

GET /apps/:app/config_vars



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

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

#get_domains(app) ⇒ Object

GET /apps/:app/domains



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

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

#get_feature(feature, app = nil) ⇒ Object

GET /features/:feature



25
26
27
28
29
30
31
32
# File 'lib/heroku/api/features.rb', line 25

def get_feature(feature, app = nil)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/features/#{feature}",
    :query    => { 'app' => app }
  )
end

#get_features(app = nil) ⇒ Object

GET /features



15
16
17
18
19
20
21
22
# File 'lib/heroku/api/features.rb', line 15

def get_features(app = nil)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/features",
    :query    => { 'app' => app }
  )
end

#get_keysObject

GET /user/keys



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

def get_keys
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/user/keys"
  )
end

#get_logs(app, options = {}) ⇒ Object

GET /apps/:app/logs



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/heroku/api/logs.rb', line 5

def get_logs(app, options = {})
  options = {
    'logplex' => 'true'
  }.merge(options)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{escape app}/logs",
    :query    => options
  )
end

#get_ps(app) ⇒ Object

GET /apps/:app/ps



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

def get_ps(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{escape app}/ps"
  )
end

#get_release(app, release) ⇒ Object

GET /apps/:app/releases/:release



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

def get_release(app, release)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{escape app}/releases/#{release}"
  )
end

#get_releases(app) ⇒ Object

GET /apps/:app/releases



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

def get_releases(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{escape app}/releases"
  )
end

#get_ssl_endpoint(app, cname) ⇒ Object

GET /apps/:app/ssl-endpoint/:cname



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

def get_ssl_endpoint(app, cname)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{escape app}/ssl-endpoints/#{escape(cname)}"
  )
end

#get_ssl_endpoints(app) ⇒ Object

GET /apps/:app/ssl-endpoints



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

def get_ssl_endpoints(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{escape app}/ssl-endpoints"
  )
end

#get_stack(app) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/heroku/api/stacks.rb', line 4

def get_stack(app)
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/apps/#{escape app}/stack"
  )
end

#get_userObject

GET /user



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

def get_user
  request(
    :expects  => 200,
    :method   => :get,
    :path     => "/user"
  )
end

#post_addon(app, addon, config = {}) ⇒ Object

POST /apps/:app/addons/:addon



29
30
31
32
33
34
35
36
# File 'lib/heroku/api/addons.rb', line 29

def post_addon(app, addon, config = {})
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{escape app}/addons/#{addon}",
    :query    => addon_params(config)
  )
end

#post_app(params = {}) ⇒ Object

POST /apps



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

def post_app(params={})
  request(
    :expects  => 202,
    :method   => :post,
    :path     => '/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/heroku/api/apps.rb', line 51

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

#post_collaborator(app, email) ⇒ Object

POST /apps/:app/collaborators



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

def post_collaborator(app, email)
  request(
    :expects  => [200, 201],
    :method   => :post,
    :path     => "/apps/#{escape app}/collaborators",
    :query    => {'collaborator[email]' => email}
  )
end

#post_domain(app, domain) ⇒ Object

POST /apps/:app/domains



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

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

#post_feature(feature, app = nil) ⇒ Object

POST /features/:feature



35
36
37
38
39
40
41
42
# File 'lib/heroku/api/features.rb', line 35

def post_feature(feature, app = nil)
  request(
    :expects  => [200, 201],
    :method   => :post,
    :path     => "/features/#{feature}",
    :query    => { 'app' => app }
  )
end

#post_key(key) ⇒ Object

POST /user/keys



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

def post_key(key)
  request(
    :body     => key,
    :expects  => 200,
    :method   => :post,
    :path     => "/user/keys"
  )
end

#post_login(username, password) ⇒ Object



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

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

#post_ps(app, command, options = {}) ⇒ Object

POST /apps/:app/ps



14
15
16
17
18
19
20
21
22
# File 'lib/heroku/api/processes.rb', line 14

def post_ps(app, command, options={})
  options = { 'command' => command }.merge(options)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{escape app}/ps",
    :query    => ps_options(options)
  )
end

#post_ps_restart(app, options = {}) ⇒ Object

POST /apps/:app/ps/restart



25
26
27
28
29
30
31
32
# File 'lib/heroku/api/processes.rb', line 25

def post_ps_restart(app, options={})
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{escape app}/ps/restart",
    :query    => options
  )
end

#post_ps_scale(app, type, quantity) ⇒ Object

POST /apps/:app/ps/scale



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/heroku/api/processes.rb', line 35

def post_ps_scale(app, type, quantity)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{escape app}/ps/scale",
    :query    => {
      'type'  => type,
      'qty'   => quantity
    }
  )
end

#post_ps_stop(app, options) ⇒ Object

POST /apps/:app/ps/stop



48
49
50
51
52
53
54
55
# File 'lib/heroku/api/processes.rb', line 48

def post_ps_stop(app, options)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{escape app}/ps/stop",
    :query    => options
  )
end

#post_release(app, release = nil) ⇒ Object

POST /apps/:app/releases/:release



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

def post_release(app, release=nil)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{escape app}/releases",
    :query    => {'rollback' => release}
  )
end

#post_ssl_endpoint(app, pem, key) ⇒ Object

POST /apps/:app/ssl-endpoints



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

def post_ssl_endpoint(app, pem, key)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{escape app}/ssl-endpoints",
    :query     => { 'key' => key, 'pem' => pem }
  )
end

#post_ssl_endpoint_rollback(app, cname) ⇒ Object

POST /apps/:app/ssl-endpoints/:cname/rollback



42
43
44
45
46
47
48
# File 'lib/heroku/api/ssl_endpoints.rb', line 42

def post_ssl_endpoint_rollback(app, cname)
  request(
    :expects  => 200,
    :method   => :post,
    :path     => "/apps/#{escape app}/ssl-endpoints/#{escape(cname)}/rollback"
  )
end

#put_addon(app, addon, config = {}) ⇒ Object

PUT /apps/:app/addons/:addon



39
40
41
42
43
44
45
46
# File 'lib/heroku/api/addons.rb', line 39

def put_addon(app, addon, config = {})
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{escape app}/addons/#{addon}",
    :query    => addon_params(config)
  )
end

#put_app(app, params) ⇒ Object

PUT /apps/:app



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

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

#put_config_vars(app, vars) ⇒ Object

PUT /apps/:app/config_vars



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

def put_config_vars(app, vars)
  request(
    :body     => Heroku::API::OkJson.encode(vars),
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{escape app}/config_vars"
  )
end

#put_dynos(app, dynos) ⇒ Object

PUT /apps/:app/dynos



58
59
60
61
62
63
64
65
# File 'lib/heroku/api/processes.rb', line 58

def put_dynos(app, dynos)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{escape app}/dynos",
    :query    => {'dynos' => dynos}
  )
end

#put_ssl_endpoint(app, cname, pem, key) ⇒ Object

PUT /apps/:app/ssl-endpoints/:cname



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

def put_ssl_endpoint(app, cname, pem, key)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{escape app}/ssl-endpoints/#{escape(cname)}",
    :query     => { 'key' => key, 'pem' => pem }
  )
end

#put_stack(app, stack) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/heroku/api/stacks.rb', line 12

def put_stack(app, stack)
  request(
    :body     => stack,
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{escape app}/stack"
  )
end

#put_workers(app, workers) ⇒ Object

PUT /apps/:app/workers



68
69
70
71
72
73
74
75
# File 'lib/heroku/api/processes.rb', line 68

def put_workers(app, workers)
  request(
    :expects  => 200,
    :method   => :put,
    :path     => "/apps/#{escape app}/workers",
    :query    => {'workers' => workers}
  )
end

#request(params, &block) ⇒ Object



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
106
107
108
109
110
111
112
113
114
# File 'lib/heroku/api.rb', line 72

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

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

  if response.body && !response.body.empty?
    if response.headers['Content-Encoding'] == 'gzip'
      response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
    end
    begin
      response.body = Heroku::API::OkJson.decode(response.body)
    rescue
      # leave non-JSON body as is
    end
  end

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

  response
end