module Heroku::Kensa::HTTP

Public Instance Methods

delete(credentials, path, payload=nil) click to toggle source
# File lib/heroku/kensa/http.rb, line 20
def delete(credentials, path, payload=nil)
  request(:delete, credentials, path, payload)
end
get(path, params={}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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, pass)[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