Sha256: 99b468d59651c9113d20df53be797b8e4c268dfd391a11da5e6c512044f80cb7

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 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: transform_keys(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

    def transform_keys(params)
      convert_params_to_hash(params).deep_transform_keys { |key| key.to_s.camelcase }
    end

    def convert_params_to_hash(params)
      if params.respond_to?(:to_unsafe_h)
        params.to_unsafe_h
      else
        params
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
novaposhta_api-0.1.9 lib/novaposhta_api/http_client.rb
novaposhta_api-0.1.8 lib/novaposhta_api/http_client.rb
novaposhta_api-0.1.7 lib/novaposhta_api/http_client.rb
novaposhta_api-0.1.5 lib/novaposhta_api/http_client.rb