russianpost 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47bbb1af0b21cfb7f4fb2caa809e2cf4f999deb8
4
- data.tar.gz: 9d959970148f6bb4ddb7dc438433f0f2cb7fa580
3
+ metadata.gz: 5a33521adb8e7e20a979b5f101b9d47c87fa0e95
4
+ data.tar.gz: 71563d96c3fa637fd2c7dbbaca8f7e98f718ff1b
5
5
  SHA512:
6
- metadata.gz: a4132cfbcb85e7d41cf74653cc0f70681e723432264508d2ebfe54a0bcbaa30a4761849dca92166d5b5b65df8fa09e2d7a291b71e0e841c1d1be7f06c41b70e8
7
- data.tar.gz: 9bf5df8b9ad495761182fcee06d3ebdd3e9d0d80bbea77b4e195c0d97b5dba85a7a63fc48622536cac1295355c30cfdb99674ed4abecd191d93cbfde465a6eef
6
+ metadata.gz: 751bd6b261ef027001fbc32546b717ce23f767fd58b8b852abcf385b414410e8b252bb8abdf10a8d978df03aea762c17755e46d08cbbafee974a500c66eb91b4
7
+ data.tar.gz: 78b55dbd115fefc61be53b0c9a7a8ac98f19cbaf23648f9c714366d3b6539ddd978d021dd4b93bb3e0ec73c4906698945026c34e3b75fb290fbc3ed28509b995
data/README.md CHANGED
@@ -14,20 +14,20 @@ To install gem stand-alone:
14
14
 
15
15
  To use gem in a Rails app, add the following to your `Gemfile`:
16
16
 
17
- gem "russianpost", "~> 0.6.0"
17
+ gem 'russianpost', '~> 0.7.0'
18
18
 
