Sha256: 427e60eb79aa5e94deb766d454eacb023b8d14bf9467acdc1112b61a3078154b

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module NovaposhtaApi
  class HttpClient
    DEFAULT_HEADERS = {
      accept: 'application/json',
      content_type: 'application/json'
    }.freeze

    attr_reader :config

    def initialize(config)
      @config = config.with_indifferent_access

      yield(connection) if block_given?
    end

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

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

    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

    def connection
      @connection ||= Faraday.new(connection_options) do |client|
        client.adapter Faraday.default_adapter
        client.response :error_handling
        client.response :json
      end
    end

    private

    def build_options(path)
      paths = path.split('/')

      {
        apiKey: api_key,
        modelName: paths[0],
        calledMethod: paths[1]
      }
    end

    def connection_options
      {
        url: uri,
        headers: DEFAULT_HEADERS
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
novaposhta_api-0.2.1 lib/novaposhta_api/http_client.rb
novaposhta_api-0.2.0 lib/novaposhta_api/http_client.rb