Module: Heroku::Client::Http

Included in:
Api
Defined in:
lib/heroku-client/http.rb

Instance Method Summary collapse

Instance Method Details

#delete(uri) ⇒ Object



16
17
18
# File 'lib/heroku-client/http.rb', line 16

def delete(uri)
  request :delete, uri
end

#get(uri) ⇒ Object



4
5
6
# File 'lib/heroku-client/http.rb', line 4

def get(uri)
  request :get, uri
end

#post(uri, body) ⇒ Object



8
9
10
# File 'lib/heroku-client/http.rb', line 8

def post(uri, body)
  request :post, uri, body
end

#put(uri, body) ⇒ Object



12
13
14
# File 'lib/heroku-client/http.rb', line 12

def put(uri, body)
  request :put, uri, body
end

#request(method, uri, body = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/heroku-client/http.rb', line 20

def request(method, uri, body = {})
  headers = { "Accept" => "application/json"}
  basic_auth = { password: apikey }

  options = { basic_auth: basic_auth, headers: headers }

  options[:body] = body unless body.empty?

  url = "https://api.heroku.com/#{uri}"

  resp = HTTParty.get url, options if method == :get
  resp = HTTParty.post url, options if method == :post
  resp = HTTParty.put url, options if method == :put
  resp = HTTParty.delete url, options if method == :delete

  unless resp.success?
    error_message = json_util.parse_error(resp.body)

    raise UnauthorizedError, error_message if resp.code == 401
    raise PaymentRequiredError, error_message if resp.code == 402
    raise ForbiddenError, error_message if resp.code == 403
    raise NotFoundError, error_message if resp.code == 404
    raise PreConditionFailedError, error_message if resp.code == 412
    raise UnprocessableEntityError, error_message if resp.code == 422
    raise LockedError, error_message if resp.code == 423

    raise UnknownError
  end

  json_util.parse_response resp.body
end