Class: NovaposhtaApi::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) {|http.connection| ... } ⇒ Client

Returns a new instance of Client.

Yields:



7
8
9
10
11
# File 'lib/novaposhta_api/client.rb', line 7

def initialize(api_key: nil)
  @api_key = api_key

  yield(http.connection) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/novaposhta_api/client.rb', line 24

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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/novaposhta_api/client.rb', line 5

def api_key
  @api_key
end

Class Method Details

.resourcesObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/novaposhta_api/client.rb', line 13

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

Instance Method Details

#httpObject



45
46
47
48
49
50
# File 'lib/novaposhta_api/client.rb', line 45

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

#resourcesObject



41
42
43
# File 'lib/novaposhta_api/client.rb', line 41

def resources
  @resources ||= {}
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/novaposhta_api/client.rb', line 33

def respond_to_missing?(method_name, include_private = false)
  with_resource?(method_name) || super
end

#with_resource?(resource_name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/novaposhta_api/client.rb', line 37

def with_resource?(resource_name)
  self.class.resources.keys.include?(resource_name)
end