novaposhta_api 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'novaposhta_api/version'
4
+ require 'novaposhta_api/configuration'
4
5
 
5
6
  module NovaposhtaApi
6
7
  autoload :Client, 'novaposhta_api/client'
@@ -18,6 +19,7 @@ module NovaposhtaApi
18
19
  autoload :CityResource, 'novaposhta_api/resources/city_resource'
19
20
  autoload :CounterpartyResource, 'novaposhta_api/resources/counterparty_resource'
20
21
  autoload :InternetDocumentResource, 'novaposhta_api/resources/internet_document_resource'
22
+ autoload :TrackingDocumentResource, 'novaposhta_api/resources/tracking_document_resource'
21
23
  autoload :SettlementResource, 'novaposhta_api/resources/settlement_resource'
22
24
  autoload :WarehouseResource, 'novaposhta_api/resources/warehouse_resource'
23
25
  end
@@ -26,16 +28,41 @@ module NovaposhtaApi
26
28
  autoload :City, 'novaposhta_api/models/city'
27
29
  autoload :Counterparty, 'novaposhta_api/models/counterparty'
28
30
  autoload :InternetDocument, 'novaposhta_api/models/internet_document'
31
+ autoload :TrackingDocument, 'novaposhta_api/models/tracking_document'
29
32
  autoload :Settlement, 'novaposhta_api/models/settlement'
30
33
  autoload :Warehouse, 'novaposhta_api/models/warehouse'
34
+ autoload :ContactPerson, 'novaposhta_api/models/contact_person'
31
35
  end
32
36
 
33
37
  Error = Class.new(StandardError)
34
38
  ResourceError = Class.new(Error)
35
39
  ResponseError = Class.new(Error)
36
40
 
41
+ class << self
42
+ attr_writer :configuration
43
+ end
44
+
45
+ def self.configuration
46
+ @configuration ||= Configuration.new
47
+ end
48
+
49
+ def self.reset
50
+ @configuration = Configuration.new
51
+ end
52
+
53
+ # @example
54
+ # NovaposhtaApi.setup do |config|
55
+ # config.api_url = 'https://api.novaposhta.ua/v2.0/json/'
56
+ # end
57
+ #
58
+ def self.setup
59
+ yield configuration
60
+ end
61
+
37
62
  if Faraday::Middleware.respond_to? :register_middleware
38
63
  Faraday::Response.register_middleware json: NovaposhtaApi::Middlewares::ParseJson
39
64
  Faraday::Response.register_middleware error_handling: NovaposhtaApi::Middlewares::ErrorHandling
40
65
  end
41
66
  end
67
+
68
+ require 'novaposhta_api/rails'
@@ -2,11 +2,12 @@
2
2
 
3
3
  module NovaposhtaApi
4
4
  class Client
5
- attr_reader :api_url, :api_key
5
+ attr_reader :api_key
6
6
 
7
- def initialize(api_url: nil, api_key:)
8
- @api_url = api_url || ENV['NOVAPOSHTA_URL']
9
- @api_key = api_key || delivery_member_api_key || ENV['NOVAPOSHTA_KEY']
7
+ def initialize(api_key: nil)
8
+ @api_key = api_key
9
+
10
+ yield(http.connection) if block_given?
10
11
  end
11
12
 
12
13
  def self.resources
@@ -14,14 +15,15 @@ module NovaposhtaApi
14
15
  cities: Resources::CityResource,
15
16
  counterparties: Resources::CounterpartyResource,
16
17
  internet_documents: Resources::InternetDocumentResource,
18
+ tracking_documents: Resources::TrackingDocumentResource,
17
19
  settlements: Resources::SettlementResource,
18
20
  warehouses: Resources::WarehouseResource
19
21
  }
20
22
  end
21
23
 
22
24
  def method_missing(name, *args, &block)
