Sha256: 3f144745dcf8b4e899829a19cdecee1a5d9b79a29b288746244c596506f5123d

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module NovaposhtaApi
  class Client
    attr_reader :api_url, :api_key

    def initialize(api_url: nil, api_key:)
      @api_url = api_url || ENV['NOVAPOSHTA_URL']
      @api_key = api_key || delivery_member_api_key || ENV['NOVAPOSHTA_KEY']
    end

    def self.resources
      {
        cities: Resources::CityResource,
        counterparties: Resources::CounterpartyResource,
        internet_documents: Resources::InternetDocumentResource,
        settlements: Resources::SettlementResource,
        warehouses: Resources::WarehouseResource
      }
    end

    def method_missing(name, *args, &block)
      if self.class.resources.keys.include?(name)
        resources[name] ||= self.class.resources[name].new(connection: connection)
        resources[name]
      else
        super
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      self.class.resources.keys.include?(method_name) || super
    end

    def resources
      @resources ||= {}
    end

    def connection
      @connection ||= NovaposhtaApi::HttpClient.new(
        uri: api_url,
        api_key: api_key
      )
    end

    def delivery_member_api_key
      @delivery_member_api_key ||= Delivery::Member.select(:api_key).primary&.api_key
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
novaposhta_api-0.1.0 lib/novaposhta_api/client.rb