nova_poshta 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: baf0a2127bb62c5a4479b4b3f73917aca8607eb3
4
+ data.tar.gz: f74ec24bbb02caa93c0fbda878449506a0bac600
5
+ SHA512:
6
+ metadata.gz: db600f9067af16c9f3f1fb78119639fc187352937db4843f5d5265c6a451f0774ad0735a57441da8d483c588ab0454b6955e201deef3433e6a74678197f171a9
7
+ data.tar.gz: b8b30f998e7e54c83a08b184a875724f9b7df5c80ae7f823dccf9a90cbe9b43ed86bde55d3eb68c3ab52ab3ecb1b165d509758ce03f5af585eec2e42f3415898
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+ .ruby-version
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.12.3
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at gigorok@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nova_poshta.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Igor Gonchar
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,61 @@
1
+ # NovaPoshta
2
+
3
+ [![Build Status](https://travis-ci.org/gigorok/nova_poshta.svg?branch=master)](https://travis-ci.org/gigorok/nova_poshta)
4
+
5
+ Ruby-client to novaposhta.ua API2.
6
+
7
+ Look at API documentation by link https://my.novaposhta.ua/data/instruction_JSON_ver.1.7.pdf
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'nova_poshta'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install nova_poshta
24
+
25
+ ## Configuration
26
+
27
+ By default the application gets API KEY from ENV['NOVA_POSHTA_API_KEY'].
28
+ But you can set it directly by next code:
29
+ ```ruby
30
+ NovaPoshta.configure do |config|
31
+ config.api_key = 'YOUR_API_KEY'
32
+ end
33
+ ```
34
+
35
+ ## Usage
36
+ ```ruby
37
+ a = NovaPoshta::Model::Address.new # an instance of address model
38
+ area = a.areas.to_a.sample # to get a some area
39
+ area.cities # to get all cities
40
+ city = a.cities(find_by_string: '[city name]').to_a.sample # you can filter by city name
41
+ city.area # get the area from a city instance
42
+ warehouse = city.warehouses(find_by_string: '[warehouse address]').to_a.sample
43
+ a.warehouses(city.ref)
44
+ a.areas
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gigorok/nova_poshta. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
56
+
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
61
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nova_poshta"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,57 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'json'
4
+
5
+ module NovaPoshta
6
+ class Api
7
+ API_URL = 'https://api.novaposhta.ua/v2.0/json/'.freeze
8
+
9
+ attr_accessor :api_key, :model_name
10
+
11
+ def initialize(model_name)
12
+ self.api_key = NovaPoshta.configuration.api_key
13
+ self.model_name = model_name
14
+ end
15
+
16
+ def request(called_method, method_properties={})
17
+ response = post(request_body(called_method, method_properties))
18
+ ::NovaPoshta::Result.new(response, called_method, self)
19
+ end
20
+
21
+ def request_body(called_method, method_props={})
22
+ camelize_keys(
23
+ {
24
+ api_key: api_key,
25
+ model_name: model_name,
26
+ called_method: called_method,
27
+ method_properties: method_properties(method_props)
28
+ }, :lower
29
+ ).to_json
30
+ end
31
+
32
+ protected
33
+
34
+ def method_properties(method_props)
35
+ camelize_keys(method_props, :upper)
36
+ end
37
+
38
+ def post(req_body)
39
+ uri = URI.parse(API_URL)
40
+ https = Net::HTTP.new(uri.host, uri.port)
41
+ https.use_ssl = true if uri.scheme == 'https'
42
+ req = Net::HTTP::Post.new(uri.path)
43
+ req.body = req_body
44
+ https.request(req).body
45
+ end
46
+
47
+ def camelize_keys(h, first_letter = :upper)
48
+ h.keys.each do |k|
49
+ new_key = k.to_s.camelize(first_letter)
50
+ new_key = new_key.to_sym if k.is_a? Symbol
51
+ h[new_key] = h.delete(k)
52
+ end
53
+ h
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,14 @@
1
+ module NovaPoshta
2
+ class Configuration
3
+ attr_writer :api_key
4
+
5
+ def initialize
6
+ @api_key = ENV['NOVA_POSHTA_API_KEY']
7
+ end
8
+
9
+ def api_key
10
+ @api_key || (raise UndefinedApiKeyError.new)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ module NovaPoshta
2
+ class Error < StandardError
3
+
4
+ end
5
+
6
+ class UndefinedApiKeyError < Error
7
+
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module NovaPoshta
2
+ module Model
3
+ class Address < ::NovaPoshta::Model::Base
4
+
5
+ # you can use params as {find_by_string: 'Одесса'} to filter by city_name
6
+ def cities(params={})
7
+ api.request('getCities', params)
8
+ end
9
+
10
+ def warehouses(city_ref, params={})
11
+ api.request('getWarehouses', {city_ref: city_ref}.merge(params))
12
+ end
13
+
14
+ def areas(params={})
15
+ api.request('getAreas', params)
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ module NovaPoshta
2
+ module Model
3
+ class Base
4
+
5
+ attr_accessor :api
6
+
7
+ def api
8
+ @api ||= Api.new(self.class.name.split('::').last)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ module NovaPoshta
2
+ module Response
3
+ class Area < ::NovaPoshta::Response::Base
4
+
5
+ attr_accessor :description, :ref, :areas_center
6
+
7
+ # ugly API do not provide searching by area reference
8
+ def cities(params={})
9
+ @api.request('getCities', params).select { |city| city.area_ref == self.ref }
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ module NovaPoshta
2
+ module Response
3
+ class Base
4
+
5
+ attr_accessor :api, :result
6
+
7
+ def initialize(attrs)
8
+ attrs.each do |k, v|
9
+ if self.respond_to?("#{k.to_s.underscore}=".to_sym) # map only defined
10
+ self.public_send("#{k.to_s.underscore}=".to_sym, v)
11
+ end
12
+ end
13
+
14
+ yield self if block_given?
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module NovaPoshta
2
+ module Response
3
+ class City < ::NovaPoshta::Response::Base
4
+
5
+ attr_accessor :description, :description_ru, :ref, :delivery1, :delivery2, :delivery3, :delivery4,
6
+ :delivery5, :delivery6, :delivery7, :area_ref, :conglomerates, :city_id
7
+
8
+ def warehouses(params={})
9
+ @api.request('getWarehouses', {city_ref: self.ref}.merge(params))
10
+ end
11
+
12
+ def area(params={})
13
+ @api.request('getAreas', params).select { |area| area.ref == self.area_ref }.first.result
14
+ end
15
+
16
+ # city has area attribute, but it should be area_ref, so next writer will fix this situation
17
+ def area=(area_ref)
18
+ self.area_ref = area_ref
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ module NovaPoshta
2
+ module Response
3
+ class Warehouse < ::NovaPoshta::Response::Base
4
+
5
+ attr_accessor :description, :description_ru, :phone, :type_of_warehouse, :ref, :number, :city_ref,
6
+ :max_weight_allowed, :longitude, :latitude, :reception, :delivery, :schedule,
7
+ :site_key, :city_description, :city_description_ru, :post_finance, :pos_terminal,
8
+ :total_max_weight_allowed, :place_max_weight_allowed
9
+
10
+ # ugly API do not provide searching by city reference
11
+ def city(params={})
12
+ @api.request('getCities', params).select { |city| city.ref == self.city_ref }.first.result
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,54 @@
1
+ module NovaPoshta
2
+ class Result
3
+
4
+ include Enumerable
5
+
6
+ attr_accessor :success, :data, :errors, :warnings, :info
7
+ attr_reader :body, :raw_body
8
+
9
+ def initialize(raw_body, called_method, api)
10
+ @raw_body = raw_body
11
+ @body = JSON.parse(raw_body)
12
+ @called_method = called_method
13
+ @api = api
14
+ end
15
+
16
+ def success?
17
+ body['success']
18
+ end
19
+
20
+ def data
21
+ body['data'].map do |attrs|
22
+ response_class.new(attrs) do |r|
23
+ r.api = @api
24
+ r.result = self
25
+ end
26
+ end if body['data']
27
+ end
28
+
29
+ def errors
30
+ body['errors']
31
+ end
32
+
33
+ def warnings
34
+ body['warnings']
35
+ end
36
+
37
+ def info
38
+ body['info']
39
+ end
40
+
41
+ def each(&block)
42
+ data.each do |member|
43
+ block.call(member)
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ def response_class
50
+ "::NovaPoshta::Response::#{@called_method[3..-1].singularize}".safe_constantize
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module NovaPoshta
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_support/all'
2
+
3
+ # require base classes because the order in this situation makes sense
4
+ require "nova_poshta/response/base"
5
+ require "nova_poshta/model/base"
6
+
7
+ Dir[File.dirname(__FILE__) + '/nova_poshta/*.rb'].each { |f| require f }
8
+ Dir[File.dirname(__FILE__) + '/nova_poshta/response/*.rb'].each { |f| require f }
9
+ Dir[File.dirname(__FILE__) + '/nova_poshta/model/*.rb'].each { |f| require f }
10
+
11
+ module NovaPoshta
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.configure
18
+ yield configuration
19
+ end
20
+
21
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nova_poshta/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nova_poshta"
8
+ spec.version = NovaPoshta::VERSION
9
+ spec.authors = ["Igor Gonchar"]
10
+ spec.email = ["igor.gonchar@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby-client to novaposhta.ua API.}
13
+ spec.homepage = "https://github.com/gigorok/nova_poshta"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "webmock"
26
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nova_poshta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Igor Gonchar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - igor.gonchar@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/nova_poshta.rb
101
+ - lib/nova_poshta/api.rb
102
+ - lib/nova_poshta/configuration.rb
103
+ - lib/nova_poshta/error.rb
104
+ - lib/nova_poshta/model/address.rb
105
+ - lib/nova_poshta/model/base.rb
106
+ - lib/nova_poshta/response/area.rb
107
+ - lib/nova_poshta/response/base.rb
108
+ - lib/nova_poshta/response/city.rb
109
+ - lib/nova_poshta/response/warehouse.rb
110
+ - lib/nova_poshta/result.rb
111
+ - lib/nova_poshta/version.rb
112
+ - nova_poshta.gemspec
113
+ homepage: https://github.com/gigorok/nova_poshta
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.7
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Ruby-client to novaposhta.ua API.
137
+ test_files: []