class HOALife::Client::Base
Base
class for all HTTP requests Handles the implementation specific code of the API
Public Class Methods
new(url, body = nil)
click to toggle source
# File lib/hoalife/client/base.rb, line 10 def initialize(url, body = nil) @url = url @body = body end
Public Instance Methods
json()
click to toggle source
# File lib/hoalife/client/base.rb, line 19 def json @json ||= JSON.parse(response.body) end
response()
click to toggle source
# File lib/hoalife/client/base.rb, line 23 def response @response ||= validate_response! end
status()
click to toggle source
# File lib/hoalife/client/base.rb, line 15 def status @status ||= response.code.to_i end
Private Instance Methods
api_version()
click to toggle source
# File lib/hoalife/client/base.rb, line 101 def api_version return nil if HOALife.api_version.nil? "application/vnd.api+json; version=#{HOALife.api_version}" end
auth_error(headers, body, code)
click to toggle source
rubocop:enable Metrics/MethodLength
# File lib/hoalife/client/base.rb, line 74 def auth_error(headers, body, code) case code when 401 raise HOALife::UnauthorizedError.new(code, headers, body) when 403 raise HOALife::ForbiddenError.new(code, headers, body) end end
generic_error(resp)
click to toggle source
# File lib/hoalife/client/base.rb, line 113 def generic_error(resp) JSON.parse(resp.body) rescue JSON::ParserError { 'data' => { 'id' => resp['X-Request-Id'], 'type' => 'error', 'attributes' => { 'id' => resp['X-Request-Id'], 'title' => 'HTTP Error', 'status' => resp.code.to_i, 'detail' => resp.body } } } end
http_error(headers, body, code)
click to toggle source
# File lib/hoalife/client/base.rb, line 83 def http_error(headers, body, code) case code when 404 raise HOALife::NotFoundError.new(code, headers, body) when 429 raise HOALife::RateLimitError.new(code, headers, body) end end
request!()
click to toggle source
# File lib/hoalife/client/base.rb, line 29 def request! raise 'Not implemented' end
request_headers()
click to toggle source
# File lib/hoalife/client/base.rb, line 37 def request_headers { 'Authorization' => authorization_header, 'ACCEPT' => api_version, 'Content-Type' => 'application/vnd.api+json' } end
uri()
click to toggle source
# File lib/hoalife/client/base.rb, line 33 def uri @uri ||= URI(@url) end
validate_response!()
click to toggle source
# File lib/hoalife/client/base.rb, line 45 def validate_response! response = request! verify_successful_response!(response) verify_signature!(response) unless HOALife.signing_secret.nil? response end
verify_signature!(resp)
click to toggle source
# File lib/hoalife/client/base.rb, line 92 def verify_signature!(resp) digest = OpenSSL::Digest.new('sha256') signature = OpenSSL::HMAC.hexdigest( digest, HOALife.signing_secret, resp.body ) raise HOALife::SigningMissmatchError if signature != resp['X-Signature'] end
verify_successful_response!(resp)
click to toggle source
rubocop:disable Metrics/MethodLength
# File lib/hoalife/client/base.rb, line 56 def verify_successful_response!(resp) headers = resp.each_header.to_h body = resp.body code = resp.code.to_i case code when 400 raise HOALife::BadRequestError.new(code, headers, body) when 401..403 auth_error(headers, body, code) when 404..429 http_error(headers, body, code) when 400..600 raise HOALife::HTTPError.new(code, headers, generic_error(resp)) end end