Class: NovaposhtaApi::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/novaposhta_api/http_client.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  accept: 'application/json',
  content_type: 'application/json'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) {|connection| ... } ⇒ HttpClient

Returns a new instance of HttpClient.

Yields:



12
13
14
15
16
# File 'lib/novaposhta_api/http_client.rb', line 12

def initialize(config)
  @config = config.with_indifferent_access

  yield(connection) if block_given?
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/novaposhta_api/http_client.rb', line 10

def config
  @config
end

Instance Method Details

#api_keyObject



22
23
24
# File 'lib/novaposhta_api/http_client.rb', line 22

def api_key
  @api_key ||= config[:api_key]
end

#connectionObject



32
33
34
35
36
37
38
# 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 = {}) ⇒ Object



26
27
28
29
30
# File 'lib/novaposhta_api/http_client.rb', line 26

def request(http_method, path, params = {})
  options = build_options(path).merge(methodProperties: params)
  response = connection.public_send(http_method, '', options.to_json)
  response.body
end

#uriObject



18
19
20
# File 'lib/novaposhta_api/http_client.rb', line 18

def uri
  @uri ||= URI(config[:uri])
end