Module: Tipjoy::Requestor::ClassMethods

Defined in:
lib/tipjoy/tipjoy_requestor.rb

Instance Method Summary collapse

Instance Method Details

#tipjoy_http_request(method, url, data = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/tipjoy/tipjoy_requestor.rb', line 15

def tipjoy_http_request(method, url, data={})
  if method == :post
    Net::HTTP.post_form URI.parse(url), data
  else
    Net::HTTP.get_response(URI.parse(url))
  end
end

#tipjoy_request(method, url, data = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tipjoy/tipjoy_requestor.rb', line 23

def tipjoy_request(method, url, data={})
  Tipjoy.logger.error("requesting via #{method} " + url + " with args " + data.inspect)
  response = tipjoy_http_request(method, url, data)

  if !(200..299).include?(response.code.to_i)
    Tipjoy.logger.error("  RequestError " + response.inspect)
    raise RequestError.new('ah!')
  end


  json = JSON.parse(response.body)

  if json['result'] != 'success'
    Tipjoy.logger.error("  ResultError " + json.inspect)
    raise ::Tipjoy::ResultError.new(json)
  end

  Tipjoy.logger.error("  response: " + json.inspect)
  json
end