Class: WePredict::API

Inherits:
Object
  • Object
show all
Defined in:
lib/wepredict/api.rb

Direct Known Subclasses

Classifiers::Classifier, User

Constant Summary collapse

@@endpoint =
URI.parse((ENV["WP_DEV"]) ? "http://wepredict.dev/api/" : "http://demo.wepredictapp.com/api/")

Class Method Summary collapse

Class Method Details

.endpointObject

@@endpoint accessor



37
38
39
# File 'lib/wepredict/api.rb', line 37

def self.endpoint
  @@endpoint
end

.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