class Lurch::Client
Constants
- AUTHORIZATION
- STATUS_EXCEPTIONS
Attributes
config[R]
url[R]
Public Class Methods
new(url, config)
click to toggle source
# File lib/lurch/client.rb, line 14 def initialize(url, config) @url = url @config = config end
Public Instance Methods
delete(path, payload = nil)
click to toggle source
# File lib/lurch/client.rb, line 35 def delete(path, payload = nil) response = timed_request("DELETE", path, payload) do client.delete do |req| req.url path req.body = payload unless payload.nil? end end catch_errors(response).body end
get(path)
click to toggle source
# File lib/lurch/client.rb, line 19 def get(path) response = timed_request("GET", path) { client.get(path) } catch_errors(response).body end
patch(path, payload)
click to toggle source
# File lib/lurch/client.rb, line 30 def patch(path, payload) response = timed_request("PATCH", path, payload) { client.patch(path, payload) } catch_errors(response).body end
post(path, payload)
click to toggle source
# File lib/lurch/client.rb, line 24 def post(path, payload) response = timed_request("POST", path, payload) { client.post(path, payload) } # TODO: if 204 is returned, use payload as return body (http://jsonapi.org/format/#crud-creating-responses-204) catch_errors(response).body end
Private Instance Methods
catch_errors(response)
click to toggle source
# File lib/lurch/client.rb, line 69 def catch_errors(response) raise_error(STATUS_EXCEPTIONS[response.status], response) if STATUS_EXCEPTIONS[response.status] raise_error(Errors::ClientError, response) if (400..499).cover?(response.status) raise_error(Errors::ServerError, response) unless (200..299).cover?(response.status) # TODO: handle 3xx response end
client()
click to toggle source
# File lib/lurch/client.rb, line 82 def client @client ||= Faraday.new(url: url, request: { params_encoder: FaradayParamsEncoder }) do |conn| conn.headers[AUTHORIZATION] = authorization unless authorization.nil? conn.request :jsonapi conn.response :jsonapi conn.adapter :typhoeus end end
log_request(method, path, payload)
click to toggle source
# File lib/lurch/client.rb, line 59 def log_request(method, path, payload) Logger.debug { "-> #{method} #{path}" } Logger.debug { "-> #{payload}" } if payload && Lurch.configuration.log_payloads end
log_response(response, time_in_ms)
click to toggle source
# File lib/lurch/client.rb, line 64 def log_response(response, time_in_ms) Logger.debug { "<- #{response.status} in #{time_in_ms}ms" } Logger.debug { "<- #{response.body}" } if Lurch.configuration.log_payloads end
raise_error(klass, response)
click to toggle source
# File lib/lurch/client.rb, line 78 def raise_error(klass, response) raise klass.new(response.body, response.status) end
timed_request(method, path, payload = nil) { || ... }
click to toggle source
# File lib/lurch/client.rb, line 49 def timed_request(method, path, payload = nil) log_request(method, path, payload) start_time = Time.now.to_f response = yield end_time = Time.now.to_f time_in_ms = ((end_time - start_time) * 1000).to_i log_response(response, time_in_ms) response end