class Heroku::Client

A Ruby class to call the Heroku REST API. You might use this if you want to manage your Heroku apps from within a Ruby program, such as Capistrano.

Example:

require 'heroku'
heroku = Heroku::Client.new('me@example.com', 'mypass')
heroku.create('myapp')

Public Class Methods

auth(user, password, host=Heroku::Auth.host) click to toggle source
# File lib/heroku/client.rb, line 33
def self.auth(user, password, host=Heroku::Auth.host)
  client = new(user, password, host)
  json_decode client.post('/login', { :username => user, :password => password }, :accept => 'json').to_s
end
gem_version_string() click to toggle source
# File lib/heroku/client.rb, line 27
def self.gem_version_string
  "heroku-gem/#{version}"
end
new(user, password, host=Heroku::Auth.host) click to toggle source
# File lib/heroku/client.rb, line 38
def initialize(user, password, host=Heroku::Auth.host)
  @user = user
  @password = password
  @host = host
end
version() click to toggle source
# File lib/heroku/client.rb, line 23
def self.version
  Heroku::VERSION
end

Public Instance Methods

add_collaborator(app_name, email) click to toggle source

Invite a person by email address to collaborate on the app.

# File lib/heroku/client.rb, line 108
def add_collaborator(app_name, email)
  xml(post("/apps/#{app_name}/collaborators", { 'collaborator[email]' => email }).to_s)
end
add_config_vars(app_name, new_vars) click to toggle source
# File lib/heroku/client.rb, line 469
def add_config_vars(app_name, new_vars)
  put("/apps/#{app_name}/config_vars", json_encode(new_vars), :accept => :json).to_s
end
add_domain(app_name, domain) click to toggle source
# File lib/heroku/client.rb, line 132
def add_domain(app_name, domain)
  post("/apps/#{app_name}/domains", domain).to_s
end
add_drain(app_name, url) click to toggle source
# File lib/heroku/client.rb, line 437
def add_drain(app_name, url)
  post("/apps/#{app_name}/logs/drains", "url=#{url}").to_s
end
add_key(key) click to toggle source

Add an ssh public key to the current user.

# File lib/heroku/client.rb, line 166
def add_key(key)
  post("/user/keys", key, { 'Content-Type' => 'text/ssh-authkey' }).to_s
end
add_ssl(app_name, pem, key) click to toggle source
# File lib/heroku/client.rb, line 145
def add_ssl(app_name, pem, key)
  json_decode(post("/apps/#{app_name}/ssl", :pem => pem, :key => key).to_s)
end
addons() click to toggle source
# File lib/heroku/client.rb, line 481
def addons
  json_decode get("/addons", :accept => 'application/json').to_s
end
bounce(app_name, upid) click to toggle source

Bounce a service.

# File lib/heroku/client.rb, line 358
def bounce(app_name, upid)
  service(app_name, upid).bounce
end
clear_config_vars(app_name) click to toggle source
# File lib/heroku/client.rb, line 477
def clear_config_vars(app_name)
  delete("/apps/#{app_name}/config_vars").to_s
end
clear_ssl(app_name) click to toggle source
# File lib/heroku/client.rb, line 153
def clear_ssl(app_name)
  delete("/apps/#{app_name}/ssl")
end
config_vars(app_name) click to toggle source
# File lib/heroku/client.rb, line 465
def config_vars(app_name)
  json_decode get("/apps/#{app_name}/config_vars", :accept => :json).to_s
end
confirm_billing() click to toggle source
# File lib/heroku/client.rb, line 547
def confirm_billing
  post("/user/#{escape(@user)}/confirm_billing").to_s
end
console(app_name, cmd=nil) { |console_session| ... } click to toggle source

Execute a one-off console command, or start a new console tty session if cmd is nil.