19
19
  This gem uses [Savon](http://savonrb.com/), which in turn uses [HTTPI](https://github.com/savonrb/httpi) internally. HTTPI chooses the best HTTP library of those you have installed. For the fastest results add [Curb](https://github.com/taf2/curb) to your `Gemfile`:
20
20
 
21
- gem "curb"
22
- gem "russianpost"
21
+ gem 'curb'
22
+ gem 'russianpost'
23
23
 
24
- If you use RussianPost outside Rails, `require "curb"` before requiring RussianPost.
24
+ If you use RussianPost outside Rails, `require 'curb'` before requiring RussianPost.
25
25
 
26
26
  ## Usage
27
27
 
28
28
  Initialize a parcel object, passing the package barcode to the constructor:
29
29
 
30
- parcel = RussianPost::Parcel.new("EC123456789RU")
30
+ parcel = RussianPost::Parcel.new('EC123456789RU')
31
31
 
32
32
  Fetch operation history for the parcel:
33
33
 
@@ -54,6 +54,16 @@ RussianPost makes guesses about current state of the parcel.
54
54
  # Type
55
55
  parcel.type
56
56
 
57
+ ### Barcode validator
58
+
59
+ You can use the included barcode validator separately. It not only validates the format of a barcode, but also calculates check digits and tests barcodes against them.
60
+
61
+ barcode = RussianPost::Barcode.new('EC123456789RU')
62
+ barcode.valid? #=> false
63
+
64
+ barcode = RussianPost::Barcode.new('RD025500807SE')
65
+ barcode.valid? #=> true
66
+
57
67
  ### Operations API
58
68
 
59
69
  Each `Operation` responds to the following methods. If there is no information, methods return `nil`. See the [wiki](https://github.com/artemshitov/russianpost/wiki) for a list of what else can be returned.
data/Rakefile CHANGED
@@ -1,9 +1,9 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
3
 
4
4
  task default: :test
5
5
 
6
6
  Rake::TestTask.new do |t|
7
- t.libs << "test"
7
+ t.libs << 'test'
8
8
  t.test_files = FileList['test/*_test.rb', 'test/*/*_test.rb']
9
9
  end
@@ -1,5 +1,5 @@
1
- require "russianpost/version"
2
- require "russianpost/parcel"
1
+ require 'russianpost/version'
2
+ require 'russianpost/parcel'
3
3
 
4
4
  module Russianpost
5
5
  end
@@ -1,4 +1,4 @@
1
- require "forwardable"
1
+ require 'forwardable'
2
2
 
3
3
  module RussianPost
4
4
  module Barcode
@@ -19,7 +19,7 @@ module RussianPost
19
19
  end
20
20
 
21
21
  def valid?
22
- barcode =~ self.class.format && digits.last == checkdigit
22
+ (barcode =~ self.class.format || false) && digits.last == checkdigit
23
23
  end
24
24
 
25
25
  def to_s
@@ -1,4 +1,5 @@
1
1
  require 'russianpost/barcode/base'
2
+ require 'iso3166_ru'
2
3
 
3
4
  module RussianPost
4
5
  module Barcode
@@ -9,8 +10,16 @@ module RussianPost
9
10
  /\A([A-Z]{2}\d{9}[A-Z]{2})\Z/
10
11
  end
11
12
 
13
+ def valid?
14
+ super && country_exists?
15
+ end
16
+
12
17
  private
13
18
 
19
+ def country_exists?
20
+ !Iso3166Ru.find_by(alpha2: barcode[-2..-1]).nil?
21
+ end
22
+
14
23
  def checkdigit
15
24
  checksum < 11 ? checksum % 10 : 5
16
25
  end
@@ -1,11 +1,14 @@
1
- require "savon"
1
+ require 'savon'
2
+ require 'singleton'
2
3
 
3
4
  module RussianPost
4
5
  class Client
6
+ include Singleton
7
+
5
8
  attr_reader :savon, :endpoint, :namespace
6
9
 
7
- ENDPOINT = "http://voh.russianpost.ru:8080/niips-operationhistory-web/OperationHistory"
8
- NAMESPACE = "http://russianpost.org/operationhistory/data"
10
+ ENDPOINT = 'http://voh.russianpost.ru:8080/niips-operationhistory-web/OperationHistory'
11
+ NAMESPACE = 'http://russianpost.org/operationhistory/data'
9
12
 
10
13
  def initialize(opts = {})
11
14
  @savon = Savon.client(
@@ -16,8 +19,8 @@ module RussianPost
16
19
  end
17
20
 
18
21
  def call(opts = {barcode: nil})
19
- message = { "wsdl:Barcode" => opts[:barcode], "wsdl:MessageType" => "0" }
20
- response = savon.call("OperationHistoryRequest", message: message)
22
+ message = { 'wsdl:Barcode' => opts[:barcode], 'wsdl:MessageType' => '0' }
23
+ response = savon.call('OperationHistoryRequest', message: message)
21
24
  result = response.to_hash[:operation_history_data][:history_record]
22
25
 
23
26
  result.kind_of?(Array) ? result : [result].compact
@@ -1,10 +1,10 @@
1
- require "iso3166_ru"
2
- require "russianpost/operation"
1
+ require 'iso3166_ru'
2
+ require 'russianpost/operation'
3
3
 
4
4
  module RussianPost
5
5
  module CountryFactory
6
6
  def self.build(country_config, opts = {})
7
- country_list = opts[:country_list] || Iso3166Ru::CountryList.new
7
+ country_list = opts[:country_list] || Iso3166Ru
8
8
  proxy = country_list.find_by(iso: country_config[:id])
9
9
 
10
10
  RussianPost::Country.new(
@@ -1,12 +1,12 @@
1
- require "iso3166_ru"
2
- require "russianpost/operation"
3
- require "russianpost/country_factory"
1
+ require 'iso3166_ru'
2
+ require 'russianpost/operation'
3
+ require 'russianpost/country_factory'
4
4
 
5
5
  module RussianPost
6
6
  module OperationsFactory
7
7
  class << self
8
8
  def build(operations_hash)
9
- @country_list = Iso3166Ru::CountryList.new
9
+ @country_list = Iso3166Ru
10
10
  operations_hash.map { |o| build_operation(o) }
11
11
  end
12
12
 
@@ -1,6 +1,6 @@
1
- require "russianpost/client"
2
- require "russianpost/operations_factory"
3
- require "russianpost/barcode"
1
+ require 'russianpost/client'
2
+ require 'russianpost/operations_factory'
3
+ require 'russianpost/barcode'
4
4
 
5
5
  module RussianPost
6
6
  class Parcel
@@ -8,7 +8,7 @@ module RussianPost
8
8
 
9
9
  def initialize(barcode, opts = {})
10
10
  @barcode = Barcode.new(barcode)
11
- @client = (opts[:client] || Client).new
11
+ @client = (opts[:client] || Client).instance
12
12
  end
13
13
 
14
14
  def operations
@@ -1,3 +1,3 @@
1
1
  module Russianpost
2
- VERSION = "0.6.0"
2
+ VERSION = '0.7.0'
3
3
  end
@@ -4,25 +4,25 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'russianpost/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "russianpost"
7
+ spec.name = 'russianpost'
8
8
  spec.version = Russianpost::VERSION
9
- spec.authors = ["Artem Shitov"]
10
- spec.email = ["inbox@artemshitov.ru"]
9
+ spec.authors = ['Artem Shitov']
10
+ spec.email = ['inbox@artemshitov.ru']
11
11
  spec.description = %q{Thin wrapper around Russian Post package tracking SOAP API. Works on a per-package basis (contrary to the bulk ticket-based API). Use it at your own risk, since the API may appear unstable and require authorization in future.}
12
12
  spec.summary = %q{Russian Post package tracking API client}
13
- spec.homepage = "https://github.com/artemshitov/russianpost"
14
- spec.license = "MIT"
13
+ spec.homepage = 'https://github.com/artemshitov/russianpost'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "webmock"
24
- spec.add_development_dependency "vcr", "~> 2.4.0"
25
- spec.add_development_dependency "minitest", "~> 5.0.0"
26
- spec.add_dependency "savon", "~> 2.1.0"
27
- spec.add_dependency "iso3166_ru", "~>0.1.2"
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'webmock'
24
+ spec.add_development_dependency 'vcr', '~> 2.4.0'
25
+ spec.add_development_dependency 'minitest', '~> 5.0.0'
26
+ spec.add_dependency 'savon', '~> 2.1.0'
27
+ spec.add_dependency 'iso3166_ru', '~> 0.2.0'
28
28
  end
@@ -1,19 +1,19 @@
1
- require "test_helper"
2
- require "russianpost/barcode"
1
+ require 'test_helper'
2
+ require 'russianpost/barcode'
3
3
 
4
4
  module RussianPost
5
5
  class BarcodeTest < Minitest::Test
6
6
  def test_implicitly_converts_to_string
7
- barcode = Barcode.new("RD025500807SE")
8
- assert_equal "bar RD025500807SE", "bar #{barcode}"
7
+ barcode = Barcode.new('RD025500807SE')
8
+ assert_equal 'bar RD025500807SE', "bar #{barcode}"
9
9
  end
10
10
 
11
11
  def test_validates_barcode
12
- ["123", "RR123456789EE"].each do |barcode|
12
+ ['123', 'RR123456789EE', 'RD025500807SP'].each do |barcode|
13
13
  refute Barcode.new(barcode).valid?
14
14
  end
15
15
 
16
- ["RD025500807SE", "62009147017544"].each do |barcode|
16
+ ['RD025500807SE', '62009147017544'].each do |barcode|
17
17
  assert Barcode.new(barcode).valid?
18
18
  end
19
19
  end
@@ -1,13 +1,13 @@
1
- require "test_helper"
2
- require "russianpost/client"
1
+ require 'test_helper'
2
+ require 'russianpost/client'
3
3
 
4
4
  class TestClient < Minitest::Test
5
5
  def setup
6
- @client = RussianPost::Client.new
6
+ @client = RussianPost::Client.instance
7
7
  end
8
8
 
9
9
  def test_returns_array_of_hashes_on_existing_parcel
10
- ["RD025500807SE", "LK035658376US"].each do |code|
10
+ ['RD025500807SE', 'LK035658376US'].each do |code|
11
11
  VCR.use_cassette(code) do
12
12
  response = @client.call(barcode: code)
13
13
  assert_kind_of Array, response
@@ -17,8 +17,8 @@ class TestClient < Minitest::Test
17
17
  end
18
18
 
19
19
  def test_returns_empty_array_on_nonexistent_parcel
20
- VCR.use_cassette("RR123456789EE") do
21
- response = @client.call(barcode: "RR123456789EE")
20
+ VCR.use_cassette('RR123456789EE') do
21
+ response = @client.call(barcode: 'RR123456789EE')
22
22
  assert_kind_of Array, response
23
23
  assert_empty response
24
24
  end
@@ -1,21 +1,21 @@
1
1
  # encoding: utf-8
2
2
 
3
- require "test_helper"
4
- require "russianpost/parcel"
3
+ require 'test_helper'
4
+ require 'russianpost/parcel'
5
5
 
6
6
  module RussianPost
7
7
  class ParcelTest < Minitest::Test
8
8
  def test_returns_array_of_operations
9
- VCR.use_cassette("RD025500807SE") do
10
- parcel = Parcel.new("RD025500807SE")
9
+ VCR.use_cassette('RD025500807SE') do
10
+ parcel = Parcel.new('RD025500807SE')
11
11
  assert_kind_of Array, parcel.operations
12
12
  assert_kind_of Operation, parcel.operations[0]
13
13
  end
14
14
  end
15
15
 
16
16
  def test_returns_empty_array_on_nonexistent_parcel
17
- VCR.use_cassette("RR123456785EE") do
18
- parcel = Parcel.new("RR123456785EE")
17
+ VCR.use_cassette('RR123456785EE') do
18
+ parcel = Parcel.new('RR123456785EE')
19
19
  assert_kind_of Array, parcel.operations
20
20
  assert parcel.operations.empty?
21
21
  end
@@ -23,7 +23,7 @@ module RussianPost
23
23
 
24
24
  def test_raises_error_on_invalid_barcode
25
25
  assert_raises InvalidBarcode do
26
- Parcel.new("123").operations
26
+ Parcel.new('123').operations
27
27
  end
28
28
  end
29
29
  end
@@ -32,14 +32,14 @@ module RussianPost
32
32
  attr_reader :parcel
33
33
 
34
34
  def setup
35
- VCR.use_cassette("RD025500807SE") do
36
- @parcel = Parcel.new("RD025500807SE")
35
+ VCR.use_cassette('RD025500807SE') do
36
+ @parcel = Parcel.new('RD025500807SE')
37
37
  parcel.operations
38
38
  end
39
39
  end
40
40
 
41
41
  def test_knows_current_location
42
- location = Address.new("127576", "Москва 576")
42
+ location = Address.new('127576', 'Москва 576')
43
43
  assert_equal location, parcel.location
44
44
  end
45
45
 
@@ -48,22 +48,22 @@ module RussianPost
48
48
  end
49
49
 
50
50
  def test_knows_type
51
- type = GenericOperationParameter.new(5, "Мелкий пакет")
51
+ type = GenericOperationParameter.new(5, 'Мелкий пакет')
52
52
  assert_equal type, parcel.type
53
53
  end
54
54
 
55
55
  def test_knows_rank
56
- rank = GenericOperationParameter.new(0, "Без разряда")
56
+ rank = GenericOperationParameter.new(0, 'Без разряда')
57
57
  assert_equal rank, parcel.rank
58
58
  end
59
59
 
60
60
  def test_knows_recipient
61
- assert_equal "ЕЛЕНА", parcel.recipient
61
+ assert_equal 'ЕЛЕНА', parcel.recipient
62
62
  end
63
63
 
64
64
  def test_meta_methods_dont_fail_when_no_operations
65
- VCR.use_cassette("RR123456785EE") do
66
- parcel = Parcel.new("RR123456785EE")
65
+ VCR.use_cassette('RR123456785EE') do
66
+ parcel = Parcel.new('RR123456785EE')
67
67
  [:location, :mass, :type, :rank, :recipient].each do |m|
68
68
  assert_nil parcel.send(m)
69
69
  end
@@ -1,5 +1,5 @@
1
- require "minitest/autorun"
2
- require "vcr"
1
+ require 'minitest/autorun'
2
+ require 'vcr'
3
3
 
4
4
  VCR.configure do |c|
5
5
  c.cassette_library_dir = 'test/fixtures/vcr_cassettes'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: russianpost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Shitov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-30 00:00:00.000000000 Z
11
+ date: 2013-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - ~>
102
102
  - !ruby/object:Gem::Version
103
- version: 0.1.2
103
+ version: 0.2.0
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - ~>
109
109
  - !ruby/object:Gem::Version
110
- version: 0.1.2
110
+ version: 0.2.0
111
111
  description: Thin wrapper around Russian Post package tracking SOAP API. Works on
112
112
  a per-package basis (contrary to the bulk ticket-based API). Use it at your own
113
113
  risk, since the API may appear unstable and require authorization in future.