Class: Parse::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/http_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ HttpClient

Returns a new instance of HttpClient.



4
5
6
# File 'lib/parse/http_client.rb', line 4

def initialize host
  @host = host
end

Instance Method Details

#request(method, endpoint, headers = {}, body = nil, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/parse/http_client.rb', line 8

def request method, endpoint, headers={}, body=nil, &block
  req = eval("Net::HTTP::#{method.to_s.capitalize}").new endpoint, headers
  req.body = body if body
  client = Net::HTTP.new @host, 443
  #client.set_debug_output $stderr
  client.use_ssl = true
  client.start do
    resp = client.request req
    resp_body = JSON.parse resp.body
    raise StandardError.new "error calling #{endpoint}: #{
      resp_body['error']}" if resp_body.has_key? 'error'
    block.call resp_body if block
  end
end