Class: NovaposhtaApi::HttpClient
- Inherits:
-
Object
- Object
- NovaposhtaApi::HttpClient
- 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
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #api_key ⇒ Object
- #connection ⇒ Object
-
#initialize(config) {|connection| ... } ⇒ HttpClient
constructor
A new instance of HttpClient.
- #request(http_method, path, params = {}) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(config) {|connection| ... } ⇒ HttpClient
Returns a new instance of HttpClient.
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
#config ⇒ Object (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_key ⇒ Object
22 23 24 |
# File 'lib/novaposhta_api/http_client.rb', line 22 def api_key @api_key ||= config[:api_key] end |
#connection ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/novaposhta_api/http_client.rb', line 32 def connection @connection ||= Faraday.new() 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 = {}) = (path).merge(methodProperties: params) response = connection.public_send(http_method, '', .to_json) response.body end |
#uri ⇒ Object
18 19 20 |
# File 'lib/novaposhta_api/http_client.rb', line 18 def uri @uri ||= URI(config[:uri]) end |