class Infreemation::API
This class is responsible for communicating with the remote API
server
Public Class Methods
get(path, params)
click to toggle source
The get method is doing a POST request. This is because the Infreemation
API
is expecting raw POST data and the Ruby net/http core library understandably does not support this.
# File lib/infreemation/api.rb, line 15 def get(path, params) Infreemation.log :debug, "GET #{path} with #{params.inspect}" resource[path].post(with_auth(params), &parser) rescue RestClient::Exception => e raise RequestError, e.message end
post(path, body)
click to toggle source
# File lib/infreemation/api.rb, line 22 def post(path, body) Infreemation.log :debug, "POST #{path} with #{body.inspect}" resource[path].post(with_auth(body), &parser) rescue RestClient::Exception => e raise RequestError, e.message end
Private Class Methods
parser()
click to toggle source
# File lib/infreemation/api.rb, line 42 def parser lambda do |response, _request, _result| Infreemation.log :debug, response.body begin JSON.parse(response.body, symbolize_names: true).tap do |data| raise Errors[data[:ref]], data[:error] if data[:status] == 'ERROR' end rescue JSON::ParserError raise ResponseError, "JSON invalid (#{response.body[0...100]})" end end end
resource()
click to toggle source
# File lib/infreemation/api.rb, line 38 def resource RestClient::Resource.new(Infreemation.url) end
with_auth(options)
click to toggle source
# File lib/infreemation/api.rb, line 31 def with_auth(options) options.merge( key: Infreemation.api_key, username: Infreemation.username ).to_json end