class Betaface::REST::Client
Constants
- HTTP_HEADERS
Attributes
last_request[R]
last_response[R]
Public Class Methods
new(*args)
click to toggle source
Instantiate the Client
with a key/secret if not set before Just call new if Betaface.key
and Betaface.secret
have been init Last argument is a hash of options
# File lib/betaface/rest/client.rb, line 20 def initialize(*args) @config = Betaface::Util::ClientConfig.new @key = args[0] || Betaface.key @secret = args[1] || Betaface.secret if @key.nil? || @secret.nil? raise ArgumentError, 'Key and Secret are required' end end
Protected Instance Methods
connect_and_send(request)
click to toggle source
Send an HTTP request using the cached @connection
object and return the JSON response body parsed into a hash. Also save the raw Net::HTTP::Request and Net::HTTP::Response objects as @last_request
and @last_response
to allow for inspection later.
# File lib/betaface/rest/client.rb, line 55 def connect_and_send(request) @last_request = request retries_left = @config.retry_limit begin http = Net::HTTP.new(request.uri.host, request.uri.port) http.use_ssl = true if request.uri.scheme == "https" response = http.start do |h| h.request(request) end @last_response = response if response.kind_of? Net::HTTPServerError raise Betaface::REST::ServerError end rescue raise if request.class == Net::HTTP::Post if retries_left > 0 then retries_left -= 1; retry else raise end end if response.body and !response.body.empty? object = MultiJson.load response.body elsif response.kind_of? Net::HTTPBadRequest object = { message: 'Bad request', code: 400 } end if response.kind_of? Net::HTTPClientError raise Betaface::REST::RequestError.new object['message'], object['code'] end object end