class Heroku::Client::Organizations

Public Class Methods

add_headers(headers) click to toggle source
# File lib/heroku/client/organizations.rb, line 21
def add_headers(headers)
  @headers.merge! headers
end
add_member(org, member, role) click to toggle source
# File lib/heroku/client/organizations.rb, line 198
def add_member(org, member, role)
  api.request(
    :expects => [201, 302],
    :method => :post,
    :path => "/v1/organization/#{org}/user",
    :body => Heroku::Helpers.json_encode( { "email" => member, "role" => role } ),
    :headers => {"Content-Type" => "application/json"}
  )
end
api(options = {}) click to toggle source
# File lib/heroku/client/organizations.rb, line 9
def api options = {}
  @api ||= begin
    require("excon")
    key = Heroku::Auth.get_credentials[1]
    auth = "Basic #{Base64.encode64(':' + key).gsub("\n", '')}"
    hdrs = headers.merge( {"Authorization" => auth } )
    @connection = Excon.new(manager_url, options.merge(:headers => hdrs))
  end

  self
end
delete_collaborator(org, app, user) click to toggle source
# File lib/heroku/client/organizations.rb, line 148
def delete_collaborator(org, app, user)
  api.request(
    :expects => 200,
    :method => :delete,
    :path => "v1/organization/#{org}/app/#{app}/collaborators/#{user}"
  )
end
get_apps(org) click to toggle source

Apps

# File lib/heroku/client/organizations.rb, line 108
def get_apps(org)
  api.request(
    :expects => 200,
    :method => :get,
    :path => "/v1/organization/#{org}/app"
  )
end
get_members(org) click to toggle source

Members

# File lib/heroku/client/organizations.rb, line 190
def get_members(org)
  api.request(
    :expects => 200,
    :method => :get,
    :path => "/v1/organization/#{org}/user"
  )
end
get_orgs() click to toggle source

Orgs

# File lib/heroku/client/organizations.rb, line 74
def get_orgs
  begin
    api.request(
      :expects => 200,
      :path => "/v1/user/info",
      :method => :get
    )

  # user is not a member of any organization
  rescue Heroku::API::Errors::NotFound
    Excon::Response.new(:body => { 'user' => {:not_found => true} })
  end
end
headers() click to toggle source
# File lib/heroku/client/organizations.rb, line 25
def headers
  @headers
end
join_app(app) click to toggle source
# File lib/heroku/client/organizations.rb, line 156
def join_app(app)
  api.request(
    :expects => 200,
    :method => :post,
    :path => "/v1/app/#{app}/join"
  )
end
leave_app(app) click to toggle source
# File lib/heroku/client/organizations.rb, line 164
def leave_app(app)
  api.request(
    :expects => 204,
    :method => :delete,
    :path => "/v1/app/#{app}/join"
  )
end
lock_app(app) click to toggle source
# File lib/heroku/client/organizations.rb, line 172
def lock_app(app)
  api.request(
    :expects => 200,
    :method => :post,
    :path => "/v1/app/#{app}/lock"
  )
end
post_app(params, org) click to toggle source
# File lib/heroku/client/organizations.rb, line 116
def post_app(params, org)
  params["app_name"] = params.delete("name") if params["name"]

  api.request(
    :expects => 201,
    :method => :post,
    :path => "/v1/organization/#{org}/create-app",
    :body => Heroku::Helpers.json_encode(params),
    :headers => {"Content-Type" => "application/json"}
  )
end
post_collaborator(org, app, user) click to toggle source
# File lib/heroku/client/organizations.rb, line 138
def post_collaborator(org, app, user)
  api.request(
    :expects => 200,
    :method => :post,
    :path => "v1/organization/#{org}/app/#{app}/collaborators",
    :body => Heroku::Helpers.json_encode({ "email" => user }),
    :headers => {"Content-Type" => "application/json"}
  )
end
remove_default_org() click to toggle source
# File lib/heroku/client/organizations.rb, line 88
def remove_default_org
  api.request(
    :expects => 204,
    :method => :delete,
    :path => "/v1/user/default-organization"
  )
end
remove_member(org, member) click to toggle source
# File lib/heroku/client/organizations.rb, line 218
def remove_member(org, member)
  api.request(
    :expects => 204,
    :method => :delete,
    :path => "/v1/organization/#{org}/user/#{CGI.escape(member)}"
  )
end
request(params) click to toggle source
# File lib/heroku/client/organizations.rb, line 29
def request params
  begin
    response = @connection.request(params)
  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

    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 = Heroku::OkJson.decode(response.body)
    rescue
      # leave non-JSON body as is
    end
  end

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

  response
end
set_default_org(org) click to toggle source
# File lib/heroku/client/organizations.rb, line 96
def set_default_org(org)
  api.request(
    :expects => 200,
    :method => :post,
    :path => "/v1/user/default-organization",
    :body => Heroku::Helpers.json_encode( { "default_organization" => org } ),
    :headers => {"Content-Type" => "application/json"}
  )
end
set_member(org, member, role) click to toggle source
# File lib/heroku/client/organizations.rb, line 208
def set_member(org, member, role)
  api.request(
    :expects => [200, 304],
    :method => :put,
    :path => "/v1/organization/#{org}/user/#{CGI.escape(member)}",
    :body => Heroku::Helpers.json_encode( { "role" => role } ),
    :headers => {"Content-Type" => "application/json"}
  )
end
transfer_app(to_org, app, locked) click to toggle source
# File lib/heroku/client/organizations.rb, line 128
def transfer_app(to_org, app, locked)
  api.request(
    :expects => 200,
    :method => :put,
    :path => "/v1/app/#{app}",
    :body => Heroku::Helpers.json_encode( { "owner" => to_org, "locked" => locked || 'false' } ),
    :headers => {"Content-Type" => "application/json"}
  )
end
unlock_app(app) click to toggle source
# File lib/heroku/client/organizations.rb, line 180
def unlock_app(app)
  api.request(
    :expects => 204,
    :method => :delete,
    :path => "/v1/app/#{app}/lock"
  )
end

Private Class Methods

decompress_response!(response) click to toggle source
# File lib/heroku/client/organizations.rb, line 228
def decompress_response!(response)
  return unless response.headers['Content-Encoding'] == 'gzip'
  response.body = Zlib::GzipReader.new(StringIO.new(response.body)).read
end
manager_url() click to toggle source
# File lib/heroku/client/organizations.rb, line 233
def manager_url
  ENV['HEROKU_MANAGER_URL'] || "https://manager-api.heroku.com"
end