class Finicity::Fetchers::Base
Public Class Methods
request(method, endpoint, opts = {})
click to toggle source
# File lib/finicity/fetchers/base.rb, line 15 def request(method, endpoint, opts = {}) tries = 0 loop do begin break fetch(method, endpoint, opts) rescue Net::ReadTimeout, Errno::ECONNREFUSED, Net::OpenTimeout => e raise e if (tries += 1) > Finicity.configs.max_retries.to_i end end end
Protected Class Methods
camelcase_keys(hash)
click to toggle source
# File lib/finicity/fetchers/base.rb, line 62 def camelcase_keys(hash) hash.deep_transform_keys! { |k| k.to_s.camelcase(:lower) } end
default_headers()
click to toggle source
# File lib/finicity/fetchers/base.rb, line 75 def default_headers {} end
fetch(method, endpoint, opts)
click to toggle source
# File lib/finicity/fetchers/base.rb, line 28 def fetch(method, endpoint, opts) request_opts = normalize_request_options(opts) response = send(method, endpoint, request_opts) raise Finicity::ApiServerError, response.body if server_error?(response) Hashie::Mash.new( success?: response.success?, status_code: response.code, body: parse_json(response.body), headers: response.headers ) end
jsonify(body)
click to toggle source
# File lib/finicity/fetchers/base.rb, line 58 def jsonify(body) camelcase_keys(body).to_json end
normalize_request_options(opts)
click to toggle source
# File lib/finicity/fetchers/base.rb, line 43 def normalize_request_options(opts) opts.clone.tap do |o| o[:headers] = default_headers.merge(o[:headers].to_h) o[:body] = jsonify(o[:body]) if o[:body].present? o[:query] = camelcase_keys(o[:query]) if o[:query].present? end end
other_content_type?(response)
click to toggle source
# File lib/finicity/fetchers/base.rb, line 70 def other_content_type?(response) content_type = response.headers["Content-Type"]&.downcase content_type.present? && !content_type.include?("application/json") end
parse_json(body)
click to toggle source
# File lib/finicity/fetchers/base.rb, line 51 def parse_json(body) result = JSON.parse(body.to_s).deep_transform_keys!(&:underscore) Hashie::Mash.new(result) rescue JSON::ParserError body end
server_error?(response)
click to toggle source
# File lib/finicity/fetchers/base.rb, line 66 def server_error?(response) other_content_type?(response) end