novaposhta_api 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rubocop.yml +69 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +4 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/novaposhta_api.rb +41 -0
- data/lib/novaposhta_api/client.rb +50 -0
- data/lib/novaposhta_api/collection.rb +13 -0
- data/lib/novaposhta_api/http_client.rb +71 -0
- data/lib/novaposhta_api/middlewares/error_handling.rb +25 -0
- data/lib/novaposhta_api/middlewares/parse_json.rb +17 -0
- data/lib/novaposhta_api/model.rb +20 -0
- data/lib/novaposhta_api/models/city.rb +12 -0
- data/lib/novaposhta_api/models/counterparty.rb +20 -0
- data/lib/novaposhta_api/models/internet_document.rb +35 -0
- data/lib/novaposhta_api/models/settlement.rb +14 -0
- data/lib/novaposhta_api/models/warehouse.rb +15 -0
- data/lib/novaposhta_api/resource.rb +21 -0
- data/lib/novaposhta_api/resources/city_resource.rb +15 -0
- data/lib/novaposhta_api/resources/counterparty_resource.rb +31 -0
- data/lib/novaposhta_api/resources/internet_document_resource.rb +13 -0
- data/lib/novaposhta_api/resources/settlement_resource.rb +15 -0
- data/lib/novaposhta_api/resources/warehouse_resource.rb +15 -0
- data/lib/novaposhta_api/version.rb +5 -0
- data/novaposhta_api.gemspec +32 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fa27fbf374f3c2131a1ad691a56accaad9901e15fe1111fce24dbedc80b326c6
|
4
|
+
data.tar.gz: 46c58b81d0cfae0d082333bc00ffac474b260ce8107be167621972cd10fdafc4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 335aa31031267df1cf5d47ac3bab9b4d1e61d9a7324f59544948320cafe7f6f58e9df6d02d43a40dd67b33b638fc0e13d371c817ff97a20b62cf31a556eb0461
|
7
|
+
data.tar.gz: 767b541fc7ef1c96453f90360f86ca87544e1aceb92658da1d69bceaa10b0c3eb3c4819dbeabf3329932a18ee2b2f07e8a6366cea5acc5c5d1f494c9536fc53f
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5.1
|
3
|
+
Exclude:
|
4
|
+
- "db/schema.rb"
|
5
|
+
- "node_modules/**/*"
|
6
|
+
- "vendor/**/*"
|
7
|
+
Layout/CaseIndentation:
|
8
|
+
IndentOneStep: true
|
9
|
+
Layout/FirstArrayElementLineBreak:
|
10
|
+
Enabled: true
|
11
|
+
Layout/FirstHashElementLineBreak:
|
12
|
+
Enabled: true
|
13
|
+
Layout/FirstMethodArgumentLineBreak:
|
14
|
+
Enabled: true
|
15
|
+
Layout/FirstMethodParameterLineBreak:
|
16
|
+
Enabled: true
|
17
|
+
Layout/MultilineAssignmentLayout:
|
18
|
+
Enabled: true
|
19
|
+
EnforcedStyle: same_line
|
20
|
+
Layout/SpaceInsideHashLiteralBraces:
|
21
|
+
EnforcedStyle: no_space
|
22
|
+
Metrics/BlockLength:
|
23
|
+
Exclude:
|
24
|
+
- "**/*.gemspec"
|
25
|
+
- "spec/**/*"
|
26
|
+
Metrics/LineLength:
|
27
|
+
Max: 100
|
28
|
+
Metrics/ParameterLists:
|
29
|
+
Max: 3
|
30
|
+
Style/AndOr:
|
31
|
+
EnforcedStyle: conditionals
|
32
|
+
Style/AutoResourceCleanup:
|
33
|
+
Enabled: true
|
34
|
+
Style/CollectionMethods:
|
35
|
+
Enabled: true
|
36
|
+
Style/Documentation:
|
37
|
+
Enabled: false
|
38
|
+
Style/EmptyLiteral:
|
39
|
+
Enabled: false
|
40
|
+
Style/EmptyMethod:
|
41
|
+
EnforcedStyle: expanded
|
42
|
+
Style/ImplicitRuntimeError:
|
43
|
+
Enabled: true
|
44
|
+
Style/MethodCalledOnDoEndBlock:
|
45
|
+
Enabled: true
|
46
|
+
Style/MissingElse:
|
47
|
+
Enabled: true
|
48
|
+
EnforcedStyle: case
|
49
|
+
Style/NumericLiterals:
|
50
|
+
Enabled: false
|
51
|
+
Style/PercentLiteralDelimiters:
|
52
|
+
PreferredDelimiters:
|
53
|
+
"%w": "[]"
|
54
|
+
"%W": "[]"
|
55
|
+
"%i": "[]"
|
56
|
+
"%I": "[]"
|
57
|
+
"%r": "()"
|
58
|
+
Style/SafeNavigation:
|
59
|
+
Enabled: false
|
60
|
+
Style/Send:
|
61
|
+
Enabled: true
|
62
|
+
Style/SignalException:
|
63
|
+
EnforcedStyle: semantic
|
64
|
+
Style/StringMethods:
|
65
|
+
Enabled: true
|
66
|
+
Style/SymbolArray:
|
67
|
+
Enabled: true
|
68
|
+
Naming/VariableNumber:
|
69
|
+
EnforcedStyle: snake_case
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at venet.dev@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 venet
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# NovaposhtaApi
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/novaposhta_api`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'novaposhta_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install novaposhta_api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/novaposhta_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/novaposhta_api/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
41
|
+
|
42
|
+
## Code of Conduct
|
43
|
+
|
44
|
+
Everyone interacting in the NovaposhtaApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/novaposhta_api/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'novaposhta_api'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'novaposhta_api/version'
|
4
|
+
|
5
|
+
module NovaposhtaApi
|
6
|
+
autoload :Client, 'novaposhta_api/client'
|
7
|
+
autoload :HttpClient, 'novaposhta_api/http_client'
|
8
|
+
autoload :Resource, 'novaposhta_api/resource'
|
9
|
+
autoload :Model, 'novaposhta_api/model'
|
10
|
+
autoload :Collection, 'novaposhta_api/collection'
|
11
|
+
|
12
|
+
module Middlewares
|
13
|
+
autoload :ErrorHandling, 'novaposhta_api/middlewares/error_handling'
|
14
|
+
autoload :ParseJson, 'novaposhta_api/middlewares/parse_json'
|
15
|
+
end
|
16
|
+
|
17
|
+
module Resources
|
18
|
+
autoload :CityResource, 'novaposhta_api/resources/city_resource'
|
19
|
+
autoload :CounterpartyResource, 'novaposhta_api/resources/counterparty_resource'
|
20
|
+
autoload :InternetDocumentResource, 'novaposhta_api/resources/internet_document_resource'
|
21
|
+
autoload :SettlementResource, 'novaposhta_api/resources/settlement_resource'
|
22
|
+
autoload :WarehouseResource, 'novaposhta_api/resources/warehouse_resource'
|
23
|
+
end
|
24
|
+
|
25
|
+
module Models
|
26
|
+
autoload :City, 'novaposhta_api/models/city'
|
27
|
+
autoload :Counterparty, 'novaposhta_api/models/counterparty'
|
28
|
+
autoload :InternetDocument, 'novaposhta_api/models/internet_document'
|
29
|
+
autoload :Settlement, 'novaposhta_api/models/settlement'
|
30
|
+
autoload :Warehouse, 'novaposhta_api/models/warehouse'
|
31
|
+
end
|
32
|
+
|
33
|
+
Error = Class.new(StandardError)
|
34
|
+
ResourceError = Class.new(Error)
|
35
|
+
ResponseError = Class.new(Error)
|
36
|
+
|
37
|
+
if Faraday::Middleware.respond_to? :register_middleware
|
38
|
+
Faraday::Response.register_middleware json: NovaposhtaApi::Middlewares::ParseJson
|
39
|
+
Faraday::Response.register_middleware error_handling: NovaposhtaApi::Middlewares::ErrorHandling
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
class Client
|
5
|
+
attr_reader :api_url, :api_key
|
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']
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.resources
|
13
|
+
{
|
14
|
+
cities: Resources::CityResource,
|
15
|
+
counterparties: Resources::CounterpartyResource,
|
16
|
+
internet_documents: Resources::InternetDocumentResource,
|
17
|
+
settlements: Resources::SettlementResource,
|
18
|
+
warehouses: Resources::WarehouseResource
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
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
|
+
resources[name]
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_to_missing?(method_name, include_private = false)
|
32
|
+
self.class.resources.keys.include?(method_name) || super
|
33
|
+
end
|
34
|
+
|
35
|
+
def resources
|
36
|
+
@resources ||= {}
|
37
|
+
end
|
38
|
+
|
39
|
+
def connection
|
40
|
+
@connection ||= NovaposhtaApi::HttpClient.new(
|
41
|
+
uri: api_url,
|
42
|
+
api_key: api_key
|
43
|
+
)
|
44
|
+
end
|
45
|
+
|
46
|
+
def delivery_member_api_key
|
47
|
+
@delivery_member_api_key ||= Delivery::Member.select(:api_key).primary&.api_key
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
class Collection < SimpleDelegator
|
5
|
+
def initialize(collection, klass)
|
6
|
+
fail StandardError, 'Collection must be a Array' unless collection.is_a? Array
|
7
|
+
|
8
|
+
@collection = collection.map { |item| klass.new(item) }
|
9
|
+
|
10
|
+
__setobj__(@collection)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
class HttpClient
|
5
|
+
DEFAULT_HEADERS = {
|
6
|
+
accept: 'application/json',
|
7
|
+
content_type: 'application/json'
|
8
|
+
}.freeze
|
9
|
+
|
10
|
+
attr_reader :config
|
11
|
+
|
12
|
+
def initialize(config)
|
13
|
+
@config = config.with_indifferent_access
|
14
|
+
end
|
15
|
+
|
16
|
+
def uri
|
17
|
+
@uri ||= URI(config[:uri])
|
18
|
+
end
|
19
|
+
|
20
|
+
def api_key
|
21
|
+
@api_key ||= config[:api_key]
|
22
|
+
end
|
23
|
+
|
24
|
+
def request(http_method, path, params = {})
|
25
|
+
options = build_options(path).merge!(methodProperties: transform_keys(params))
|
26
|
+
|
27
|
+
response = connection.public_send(http_method, '', options.to_json)
|
28
|
+
response.body
|
29
|
+
end
|
30
|
+
|
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
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_options(path)
|
46
|
+
paths = path.split('/')
|
47
|
+
|
48
|
+
{
|
49
|
+
apiKey: api_key,
|
50
|
+
modelName: paths[0],
|
51
|
+
calledMethod: paths[1]
|
52
|
+
}
|
53
|
+
end
|
54
|
+
|
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
|
+
def connection_options
|
65
|
+
{
|
66
|
+
url: uri,
|
67
|
+
headers: DEFAULT_HEADERS
|
68
|
+
}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Middlewares
|
5
|
+
class ErrorHandling < Faraday::Response::Middleware
|
6
|
+
SEPARATOR = ', '
|
7
|
+
|
8
|
+
def on_complete(env)
|
9
|
+
return if env.response_body['success']
|
10
|
+
|
11
|
+
fail(NovaposhtaApi::ResponseError, error_message(env.body))
|
12
|
+
end
|
13
|
+
|
14
|
+
def error_message(body)
|
15
|
+
(from_codes(body['errorCodes']) || body['errors']).join(SEPARATOR)
|
16
|
+
end
|
17
|
+
|
18
|
+
def from_codes(codes = [])
|
19
|
+
return nil if codes.blank?
|
20
|
+
|
21
|
+
codes.map { |code| I18n.t(code, scope: %i[NovaposhtaApi errors]) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Middlewares
|
5
|
+
class ParseJson < Faraday::Response::Middleware
|
6
|
+
def on_complete(env)
|
7
|
+
process_response(env)
|
8
|
+
end
|
9
|
+
|
10
|
+
def process_response(env)
|
11
|
+
env[:body] = JSON.parse(env[:body])
|
12
|
+
rescue Faraday::ParsingError => e
|
13
|
+
raise Faraday::ParsingError.new(e.wrapped_exception, env[:response])
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
class Model < Hashie::Trash
|
5
|
+
include Hashie::Extensions::IndifferentAccess
|
6
|
+
include Hashie::Extensions::IgnoreUndeclared
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def extract_collection(data)
|
10
|
+
NovaposhtaApi::Collection.new(data, self)
|
11
|
+
end
|
12
|
+
|
13
|
+
def extract_single(data)
|
14
|
+
item = data.is_a?(Array) ? data.first : data
|
15
|
+
|
16
|
+
new(item)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Models
|
5
|
+
class Counterparty < 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 :phone, from: :Phone
|
11
|
+
property :description, from: :Description
|
12
|
+
property :city, from: :City
|
13
|
+
property :persons, from: :ContactPerson
|
14
|
+
|
15
|
+
def person
|
16
|
+
@person ||= NovaposhtaApi::Models::Counterparty.extract_single(persons['data']&.first)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Models
|
5
|
+
class InternetDocument < Model
|
6
|
+
property :ref, from: :Ref
|
7
|
+
property :cost_on_site, from: :CostOnSite
|
8
|
+
property :estimated_delivery_date, from: :EstimatedDeliveryDate
|
9
|
+
property :int_doc_number, from: :IntDocNumber
|
10
|
+
property :type_document, from: :TypeDocument
|
11
|
+
|
12
|
+
property :sender, from: :Sender
|
13
|
+
property :city_sender, from: :CitySender
|
14
|
+
property :sender_address, from: :SenderAddress
|
15
|
+
property :contact_sender, from: :ContactSender
|
16
|
+
property :senders_phone, from: :SendersPhone
|
17
|
+
|
18
|
+
property :recipient, from: :Recipient
|
19
|
+
property :city_recipient, from: :CityRecipient
|
20
|
+
property :recipient_address, from: :RecipientAddress
|
21
|
+
property :contact_recipient, from: :ContactRecipient
|
22
|
+
property :recipients_phone, from: :RecipientsPhone
|
23
|
+
|
24
|
+
property :sevice_type, from: :SeviceType
|
25
|
+
property :cargo_type, from: :CargoType
|
26
|
+
property :payment_method, from: :PaymentMethod
|
27
|
+
property :payer_type, from: :PayerType
|
28
|
+
property :cost, from: :Cost
|
29
|
+
property :seats_amount, from: :SeatsAmount
|
30
|
+
property :description, from: :Description
|
31
|
+
property :weight, from: :Weight
|
32
|
+
property :date_time, from: :DateTime
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Models
|
5
|
+
class Settlement < Model
|
6
|
+
property :ref, from: :Ref
|
7
|
+
property :present, from: :Present
|
8
|
+
property :area, from: :Area
|
9
|
+
property :delivery_city, from: :DeliveryCity
|
10
|
+
property :main_description, from: :MainDescription
|
11
|
+
property :region, from: :Region
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Models
|
5
|
+
class Warehouse < Model
|
6
|
+
property :ref, from: :Ref
|
7
|
+
property :city_ref, from: :CityRef
|
8
|
+
property :city_description, from: :CityDescription
|
9
|
+
property :description, from: :Description
|
10
|
+
property :short_address, from: :ShortAddress
|
11
|
+
property :longitude, from: :Longitude
|
12
|
+
property :latitude, from: :Latitude
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
class Resource
|
5
|
+
attr_reader :connection
|
6
|
+
|
7
|
+
HTTP_METHODS = %i[get post patch put delete].freeze
|
8
|
+
CACHE_OPTIONS = {expires_in: 1.day}.freeze
|
9
|
+
|
10
|
+
def initialize(connection: nil)
|
11
|
+
@connection = connection
|
12
|
+
end
|
13
|
+
|
14
|
+
HTTP_METHODS.each do |http_method|
|
15
|
+
define_method http_method do |*args|
|
16
|
+
response = connection.request(http_method, *args)
|
17
|
+
response['data']
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Resources
|
5
|
+
class CityResource < Resource
|
6
|
+
def all(params = {})
|
7
|
+
response = Rails.cache.fetch("cities/#{params}", CACHE_OPTIONS) do
|
8
|
+
post('Address/getCities', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
NovaposhtaApi::Models::City.extract_collection(response)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Resources
|
5
|
+
class CounterpartyResource < Resource
|
6
|
+
def create(params = {})
|
7
|
+
response = post('Counterparty/save', params)
|
8
|
+
|
9
|
+
NovaposhtaApi::Models::Counterparty.extract_single(response)
|
10
|
+
end
|
11
|
+
|
12
|
+
def senders
|
13
|
+
response = post('Counterparty/getCounterparties', counterparty_property: 'Sender')
|
14
|
+
|
15
|
+
NovaposhtaApi::Models::Counterparty.extract_collection(response)
|
16
|
+
end
|
17
|
+
|
18
|
+
def addresses(ref)
|
19
|
+
response = post('Counterparty/getCounterpartyAddresses', ref: ref)
|
20
|
+
|
21
|
+
NovaposhtaApi::Models::Address.extract_collection(response)
|
22
|
+
end
|
23
|
+
|
24
|
+
def contacts(ref)
|
25
|
+
response = post('Counterparty/getCounterpartyContactPersons', ref: ref)
|
26
|
+
|
27
|
+
NovaposhtaApi::Models::ContactPerson.extract_collection(response)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Resources
|
5
|
+
class InternetDocumentResource < Resource
|
6
|
+
def create(params = {})
|
7
|
+
response = post('InternetDocument/save', params)
|
8
|
+
|
9
|
+
NovaposhtaApi::Models::InternetDocument.extract_single(response)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Resources
|
5
|
+
class SettlementResource < Resource
|
6
|
+
def all(params = {})
|
7
|
+
response = Rails.cache.fetch("settlements/#{params}", CACHE_OPTIONS) do
|
8
|
+
post('Address/searchSettlements', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
NovaposhtaApi::Models::Settlement.extract_collection(response[0]['Addresses'])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NovaposhtaApi
|
4
|
+
module Resources
|
5
|
+
class WarehouseResource < Resource
|
6
|
+
def all(params = {})
|
7
|
+
response = Rails.cache.fetch("warehouses/#{params}", CACHE_OPTIONS) do
|
8
|
+
post('Address/getWarehouses', params)
|
9
|
+
end
|
10
|
+
|
11
|
+
NovaposhtaApi::Models::Warehouse.extract_collection(response)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/novaposhta_api/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'novaposhta_api'
|
7
|
+
spec.version = NovaposhtaApi::VERSION
|
8
|
+
spec.authors = ['venet']
|
9
|
+
spec.email = ['venet.dev@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Novaposhta API 2.0 client'
|
12
|
+
spec.description = 'Ruby wrapper for NovaPoshta API 2.0.'
|
13
|
+
spec.homepage = 'https://github.com/venethub/novaposhta_api'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = 'exe'
|
25
|
+
spec.executables = spec.files.grep(%r(^exe/)) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_dependency 'faraday', '~> 1.0', '>= 1.0.1'
|
29
|
+
spec.add_dependency 'hashie', '~> 4.1'
|
30
|
+
|
31
|
+
spec.add_development_dependency 'rubocop', '~> 0.86.0'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: novaposhta_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- venet
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-06-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.0.1
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: hashie
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '4.1'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '4.1'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rubocop
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.86.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 0.86.0
|
61
|
+
description: Ruby wrapper for NovaPoshta API 2.0.
|
62
|
+
email:
|
63
|
+
- venet.dev@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
69
|
+
- ".rubocop.yml"
|
70
|
+
- CODE_OF_CONDUCT.md
|
71
|
+
- Gemfile
|
72
|
+
- Gemfile.lock
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- bin/console
|
77
|
+
- bin/setup
|
78
|
+
- lib/novaposhta_api.rb
|
79
|
+
- lib/novaposhta_api/client.rb
|
80
|
+
- lib/novaposhta_api/collection.rb
|
81
|
+
- lib/novaposhta_api/http_client.rb
|
82
|
+
- lib/novaposhta_api/middlewares/error_handling.rb
|
83
|
+
- lib/novaposhta_api/middlewares/parse_json.rb
|
84
|
+
- lib/novaposhta_api/model.rb
|
85
|
+
- lib/novaposhta_api/models/city.rb
|
86
|
+
- lib/novaposhta_api/models/counterparty.rb
|
87
|
+
- lib/novaposhta_api/models/internet_document.rb
|
88
|
+
- lib/novaposhta_api/models/settlement.rb
|
89
|
+
- lib/novaposhta_api/models/warehouse.rb
|
90
|
+
- lib/novaposhta_api/resource.rb
|
91
|
+
- lib/novaposhta_api/resources/city_resource.rb
|
92
|
+
- lib/novaposhta_api/resources/counterparty_resource.rb
|
93
|
+
- lib/novaposhta_api/resources/internet_document_resource.rb
|
94
|
+
- lib/novaposhta_api/resources/settlement_resource.rb
|
95
|
+
- lib/novaposhta_api/resources/warehouse_resource.rb
|
96
|
+
- lib/novaposhta_api/version.rb
|
97
|
+
- novaposhta_api.gemspec
|
98
|
+
homepage: https://github.com/venethub/novaposhta_api
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata:
|
102
|
+
homepage_uri: https://github.com/venethub/novaposhta_api
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 2.3.0
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.7.6.2
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Novaposhta API 2.0 client
|
123
|
+
test_files: []
|