Method: WePredict::API.make_request

Defined in:
lib/wepredict/api.rb

.make_request(method, params) ⇒ Object

Makes a request to the WePredict API if the API encounters an error, a WePredictError is raised containing the error message and the response body



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wepredict/api.rb', line 15

def self.make_request(method, params)
  res = Net::HTTP.start(@@endpoint.host, @@endpoint.port) do |h|
    query_string = params.map {|k,v| "#{k}=#{v}"}.join('&')
    h.get("#{@@endpoint.path}#{method}?#{URI.escape(query_string)}")
  end

  json = Yajl::Parser.parse(res.body, :symbolize_keys => true)
  if(json.kind_of?(Hash) && json[:state] == 'error')
    ex = case json[:code]
      when 400
        Errors::BadRequest
      when 403
        Errors::AccessDenied
    end

    raise ex.new(json), json[:message]
  end

  json
end