class NovaposhtaApi::HttpClient
Constants
- DEFAULT_HEADERS
Attributes
config[R]
Public Class Methods
new(config) { |connection| ... }
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 12 def initialize(config) @config = config.with_indifferent_access yield(connection) if block_given? end
Public Instance Methods
api_key()
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 22 def api_key @api_key ||= config[:api_key] end
connection()
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 32 def connection @connection ||= Faraday.new(connection_options) do |client| client.adapter Faraday.default_adapter client.response :error_handling client.response :json end end
request(http_method, path, params = {})
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 26 def request(http_method, path, params = {}) options = build_options(path).merge(methodProperties: transform_keys(params)) response = connection.public_send(http_method, '', options.to_json) response.body end
uri()
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 18 def uri @uri ||= URI(config[:uri]) end
Private Instance Methods
build_options(path)
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 42 def build_options(path) paths = path.split('/') { apiKey: api_key, modelName: paths[0], calledMethod: paths[1] } end
connection_options()
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 52 def connection_options { url: uri, headers: DEFAULT_HEADERS } end
convert_params_to_hash(params)
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 63 def convert_params_to_hash(params) if params.respond_to?(:to_unsafe_h) params.to_unsafe_h else params end end
transform_keys(params)
click to toggle source
# File lib/novaposhta_api/http_client.rb, line 59 def transform_keys(params) convert_params_to_hash(params).deep_transform_keys { |key| key.to_s.camelcase } end