Class: Parse::HttpClient

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

Direct Known Subclasses

BatchHttpClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ HttpClient

Returns a new instance of HttpClient.



6
7
8
# File 'lib/parse/http_client.rb', line 6

def initialize host
  @host = host
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



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

def host
  @host
end

Instance Method Details

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



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

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 if $DEBUG
  client.use_ssl = true
  client.start do
    # TODO
    resp = client.request req
    resp_body = resp.body.empty? ? nil : JSON.parse(resp.body)
    raise StandardError.new "error calling #{endpoint}: #{
      resp_body['error']}" if resp_body.is_a?(Hash) && resp_body.has_key?('error')
    block.call resp_body if block
  end
end