Module: Heroku::Kensa::HTTPForChecks

Included in:
DeprovisionCheck, DuplicateProvisionCheck, PlanChangeCheck, SsoCheck
Defined in:
lib/heroku/kensa/http.rb

Instance Method Summary collapse

Instance Method Details

#delete(credentials, path, payload = nil) ⇒ Object



20
21
22
# File 'lib/heroku/kensa/http.rb', line 20

def delete(credentials, path, payload=nil)
  request(:delete, credentials, path, payload)
end

#get(path, params = {}) ⇒ Object



7
8
9
10
# File 'lib/heroku/kensa/http.rb', line 7

def get(path, params={})
  path = "#{path}?" + params.map { |k, v| "#{k}=#{v}" }.join("&") unless params.empty?
  request(:get, [], path)
end

#post(credentials, path, payload = nil) ⇒ Object



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

def post(credentials, path, payload=nil)
  request(:post, credentials, path, payload)
end

#put(credentials, path, payload = nil) ⇒ Object



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

def put(credentials, path, payload=nil)
  request(:put, credentials, path, payload)
end

#request(meth, credentials, path, payload = nil) ⇒ Object



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
51
52
53
54
# File 'lib/heroku/kensa/http.rb', line 24

def request(meth, credentials, path, payload=nil)
  code = nil
  body = nil

  begin
    args = [
      { :accept => "application/json" }
    ]

    if payload
      args.first[:content_type] = "application/json"
      args.unshift OkJson.encode(payload)
    end

    user, pass = credentials
    body = RestClient::Resource.new(url, user: user, password: pass, verify_ssl: false)[path].send(
      meth,
      *args
    ).to_s

    code = 200
  rescue RestClient::ExceptionWithResponse => boom
    code = boom.http_code
    body = boom.http_body
  rescue Errno::ECONNREFUSED
    code = -1
    body = nil
  end

  [code, body]
end