23
- if self.class.resources.keys.include?(name)
24
- resources[name] ||= self.class.resources[name].new(connection: connection)
25
+ if with_resource?(name)
26
+ resources[name] ||= self.class.resources[name].new(http: http)
25
27
  resources[name]
26
28
  else
27
29
  super
@@ -29,22 +31,22 @@ module NovaposhtaApi
29
31
  end
30
32
 
31
33
  def respond_to_missing?(method_name, include_private = false)
32
- self.class.resources.keys.include?(method_name) || super
34
+ with_resource?(method_name) || super
35
+ end
36
+
37
+ def with_resource?(resource_name)
38
+ self.class.resources.keys.include?(resource_name)
33
39
  end
34
40
 
35
41
  def resources
36
42
  @resources ||= {}
37
43
  end
38
44
 
39
- def connection
40
- @connection ||= NovaposhtaApi::HttpClient.new(
41
- uri: api_url,
42
- api_key: api_key
45
+ def http
46
+ @http ||= NovaposhtaApi::HttpClient.new(
47
+ uri: NovaposhtaApi.configuration.api_url,
48
+ api_key: api_key || NovaposhtaApi.configuration.api_key
43
49
  )
44
50
  end
45
-
46
- def delivery_member_api_key
47
- @delivery_member_api_key ||= Delivery::Member.select(:api_key).primary&.api_key
48
- end
49
51
  end
50
52
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NovaposhtaApi
4
+ class Configuration
5
+ attr_accessor :api_url, :api_key
6
+
7
+ def initialize
8
+ @api_url = 'https://api.novaposhta.ua/v2.0/json/'
9
+ @api_key = ''
10
+ end
11
+ end
12
+ end
@@ -11,6 +11,8 @@ module NovaposhtaApi
11
11
 
12
12
  def initialize(config)
13
13
  @config = config.with_indifferent_access
14
+
15
+ yield(connection) if block_given?
14
16
  end
15
17
 
16
18
  def uri
@@ -22,26 +24,21 @@ module NovaposhtaApi
22
24
  end
23
25
 
24
26
  def request(http_method, path, params = {})
25
- options = build_options(path).merge!(methodProperties: transform_keys(params))
26
-
27
+ options = build_options(path).merge(methodProperties: transform_keys(params))
27
28
  response = connection.public_send(http_method, '', options.to_json)
28
29
  response.body
29
30
  end
30
31
 
31
- private
32
-
33
- def transform_keys(params)
34
- convert_params_to_hash(params).deep_transform_keys { |key| key.to_s.camelcase }
35
- end
36
-
37
- def convert_params_to_hash(params)
38
- if params.respond_to?(:to_unsafe_h)
39
- params.to_unsafe_h
40
- else
41
- params
32
+ def connection
33
+ @connection ||= Faraday.new(connection_options) do |client|
34
+ client.adapter Faraday.default_adapter
35
+ client.response :error_handling
36
+ client.response :json
42
37
  end
43
38
  end
44
39
 
40
+ private
41
+
45
42
  def build_options(path)
46
43
  paths = path.split('/')
47
44
 
@@ -52,20 +49,23 @@ module NovaposhtaApi
52
49
  }
53
50
  end
54
51
 
55
- def connection
56
- @connection ||= Faraday.new(connection_options) do |client|
57
- client.adapter Faraday.default_adapter
58
- client.use NovaposhtaApi::Middlewares::ErrorHandling
59
- client.response :json
60
- client.response :logger if Rails.env.development?
61
- end
62
- end
63
-
64
52
  def connection_options
65
53
  {
66
54
  url: uri,
67
55
  headers: DEFAULT_HEADERS
68
56
  }
69
57
  end
58
+
59
+ def transform_keys(params)
60
+ convert_params_to_hash(params).deep_transform_keys { |key| key.to_s.camelcase }
61
+ end
62
+
63
+ def convert_params_to_hash(params)
64
+ if params.respond_to?(:to_unsafe_h)
65
+ params.to_unsafe_h
66
+ else
67
+ params
68
+ end
69
+ end
70
70
  end
