russianpost 0.5.1 → 0.6.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 +4 -4
- data/README.md +2 -2
- data/lib/russianpost/barcode.rb +8 -19
- data/lib/russianpost/barcode/base.rb +30 -0
- data/lib/russianpost/barcode/domestic.rb +18 -0
- data/lib/russianpost/barcode/international.rb +23 -0
- data/lib/russianpost/barcode/invalid.rb +11 -0
- data/lib/russianpost/version.rb +1 -1
- data/test/russianpost/barcode_test.rb +1 -1
- metadata +6 -3
- data/lib/russianpost/barcode_validator.rb +0 -46
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47bbb1af0b21cfb7f4fb2caa809e2cf4f999deb8
|
4
|
+
data.tar.gz: 9d959970148f6bb4ddb7dc438433f0f2cb7fa580
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4132cfbcb85e7d41cf74653cc0f70681e723432264508d2ebfe54a0bcbaa30a4761849dca92166d5b5b65df8fa09e2d7a291b71e0e841c1d1be7f06c41b70e8
|
7
|
+
data.tar.gz: 9bf5df8b9ad495761182fcee06d3ebdd3e9d0d80bbea77b4e195c0d97b5dba85a7a63fc48622536cac1295355c30cfdb99674ed4abecd191d93cbfde465a6eef
|
data/README.md
CHANGED
@@ -14,9 +14,9 @@ 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.
|
17
|
+
gem "russianpost", "~> 0.6.0"
|
18
18
|
|
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
|
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
21
|
gem "curb"
|
22
22
|
gem "russianpost"
|
data/lib/russianpost/barcode.rb
CHANGED
@@ -1,24 +1,13 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'russianpost/barcode/international'
|
2
|
+
require 'russianpost/barcode/domestic'
|
3
|
+
require 'russianpost/barcode/invalid'
|
3
4
|
|
4
5
|
module RussianPost
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
attr_reader :barcode
|
11
|
-
|
12
|
-
def initialize(barcode)
|
13
|
-
@barcode = barcode.strip.upcase
|
14
|
-
end
|
15
|
-
|
16
|
-
def to_s
|
17
|
-
barcode
|
18
|
-
end
|
19
|
-
|
20
|
-
def valid?
|
21
|
-
BarcodeValidator.validate(barcode)
|
6
|
+
module Barcode
|
7
|
+
def self.new(barcode)
|
8
|
+
[International, Domestic, Invalid].each do |klass|
|
9
|
+
return klass.new(barcode) if barcode =~ klass.format
|
10
|
+
end
|
22
11
|
end
|
23
12
|
end
|
24
13
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
|
3
|
+
module RussianPost
|
4
|
+
module Barcode
|
5
|
+
class Base
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegator :barcode, :=~
|
9
|
+
|
10
|
+
attr_reader :barcode, :digits
|
11
|
+
|
12
|
+
def self.format
|
13
|
+
/.*/
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(barcode)
|
17
|
+
@barcode = barcode.strip.upcase
|
18
|
+
@digits = barcode.scan(/\d/).map { |d| d.to_i }
|
19
|
+
end
|
20
|
+
|
21
|
+
def valid?
|
22
|
+
barcode =~ self.class.format && digits.last == checkdigit
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
barcode
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'russianpost/barcode/base'
|
2
|
+
|
3
|
+
module RussianPost
|
4
|
+
module Barcode
|
5
|
+
class Domestic < Base
|
6
|
+
def self.format
|
7
|
+
/\A\d{14}\Z/
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def checkdigit
|
13
|
+
odds, evens = *digits.partition.with_index { |d, i| (i&1).zero? }
|
14
|
+
(10 - ((odds.reduce(:+) * 3 + evens[0..-2].reduce(:+)) % 10)) % 10
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'russianpost/barcode/base'
|
2
|
+
|
3
|
+
module RussianPost
|
4
|
+
module Barcode
|
5
|
+
class International < Base
|
6
|
+
WEIGHT_FACTORS = [8, 6, 4, 2, 3, 5, 9, 7, 0]
|
7
|
+
|
8
|
+
def self.format
|
9
|
+
/\A([A-Z]{2}\d{9}[A-Z]{2})\Z/
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def checkdigit
|
15
|
+
checksum < 11 ? checksum % 10 : 5
|
16
|
+
end
|
17
|
+
|
18
|
+
def checksum
|
19
|
+
11 - (digits.zip(WEIGHT_FACTORS).map{|i| i.reduce(:*)}.reduce(:+) % 11)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/russianpost/version.rb
CHANGED
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.
|
4
|
+
version: 0.6.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-
|
11
|
+
date: 2013-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -125,7 +125,10 @@ files:
|
|
125
125
|
- Rakefile
|
126
126
|
- lib/russianpost.rb
|
127
127
|
- lib/russianpost/barcode.rb
|
128
|
-
- lib/russianpost/
|
128
|
+
- lib/russianpost/barcode/base.rb
|
129
|
+
- lib/russianpost/barcode/domestic.rb
|
130
|
+
- lib/russianpost/barcode/international.rb
|
131
|
+
- lib/russianpost/barcode/invalid.rb
|
129
132
|
- lib/russianpost/client.rb
|
130
133
|
- lib/russianpost/country_factory.rb
|
131
134
|
- lib/russianpost/operation.rb
|
@@ -1,46 +0,0 @@
|
|
1
|
-
module RussianPost
|
2
|
-
class BarcodeValidator
|
3
|
-
attr_reader :barcode, :digits
|
4
|
-
|
5
|
-
INTERNATIONAL_FORMAT = /\A([A-Z]{2}\d{9}[A-Z]{2})\Z/
|
6
|
-
DOMESTIC_FORMAT = /\A(\d{14})\Z/
|
7
|
-
WEIGHT_FACTORS = [8, 6, 4, 2, 3, 5, 9, 7, 0]
|
8
|
-
|
9
|
-
def self.validate(barcode)
|
10
|
-
new(barcode).valid?
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(barcode)
|
14
|
-
@barcode = barcode
|
15
|
-
@digits = barcode[/\d+/].split("").map { |d| d.to_i }
|
16
|
-
end
|
17
|
-
|
18
|
-
def valid?
|
19
|
-
valid_international? || valid_domestic?
|
20
|
-
end
|
21
|
-
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def valid_international?
|
26
|
-
(barcode =~ INTERNATIONAL_FORMAT && digits.last == checkdigit) || false
|
27
|
-
end
|
28
|
-
|
29
|
-
def valid_domestic?
|
30
|
-
barcode =~ DOMESTIC_FORMAT || false
|
31
|
-
end
|
32
|
-
|
33
|
-
def checkdigit
|
34
|
-
if (1..9).include? checksum
|
35
|
-
checksum
|
36
|
-
else
|
37
|
-
checksum == 10 ? 0 : 5
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def checksum
|
42
|
-
product = digits.zip(WEIGHT_FACTORS)
|
43
|
-
(11 - product.map{|i| i.reduce(:*)}.reduce(:+)) % 11
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|