# File lib/heroku/client.rb, line 215
  def console(app_name, cmd=nil)
    if block_given?
      id = post("/apps/#{app_name}/consoles").to_s
      yield ConsoleSession.new(id, app_name, self)
      delete("/apps/#{app_name}/consoles/#{id}").to_s
    else
      run_console_command("/apps/#{app_name}/console", cmd)
    end
  rescue RestClient::BadGateway => e
    raise(AppCrashed, "Unable to attach to a dyno to open a console session.
Your application may have crashed.
Check the output of "heroku ps" and "heroku logs" for more information.
")
  end
create(name=nil, options={}) click to toggle source

Create a new app, with an optional name.

# File lib/heroku/client.rb, line 65
def create(name=nil, options={})
  name = create_request(name, options)
  loop do
    break if create_complete?(name)
    sleep 1
  end
  name
end
create_app(name=nil, options={}) click to toggle source
# File lib/heroku/client.rb, line 74
def create_app(name=nil, options={})
  options[:name] = name if name
  json_decode(post("/apps", { :app => options }, :accept => "application/json").to_s)
end
create_complete?(name) click to toggle source
# File lib/heroku/client.rb, line 84
def create_complete?(name)
  put("/apps/#{name}/status", {}).code == 201
end
create_request(name=nil, options={}) click to toggle source
# File lib/heroku/client.rb, line 79
def create_request(name=nil, options={})
  options[:name] = name if name
  xml(post('/apps', :app => options).to_s).elements["//app/name"].text
end
cron_logs(app_name) click to toggle source

Fetch recent cron logs from the app server.

# File lib/heroku/client.rb, line 374
def cron_logs(app_name)
  get("/apps/#{app_name}/cron_logs").to_s
end
database_reset(app_name) click to toggle source
# File lib/heroku/client.rb, line 506
def database_reset(app_name)
  post("/apps/#{app_name}/database/reset", '').to_s
end
database_session(app_name) click to toggle source
# File lib/heroku/client.rb, line 502
def database_session(app_name)
  json_decode(post("/apps/#{app_name}/database/session2", '', :x_taps_version => ::Taps.version).to_s)
end
destroy(name) click to toggle source

Destroy the app permanently.

# File lib/heroku/client.rb, line 95
def destroy(name)
  delete("/apps/#{name}").to_s
end
disable_feature(app, name) click to toggle source
# File lib/heroku/client.rb, line 563
def disable_feature(app, name)
  json_decode delete("/features/#{name}?app=#{app}", :accept => :json).to_s
end
down(app_name, upid) click to toggle source

Bring a service down.

# File lib/heroku/client.rb, line 353
def down(app_name, upid)
  service(app_name, upid).down
end
downgrade_addon(app_name, addon, config={}) click to toggle source
Alias for: upgrade_addon
dynos(app_name) click to toggle source
# File lib/heroku/client.rb, line 445
def dynos(app_name)
  doc = xml(get("/apps/#{app_name}").to_s)
  doc.elements["//app/dynos"].text.to_i
end
enable_feature(app, name) click to toggle source
# File lib/heroku/client.rb, line 559
def enable_feature(app, name)
  json_decode post("/features/#{name}?app=#{app}", :accept => :json).to_s
end
extract_warning(response) click to toggle source
# File lib/heroku/client.rb, line 619
def extract_warning(response)
  return unless response
  if response.headers[:x_heroku_warning] && @warning_callback
    warning = response.headers[:x_heroku_warning]
    @displayed_warnings ||= {}
    unless @displayed_warnings[warning]
      @warning_callback.call(warning)
      @displayed_warnings[warning] = true
    end
  end
end
get_feature(app, name) click to toggle source
# File lib/heroku/client.rb, line 555
def get_feature(app, name)
  json_decode get("features/#{name}?app=#{app}", :accept => :json).to_s
end
httpcache_purge(app_name) click to toggle source
# File lib/heroku/client.rb, line 515
def httpcache_purge(app_name)
  delete("/apps/#{app_name}/httpcache").to_s
end
info(name_or_domain) click to toggle source

Show info such as mode, custom domain, and collaborators on an app.

# File lib/heroku/client.rb, line 55
def info(name_or_domain)
  raise ArgumentError.new("name_or_domain is required for info") unless name_or_domain
  name_or_domain = name_or_domain.gsub(%r^(http:\/\/)?(www\.)?/, '')
  doc = xml(get("/apps/#{name_or_domain}").to_s)
  attrs = hash_from_xml_doc(doc)[:app]
  attrs.merge!(:collaborators => list_collaborators(attrs[:name]))
  attrs.merge!(:addons        => installed_addons(attrs[:name]))
end
install_addon(app_name, addon, config={}) click to toggle source
# File lib/heroku/client.rb, line 489
def install_addon(app_name, addon, config={})
  configure_addon :install, app_name, addon, config
end
installed_addons(app_name) click to toggle source
# File lib/heroku/client.rb, line 485
def installed_addons(app_name)
  json_decode get("/apps/#{app_name}/addons", :accept => 'application/json').to_s
end
keys() click to toggle source

Get the list of ssh public keys for the current user.

# File lib/heroku/client.rb, line 158
def keys
  doc = xml get('/user/keys').to_s
  doc.elements.to_a('//keys/key').map do |key|
    key.elements['contents'].text
  end
end
list() click to toggle source

Show a list of apps which you are a collaborator on.

# File lib/heroku/client.rb, line 45
def list
  doc = xml(get('/apps').to_s)
  doc.elements.to_a("//apps/app").map do |a|
    name = a.elements.to_a("name").first
    owner = a.elements.to_a("owner").first
    [name.text, owner.text]
  end
end
list_collaborators(app_name) click to toggle source

Get a list of collaborators on the app, returns an array of hashes each with :email

# File lib/heroku/client.rb, line 100
def list_collaborators(app_name)
  doc = xml(get("/apps/#{app_name}/collaborators").to_s)
  doc.elements.to_a("//collaborators/collaborator").map do |a|
    { :email => a.elements['email'].text }
  end
end
list_domains(app_name) click to toggle source
# File lib/heroku/client.rb, line 117
def list_domains(app_name)
  doc = xml(get("/apps/#{app_name}/domains").to_s)
  doc.elements.to_a("//domain-names/*").map do |d|
    attrs = { :domain => d.elements['domain'].text }
    if cert = d.elements['cert']
      attrs[:cert] = {
        :expires_at => Time.parse(cert.elements['expires-at'].text),
        :subject    => cert.elements['subject'].text,
        :issuer     => cert.elements['issuer'].text,
      }
    end
    attrs
  end
end
list_drains(app_name) click to toggle source
# File lib/heroku/client.rb, line 433
def list_drains(app_name)
  get("/apps/#{app_name}/logs/drains").to_s
end
list_features(app) click to toggle source
# File lib/heroku/client.rb, line 551
def list_features(app)
  json_decode(get("features?app=#{app}", :accept => :json).to_s)
end
list_stacks(app_name, options={}) click to toggle source

Get a list of stacks available to the app, with the current one marked.

# File lib/heroku/client.rb, line 181
def list_stacks(app_name, options={})
  include_deprecated = options.delete(:include_deprecated) || false

  json_decode get("/apps/#{app_name}/stack",
    :params => { :include_deprecated => include_deprecated },
    :accept => 'application/json'
  ).to_s
end
logs(app_name) click to toggle source

Fetch recent logs from the app server.

# File lib/heroku/client.rb, line 369
def logs(app_name)
  get("/apps/#{app_name}/logs").to_s
end
maintenance(app_name, mode) click to toggle source
# File lib/heroku/client.rb, line 510
def maintenance(app_name, mode)
  mode = mode == :on ? '1' : '0'
  post("/apps/#{app_name}/server/maintenance", :maintenance_mode => mode).to_s
end
migrate_to_stack(app_name, stack) click to toggle source

Request a stack migration.

# File lib/heroku/client.rb, line 191
def migrate_to_stack(app_name, stack)
  put("/apps/#{app_name}/stack", stack, :accept => 'text/plain').to_s
end
on_warning(&blk) click to toggle source
# File lib/heroku/client.rb, line 567
def on_warning(&blk)
  @warning_callback = blk
end
process(method, uri, extra_headers={}, payload=nil) click to toggle source
# File lib/heroku/client.rb, line 599
def process(method, uri, extra_headers={}, payload=nil)
  headers  = heroku_headers.merge(extra_headers)
  args     = [method, payload, headers].compact

  resource_options = default_resource_options_for_uri(uri)

  begin
    response = resource(uri, resource_options).send(*args)
  rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, SocketError
    host = URI.parse(realize_full_uri(uri)).host
    error "Unable to connect to #{host}"
  rescue RestClient::SSLCertificateNotVerified => ex
    host = URI.parse(realize_full_uri(uri)).host
    error "WARNING: Unable to verify SSL certificate for #{host}\nTo disable SSL verification, run with HEROKU_SSL_VERIFY=disable"
  end

  extract_warning(response)
  response
end
ps(app_name) click to toggle source

Retreive ps list for the given app name.

# File lib/heroku/client.rb, line 332
def ps(app_name)
  json_decode get("/apps/#{app_name}/ps", :accept => 'application/json').to_s
end
ps_restart(app, opts={}) click to toggle source
# File lib/heroku/client.rb, line 539
def ps_restart(app, opts={})
  post("/apps/#{app}/ps/restart", opts)
end
ps_run(app, opts={}) click to toggle source
# File lib/heroku/client.rb, line 531
def ps_run(app, opts={})
  json_decode post("/apps/#{app}/ps", opts, :accept => :json).to_s
end
ps_scale(app, opts={}) click to toggle source
# File lib/heroku/client.rb, line 535
def ps_scale(app, opts={})
  Integer(post("/apps/#{app}/ps/scale", opts).to_s)
end
ps_stop(app, opts={}) click to toggle source
# File lib/heroku/client.rb, line 543
def ps_stop(app, opts={})
  post("/apps/#{app}/ps/stop", opts)
end
rake(app_name, cmd) click to toggle source

Run a rake command on the Heroku app and return all output as a string.

# File lib/heroku/client.rb, line 199
def rake(app_name, cmd)
  start(app_name, "rake #{cmd}", :attached).to_s
end
read_logs(app_name, options=[]) { |chunk| ... } click to toggle source
# File lib/heroku/client.rb, line 378
def read_logs(app_name, options=[])
  query = "&" + options.join("&") unless options.empty?
  url = get("/apps/#{app_name}/logs?logplex=true#{query}").to_s
  if url == 'Use old logs'
    puts get("/apps/#{app_name}/logs").to_s
  else
    uri  = URI.parse(url);

    if uri.scheme == 'https'
      proxy = https_proxy
    else
      proxy = http_proxy
    end

    if proxy
      proxy_uri = URI.parse(proxy)
      http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
    else
      http = Net::HTTP.new(uri.host, uri.port)
    end

    if uri.scheme == 'https'
      http.use_ssl = true
      if ENV["HEROKU_SSL_VERIFY"] == "disable"
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      else
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
        http.ca_file = local_ca_file
        http.verify_callback = lambda do |preverify_ok, ssl_context|
          if (!preverify_ok) || ssl_context.error != 0
            error "WARNING: Unable to verify SSL certificate for #{host}\nTo disable SSL verification, run with HEROKU_SSL_VERIFY=disable"
          end
          true
        end
      end
    end

    http.read_timeout = 60 * 60 * 24

    begin
      http.start do
        http.request_get(uri.path + (uri.query ? "?" + uri.query : "")) do |request|
          request.read_body do |chunk|
            yield chunk
          end
        end
      end
    rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT, SocketError
      error("Could not connect to logging service")
    rescue Timeout::Error, EOFError
      error("\nRequest timed out")
    end
  end
end
release(app, release) click to toggle source
# File lib/heroku/client.rb, line 523
def release(app, release)
  json_decode get("/apps/#{app}/releases/#{release}", :accept => :json).to_s
end
releases(app) click to toggle source
# File lib/heroku/client.rb, line 519
def releases(app)
  json_decode get("/apps/#{app}/releases", :accept => :json).to_s
end
remove_all_keys() click to toggle source

Clear all keys on the current user.

# File lib/heroku/client.rb, line 176
def remove_all_keys
  delete("/user/keys").to_s
end
remove_collaborator(app_name, email) click to toggle source

Remove a collaborator.

# File lib/heroku/client.rb, line 113
def remove_collaborator(app_name, email)
  delete("/apps/#{app_name}/collaborators/#{escape(email)}").to_s
end
remove_config_var(app_name, key) click to toggle source
# File lib/heroku/client.rb, line 473
def remove_config_var(app_name, key)
  delete("/apps/#{app_name}/config_vars/#{escape(key)}", :accept => :json).to_s
end
remove_domain(app_name, domain) click to toggle source
# File lib/heroku/client.rb, line 136
def remove_domain(app_name, domain)
  raise ArgumentError.new("invalid domain: #{domain.inspect}") if domain.to_s.strip == ""
  delete("/apps/#{app_name}/domains/#{domain}").to_s
end
remove_domains(app_name) click to toggle source
# File lib/heroku/client.rb, line 141
def remove_domains(app_name)
  delete("/apps/#{app_name}/domains").to_s
end
remove_drain(app_name, url) click to toggle source
# File lib/heroku/client.rb, line 441
def remove_drain(app_name, url)
  delete("/apps/#{app_name}/logs/drains?url=#{URI.escape(url)}").to_s
end
remove_key(key) click to toggle source

Remove an existing ssh public key from the current user.

# File lib/heroku/client.rb, line 171
def remove_key(key)
  delete("/user/keys/#{escape(key)}").to_s
end
remove_ssl(app_name, domain) click to toggle source
# File lib/heroku/client.rb, line 149
def remove_ssl(app_name, domain)
  delete("/apps/#{app_name}/domains/#{domain}/ssl").to_s
end
resource(uri, options={}) click to toggle source
# File lib/heroku/client.rb, line 573
def resource(uri, options={})
  RestClient.proxy = case URI.parse(realize_full_uri(uri)).scheme
  when "http"
    http_proxy
  when "https"
    https_proxy
  end
  RestClient::Resource.new(realize_full_uri(uri), options.merge(:user => user, :password => password))
end
restart(app_name) click to toggle source

Restart the app servers.

# File lib/heroku/client.rb, line 364
def restart(app_name)
  delete("/apps/#{app_name}/server").to_s
end
rollback(app, release=nil) click to toggle source
# File lib/heroku/client.rb, line 527
def rollback(app, release=nil)
  post("/apps/#{app}/releases", :rollback => release)
end
run_console_command(url, command, prefix=nil) click to toggle source

internal method to run console commands formatting the output

# File lib/heroku/client.rb, line 232
def run_console_command(url, command, prefix=nil)
  output = post(url, { :command => command }, :accept => "text/plain").to_s
  return output unless prefix
  if output.include?("\n")
    lines  = output.split("\n")
    (lines[0..-2] << "#{prefix}#{lines.last}").join("\n")
  else
    prefix + output
  end
rescue RestClient::RequestFailed => e
  if e.http_code == 422
    Heroku::Command.extract_error(e.http_body, :raw => true)
  else
    raise e
  end
end
service(app_name, upid) click to toggle source

Get a Service instance to execute commands against.

# File lib/heroku/client.rb, line 343
def service(app_name, upid)
  Service.new(self, app_name, upid)
end
set_dynos(app_name, qty) click to toggle source

Scales the web processes.

# File lib/heroku/client.rb, line 456
def set_dynos(app_name, qty)
  put("/apps/#{app_name}/dynos", :dynos => qty).to_s
end
set_workers(app_name, qty) click to toggle source

Scales the background processes.

# File lib/heroku/client.rb, line 461
def set_workers(app_name, qty)
  put("/apps/#{app_name}/workers", :workers => qty).to_s
end
start(app_name, command, attached=false) click to toggle source

Run a service. If Responds to each and yields output as it's received.

# File lib/heroku/client.rb, line 337
def start(app_name, command, attached=false)
  service = Service.new(self, app_name)
  service.start(command, attached)
end
uninstall_addon(app_name, addon, options={}) click to toggle source
# File lib/heroku/client.rb, line 498
def uninstall_addon(app_name, addon, options={})
  configure_addon :uninstall, app_name, addon, options
end
up(app_name, upid) click to toggle source

Bring a service up.

# File lib/heroku/client.rb, line 348
def up(app_name, upid)
  service(app_name, upid).up
end
update(name, attributes) click to toggle source

Update an app. Available attributes:

:name => rename the app (changes http and git urls)
# File lib/heroku/client.rb, line 90
def update(name, attributes)
  put("/apps/#{name}", :app => attributes).to_s
end
upgrade_addon(app_name, addon, config={}) click to toggle source
# File lib/heroku/client.rb, line 493
def upgrade_addon(app_name, addon, config={})
  configure_addon :upgrade, app_name, addon, config
end
Also aliased as: downgrade_addon
workers(app_name) click to toggle source
# File lib/heroku/client.rb, line 450
def workers(app_name)
  doc = xml(get("/apps/#{app_name}").to_s)
  doc.elements["//app/workers"].text.to_i
end