71
71
  end
@@ -12,13 +12,13 @@ module NovaposhtaApi
12
12
  end
13
13
 
14
14
  def error_message(body)
15
- (from_codes(body['errorCodes']) || body['errors']).join(SEPARATOR)
15
+ (from_codes(body['errorCodes']) || body['errors'] || body['message']).join(SEPARATOR)
16
16
  end
17
17
 
18
18
  def from_codes(codes = [])
19
19
  return nil if codes.blank?
20
20
 
21
- codes.map { |code| I18n.t(code, scope: %i[NovaposhtaApi errors]) }
21
+ codes.map { |code| I18n.t(code, scope: %i[novaposhta errors]) }
22
22
  end
23
23
  end
24
24
  end
@@ -7,6 +7,9 @@ module NovaposhtaApi
7
7
  property :city_id, from: :CityID
8
8
  property :area, from: :Area
9
9
  property :description, from: :Description
10
+ property :description_ru, from: :DescriptionRu
11
+ property :settlement_type_description, from: :SettlementTypeDescription
12
+ property :settlement_type_description_ru, from: :SettlementTypeDescriptionRu
10
13
  end
11
14
  end
12
15
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NovaposhtaApi
4
+ module Models
5
+ class ContactPerson < Model
6
+ property :ref, from: :Ref
7
+ property :first_name, from: :FirstName
8
+ property :middle_name, from: :MiddleName
9
+ property :last_name, from: :LastName
10
+ property :phones, from: :Phones
11
+ property :description, from: :Description
12
+ property :email, from: :Email
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NovaposhtaApi
4
+ module Models
5
+ class TrackingDocument < Model
6
+ property :number, from: :Number
7
+ property :status, from: :Status
8
+ property :status_code, from: :StatusCode
9
+ property :scheduled_delivery_date, from: :ScheduledDeliveryDate
10
+ end
11
+ end
12
+ end
@@ -6,7 +6,9 @@ module NovaposhtaApi
6
6
  property :ref, from: :Ref
7
7
  property :city_ref, from: :CityRef
8
8
  property :city_description, from: :CityDescription
9
+ property :city_description_ru, from: :CityDescriptionRu
9
10
  property :description, from: :Description
11
+ property :description_ru, from: :DescriptionRu
10
12
  property :short_address, from: :ShortAddress
11
13
  property :longitude, from: :Longitude
12
14
  property :latitude, from: :Latitude
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NovaposhtaApi
4
+ class Engine < ::Rails::Engine
5
+ end
6
+ end
@@ -2,18 +2,17 @@
2
2
 
3
3
  module NovaposhtaApi
4
4
  class Resource
5
- attr_reader :connection
5
+ attr_reader :http
6
6
 
7
7
  HTTP_METHODS = %i[get post patch put delete].freeze
8
- CACHE_OPTIONS = {expires_in: 1.day}.freeze
9
8
 
10
- def initialize(connection: nil)
11
- @connection = connection
9
+ def initialize(http: nil)
10
+ @http = http
12
11
  end
13
12
 
14
13
  HTTP_METHODS.each do |http_method|
15
14
  define_method http_method do |*args|
16
- response = connection.request(http_method, *args)
15
+ response = http.request(http_method, *args)
17
16
  response['data']
18
17
  end
19
18
  end
@@ -4,9 +4,7 @@ module NovaposhtaApi
4
4
  module Resources
5
5
  class CityResource < Resource
6
6
  def all(params = {})
7
- response = Rails.cache.fetch("cities/#{params}", CACHE_OPTIONS) do
8
- post('Address/getCities', params)
9
- end
7
+ response = post('Address/getCities', params)
10
8
 
11
9
  NovaposhtaApi::Models::City.extract_collection(response)
12
10
  end
@@ -4,9 +4,7 @@ module NovaposhtaApi
4
4
  module Resources
5
5
  class SettlementResource < Resource
6
6
  def all(params = {})
7
- response = Rails.cache.fetch("settlements/#{params}", CACHE_OPTIONS) do
8
- post('Address/searchSettlements', params)
9
- end
7
+ response = post('Address/searchSettlements', params)
10
8
 
11
9
  NovaposhtaApi::Models::Settlement.extract_collection(response[0]['Addresses'])
12
10
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NovaposhtaApi
4
+ module Resources
5
+ class TrackingDocumentResource < Resource
6
+ def track(number)
7
+ response = post(
8
+ 'TrackingDocument/getStatusDocuments', documents: [{document_number: number}]
9
+ )
10
+
11
+ NovaposhtaApi::Models::TrackingDocument.extract_single(response)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -4,9 +4,7 @@ module NovaposhtaApi
4
4
  module Resources
5
5
  class WarehouseResource < Resource
6
6
  def all(params = {})
7
- response = Rails.cache.fetch("warehouses/#{params}", CACHE_OPTIONS) do
8
- post('Address/getWarehouses', params)
9
- end
7
+ response = post('Address/getWarehouses', params)
10
8
 
11
9
  NovaposhtaApi::Models::Warehouse.extract_collection(response)
12
10
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NovaposhtaApi
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: novaposhta_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - venet
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-22 00:00:00.000000000 Z
11
+ date: 2021-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -75,23 +75,31 @@ files:
75
75
  - Rakefile
76
76
  - bin/console
77
77
  - bin/setup
78
+ - config/locales/novaposhta.en.yml
79
+ - config/locales/novaposhta.ru.yml
80
+ - config/locales/novaposhta.uk.yml
78
81
  - lib/novaposhta_api.rb
79
82
  - lib/novaposhta_api/client.rb
80
83
  - lib/novaposhta_api/collection.rb
84
+ - lib/novaposhta_api/configuration.rb
81
85
  - lib/novaposhta_api/http_client.rb
82
86
  - lib/novaposhta_api/middlewares/error_handling.rb
83
87
  - lib/novaposhta_api/middlewares/parse_json.rb
84
88
  - lib/novaposhta_api/model.rb
85
89
  - lib/novaposhta_api/models/city.rb
90
+ - lib/novaposhta_api/models/contact_person.rb
86
91
  - lib/novaposhta_api/models/counterparty.rb
87
92
  - lib/novaposhta_api/models/internet_document.rb
88
93
  - lib/novaposhta_api/models/settlement.rb
94
+ - lib/novaposhta_api/models/tracking_document.rb
89
95
  - lib/novaposhta_api/models/warehouse.rb
96
+ - lib/novaposhta_api/rails.rb
90
97
  - lib/novaposhta_api/resource.rb
91
98
  - lib/novaposhta_api/resources/city_resource.rb
92
99
  - lib/novaposhta_api/resources/counterparty_resource.rb
93
100
  - lib/novaposhta_api/resources/internet_document_resource.rb
94
101
  - lib/novaposhta_api/resources/settlement_resource.rb
102
+ - lib/novaposhta_api/resources/tracking_document_resource.rb
95
103
  - lib/novaposhta_api/resources/warehouse_resource.rb
96
104
  - lib/novaposhta_api/version.rb
97
105
  - novaposhta_api.gemspec
@@ -100,7 +108,7 @@ licenses:
100
108
  - MIT
101
109
  metadata:
102
110
  homepage_uri: https://github.com/venethub/novaposhta_api
103
- post_install_message:
111
+ post_install_message:
104
112
  rdoc_options: []
105
113
  require_paths:
106
114
  - lib
@@ -115,9 +123,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
123
  - !ruby/object:Gem::Version
116
124
  version: '0'
117
125
  requirements: []
118
- rubyforge_project:
119
- rubygems_version: 2.7.6.2
120
- signing_key:
126
+ rubygems_version: 3.0.9
127
+ signing_key:
121
128
  specification_version: 4
122
129
  summary: Novaposhta API 2.0 client
123
130
  test_files: []