usps_intelligent_barcode 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +8 -0
  3. data/CHANGELOG.markdown +67 -0
  4. data/Gemfile +13 -0
  5. data/Gemfile.lock +90 -0
  6. data/LICENSE.md +11 -0
  7. data/README.markdown +64 -0
  8. data/Rakefile +40 -0
  9. data/VERSION +1 -0
  10. data/examples/example.rb +17 -0
  11. data/lib/USPS-intelligent-barcode.rb +17 -0
  12. data/lib/USPS-intelligent-barcode/bar_map.rb +52 -0
  13. data/lib/USPS-intelligent-barcode/bar_position.rb +40 -0
  14. data/lib/USPS-intelligent-barcode/bar_symbol.rb +49 -0
  15. data/lib/USPS-intelligent-barcode/bar_to_character_mapping.yml +66 -0
  16. data/lib/USPS-intelligent-barcode/barcode.rb +152 -0
  17. data/lib/USPS-intelligent-barcode/barcode_id.rb +98 -0
  18. data/lib/USPS-intelligent-barcode/character_position.rb +28 -0
  19. data/lib/USPS-intelligent-barcode/codeword_map.rb +38 -0
  20. data/lib/USPS-intelligent-barcode/codeword_to_character_mapping.yml +1366 -0
  21. data/lib/USPS-intelligent-barcode/crc.rb +52 -0
  22. data/lib/USPS-intelligent-barcode/mailer_id.rb +105 -0
  23. data/lib/USPS-intelligent-barcode/numeric_conversions.rb +22 -0
  24. data/lib/USPS-intelligent-barcode/routing_code.rb +134 -0
  25. data/lib/USPS-intelligent-barcode/serial_number.rb +88 -0
  26. data/lib/USPS-intelligent-barcode/service_type.rb +79 -0
  27. data/lib/usps_intelligent_barcode.rb +17 -0
  28. data/lib/usps_intelligent_barcode/bar_map.rb +52 -0
  29. data/lib/usps_intelligent_barcode/bar_position.rb +40 -0
  30. data/lib/usps_intelligent_barcode/bar_symbol.rb +49 -0
  31. data/lib/usps_intelligent_barcode/bar_to_character_mapping.yml +66 -0
  32. data/lib/usps_intelligent_barcode/barcode.rb +152 -0
  33. data/lib/usps_intelligent_barcode/barcode_id.rb +98 -0
  34. data/lib/usps_intelligent_barcode/character_position.rb +28 -0
  35. data/lib/usps_intelligent_barcode/codeword_map.rb +38 -0
  36. data/lib/usps_intelligent_barcode/codeword_to_character_mapping.yml +1366 -0
  37. data/lib/usps_intelligent_barcode/crc.rb +52 -0
  38. data/lib/usps_intelligent_barcode/mailer_id.rb +105 -0
  39. data/lib/usps_intelligent_barcode/numeric_conversions.rb +22 -0
  40. data/lib/usps_intelligent_barcode/routing_code.rb +134 -0
  41. data/lib/usps_intelligent_barcode/serial_number.rb +88 -0
  42. data/lib/usps_intelligent_barcode/service_type.rb +79 -0
  43. data/spec/bar_map_spec.rb +30 -0
  44. data/spec/bar_position_spec.rb +40 -0
  45. data/spec/bar_symbol_spec.rb +39 -0
  46. data/spec/barcode_id_spec.rb +106 -0
  47. data/spec/barcode_spec.rb +213 -0
  48. data/spec/character_position_spec.rb +25 -0
  49. data/spec/codeword_map_spec.rb +22 -0
  50. data/spec/crc_spec.rb +21 -0
  51. data/spec/mailer_id_spec.rb +124 -0
  52. data/spec/numeric_conversions_spec.rb +23 -0
  53. data/spec/routing_code_spec.rb +180 -0
  54. data/spec/serial_number_spec.rb +117 -0
  55. data/spec/service_type_spec.rb +93 -0
  56. data/spec/spec_helper.rb +8 -0
  57. data/usps_intelligent_barcode.gemspec +117 -0
  58. metadata +216 -0
@@ -0,0 +1,22 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module Imb
4
+
5
+ describe CodewordMap do
6
+
7
+ let(:characters) do
8
+ [
9
+ 0x001F, 0x001F, 0x001F, 0x001F, 0x1524,
10
+ 0x01A3, 0x043C, 0x1838, 0x012B, 0x0076,
11
+ ]
12
+ end
13
+ let(:codewords) {[0, 0, 0, 0, 559, 202, 508, 451, 124, 34]}
14
+ let(:codeword_map) {CodewordMap.new}
15
+
16
+ specify do
17
+ expect(codeword_map.characters(codewords)).to eq(characters)
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module Imb
4
+
5
+ describe Crc do
6
+
7
+ describe '#crc' do
8
+
9
+ specify do
10
+ expect(Crc.new.crc(0x016907B2A24ABC16A2E5C004B1)).to eq(0x751)
11
+ end
12
+
13
+ specify do
14
+ expect(Crc.new.crc(0)).to eq(0x6e0)
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,124 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module Imb
4
+
5
+ describe MailerId do
6
+
7
+ describe '::coerce' do
8
+
9
+ subject {MailerId.coerce(o)}
10
+
11
+ context 'MailerId' do
12
+ let(:o) {MailerId.new(12)}
13
+ its(:to_i) {should == 12}
14
+ end
15
+
16
+ context 'String' do
17
+ let(:o) {'12'}
18
+ its(:to_i) {should == 12}
19
+ end
20
+
21
+ context 'Integer' do
22
+ let(:o) {12}
23
+ its(:to_i) {should == 12}
24
+ end
25
+
26
+ context 'unknown' do
27
+ let(:o) {Object.new}
28
+ specify do
29
+ expect {
30
+ MailerId.coerce(o)
31
+ }.to raise_error ArgumentError, 'Cannot coerce to MailerId'
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ describe '#to_i' do
38
+ let(:value) {23}
39
+ subject {MailerId.new(value)}
40
+ its(:to_i) {should == value}
41
+ end
42
+
43
+ describe '#=' do
44
+ def o1 ; MailerId.new(1) ; end
45
+ def o2 ; 1 ; end
46
+ def o3 ; MailerId.new(2) ; end
47
+ def o4 ; Object.new ; end
48
+ specify {expect(o1).to eq(o1)}
49
+ specify {expect(o1).to eq(o2)}
50
+ specify {expect(o1).not_to eq(o3)}
51
+ specify {expect(o1).not_to eq(o4)}
52
+ end
53
+
54
+ describe '#validate' do
55
+
56
+ let(:long_mailer_id?) {double 'long_mailer_id?'}
57
+
58
+ def validate(value)
59
+ MailerId.new(value).validate(long_mailer_id?)
60
+ end
61
+
62
+ def self.is_valid(value)
63
+ context "#{value}" do
64
+ specify {validate(value)}
65
+ end
66
+ end
67
+
68
+ def self.is_out_of_range(value)
69
+ context "#{value}" do
70
+ specify do
71
+ expect {
72
+ validate(value)
73
+ }.to raise_error(ArgumentError,
74
+ 'Must be 0..899999 or 900000000..999999999')
75
+ end
76
+ end
77
+ end
78
+
79
+ is_out_of_range -1
80
+ is_valid 0
81
+ is_valid 899_999
82
+ is_out_of_range 1_000_000
83
+ is_out_of_range 899_999_999
84
+ is_valid 900_000_000
85
+ is_valid 999_999_999
86
+ is_out_of_range 1_000_000_000
87
+
88
+ end
89
+
90
+ describe '#long?' do
91
+
92
+ subject {MailerId.new(value)}
93
+
94
+ context 'short' do
95
+ let(:value) {0}
96
+ its(:long?) {should be_falsey}
97
+ end
98
+
99
+ context 'long' do
100
+ let(:value) {900_000_000}
101
+ its(:long?) {should be_truthy}
102
+ end
103
+
104
+ end
105
+
106
+ describe '#shift_and_add_to' do
107
+
108
+ context 'short' do
109
+ let(:mailer_id) {MailerId.new(999999)}
110
+ let(:long_mailer_id?) {false}
111
+ specify {expect(mailer_id.shift_and_add_to(1, long_mailer_id?)).to eq(1999999)}
112
+ end
113
+
114
+ context 'long' do
115
+ let(:mailer_id) {MailerId.new(999999999)}
116
+ let(:long_mailer_id?) {true}
117
+ specify {expect(mailer_id.shift_and_add_to(1, long_mailer_id?)).to eq(1999999999)}
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+
124
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module Imb
4
+
5
+ describe NumericConversions do
6
+
7
+ include NumericConversions
8
+
9
+ describe '#numeric_to_bytes' do
10
+
11
+ specify do
12
+ expect(numeric_to_bytes(0x1234)).to eq([0x12, 0x34])
13
+ end
14
+
15
+ specify do
16
+ expect(numeric_to_bytes(0x1234, 3)).to eq([0, 0x12, 0x34])
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,180 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module Imb
4
+
5
+ describe RoutingCode do
6
+
7
+ describe '::coerce' do
8
+
9
+ let(:zip) {85308}
10
+ let(:plus4) {1465}
11
+ let(:delivery_point) {12}
12
+
13
+ subject {RoutingCode.coerce(o)}
14
+
15
+ shared_examples 'coerces' do
16
+ its(:zip) {should == zip}
17
+ its(:plus4) {should == plus4}
18
+ its(:delivery_point) {should == delivery_point}
19
+ end
20
+
21
+ context 'RoutingCode' do
22
+ let(:o) {RoutingCode.new(zip, plus4, delivery_point)}
23
+ it_behaves_like 'coerces'
24
+ end
25
+
26
+ context 'nil' do
27
+ let(:o) {nil}
28
+ let(:zip) {nil}
29
+ let(:plus4) {nil}
30
+ let(:delivery_point) {nil}
31
+ it_behaves_like 'coerces'
32
+ end
33
+
34
+ context 'string' do
35
+
36
+ let(:o) {"#{zip}#{plus4}#{delivery_point}"}
37
+
38
+ context 'empty' do
39
+ let(:zip) {nil}
40
+ let(:plus4) {nil}
41
+ let(:delivery_point) {nil}
42
+ it_behaves_like 'coerces'
43
+ end
44
+
45
+ context "zip, plu4, delivery_point" do
46
+ it_behaves_like 'coerces'
47
+ end
48
+
49
+ end
50
+
51
+ context 'array' do
52
+ let(:o) {[zip, plus4, delivery_point]}
53
+ it_behaves_like 'coerces'
54
+ end
55
+
56
+ context 'unrecognized' do
57
+ let(:o) {Object.new}
58
+ specify do
59
+ expect {
60
+ RoutingCode.coerce(o)
61
+ }.to raise_error ArgumentError, 'Cannot coerce to RoutingCode'
62
+ end
63
+ end
64
+
65
+ end
66
+
67
+ describe '#string_to_array' do
68
+
69
+ subject(:array) {RoutingCode.string_to_array(s)}
70
+
71
+ context 'empty' do
72
+ let(:s) {''}
73
+ it {is_expected.to eq([nil, nil, nil])}
74
+ end
75
+
76
+ context 'zip' do
77
+ let(:s) {'85308'}
78
+ it {is_expected.to eq(['85308', nil, nil])}
79
+ end
80
+
81
+ context 'zip, plus4' do
82
+ let(:s) {'853081465'}
83
+ it {is_expected.to eq(['85308', '1465', nil])}
84
+ end
85
+
86
+ context 'zip, plu4, delivery_point' do
87
+ let(:s) {'85308146502'}
88
+ it {is_expected.to eq(['85308', '1465', '02'])}
89
+ end
90
+
91
+ context 'non-digits' do
92
+ let(:s) {'(85308 1465 02)'}
93
+ it {is_expected.to eq(['85308', '1465', '02'])}
94
+ end
95
+
96
+ context 'incorrect length' do
97
+ let(:s) {'1'}
98
+ specify do
99
+ expect {
100
+ array
101
+ }.to raise_error ArgumentError, 'Bad routing code: "1"'
102
+ end
103
+ end
104
+
105
+ end
106
+
107
+ describe '#convert' do
108
+
109
+ subject do
110
+ RoutingCode.new(zip, plus4, delivery_point)
111
+ end
112
+
113
+ context 'empty' do
114
+ let(:zip) {nil}
115
+ let(:plus4) {nil}
116
+ let(:delivery_point) {nil}
117
+ its(:convert) {should == 0}
118
+ end
119
+
120
+ context 'zip' do
121
+ let(:zip) {85308}
122
+ let(:plus4) {nil}
123
+ let(:delivery_point) {nil}
124
+ its(:convert) {should == 85309}
125
+ end
126
+
127
+ context 'zip, plus4' do
128
+ let(:zip) {85308}
129
+ let(:plus4) {1465}
130
+ let(:delivery_point) {nil}
131
+ its(:convert) {should == 853181466}
132
+ end
133
+
134
+ context 'zip, plus4, delivery_point' do
135
+ let(:zip) {85308}
136
+ let(:plus4) {1465}
137
+ let(:delivery_point) {2}
138
+ its(:convert) {should == 86308246503}
139
+ end
140
+
141
+ end
142
+
143
+ describe '#to_a' do
144
+ let(:zip) {85308}
145
+ let(:plus4) {1465}
146
+ let(:delivery_point) {2}
147
+ subject {RoutingCode.new(zip, plus4, delivery_point)}
148
+ its(:to_a) {should == [zip, plus4, delivery_point]}
149
+ end
150
+
151
+ describe '#==' do
152
+ def o1 ; RoutingCode.new(85308, 1465, 1) ; end
153
+ def o2 ; [85308, 1465, 1] ; end
154
+ def o3 ; RoutingCode.new(85308, 1465, 2) ; end
155
+ def o4 ; RoutingCode.new(85308, 1466, 1) ; end
156
+ def o5 ; RoutingCode.new(85309, 1465, 1) ; end
157
+ def o6 ; Object.new ; end
158
+ specify {expect(o1).to eq(o1)}
159
+ specify {expect(o1).to eq(o2)}
160
+ specify {expect(o1).not_to eq(o3)}
161
+ specify {expect(o1).not_to eq(o4)}
162
+ specify {expect(o1).not_to eq(o5)}
163
+ specify {expect(o1).not_to eq(o6)}
164
+ end
165
+
166
+ describe '#validate' do
167
+ let(:routing_code) {RoutingCode.new(nil, nil, nil)}
168
+ let(:long_mailer_id?) {double 'long_mailer_id?'}
169
+ specify {routing_code.validate(long_mailer_id?)}
170
+ end
171
+
172
+ describe '#shift_and_add_to' do
173
+ let(:routing_code) {RoutingCode.new(85308, 1465, 2)}
174
+ let(:long_mailer_id?) {double 'long mailer id'}
175
+ specify {expect(routing_code.shift_and_add_to(1, long_mailer_id?)).to eq(186308246503)}
176
+ end
177
+
178
+ end
179
+
180
+ end
@@ -0,0 +1,117 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ module Imb
4
+
5
+ describe SerialNumber do
6
+
7
+ describe '::coerce' do
8
+
9
+ subject {SerialNumber.coerce(o)}
10
+
11
+ context 'SerialNumber' do
12
+ let(:o) {SerialNumber.new(12)}
13
+ its(:to_i) {should == 12}
14
+ end
15
+
16
+ context 'String' do
17
+ let(:o) {'12'}
18
+ its(:to_i) {should == 12}
19
+ end
20
+
21
+ context 'Integer' do
22
+ let(:o) {12}
23
+ its(:to_i) {should == 12}
24
+ end
25
+
26
+ context 'unknown' do
27
+ let(:o) {Object.new}
28
+ specify do
29
+ expect {
30
+ SerialNumber.coerce(o)
31
+ }.to raise_error ArgumentError, 'Cannot coerce to SerialNumber'
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ describe '#to_i' do
38
+ let(:value) {23}
39
+ subject {SerialNumber.new(value)}
40
+ its(:to_i) {should == value}
41
+ end
42
+
43
+ describe '#=' do
44
+ def o1 ; SerialNumber.new(1) ; end
45
+ def o2 ; 1 ; end
46
+ def o3 ; SerialNumber.new(2) ; end
47
+ def o4 ; Object.new ; end
48
+ specify {expect(o1).to eq(o1)}
49
+ specify {expect(o1).to eq(o2)}
50
+ specify {expect(o1).not_to eq(o3)}
51
+ specify {expect(o1).not_to eq(o4)}
52
+ end
53
+
54
+ describe '#validate' do
55
+
56
+ def self.is_valid(value)
57
+ context "#{value}" do
58
+ let(:serial_number) {SerialNumber.new(value)}
59
+ specify {serial_number.validate(long_mailer_id?)}
60
+ end
61
+ end
62
+
63
+ def self.is_out_of_range(value)
64
+ context "#{value}" do
65
+ let(:serial_number) {SerialNumber.new(value)}
66
+ specify do
67
+ expect {
68
+ serial_number.validate(long_mailer_id?)
69
+ }.to raise_error ArgumentError, message
70
+ end
71
+ end
72
+ end
73
+
74
+ context 'when long mailer id' do
75
+ let(:long_mailer_id?) {true}
76
+ let(:message) {'Must be 0..999999'}
77
+ is_out_of_range -1
78
+ is_valid 0
79
+ is_valid 999_999
80
+ is_out_of_range 1_000_000
81
+ end
82
+
83
+ context 'when short mailer id' do
84
+ let(:long_mailer_id?) {false}
85
+ let(:message) {'Must be 0..999999999'}
86
+ is_out_of_range -1
87
+ is_valid 0
88
+ is_valid 999_999_999
89
+ is_out_of_range 1_000_000_000
90
+ end
91
+
92
+ end
93
+
94
+ describe '#shift_and_add_to' do
95
+
96
+ let(:serial_number) {SerialNumber.new(value)}
97
+
98
+ def shift_and_add_to
99
+ serial_number.shift_and_add_to(1, long_mailer_id?)
100
+ end
101
+
102
+ context 'long mailer id' do
103
+ let(:value) {999_999}
104
+ let(:long_mailer_id?) {true}
105
+ specify {expect(shift_and_add_to).to eq(1_999_999)}
106
+ end
107
+
108
+ context 'short mailer id' do
109
+ let(:value) {999_999_999}
110
+ let(:long_mailer_id?) {false}
111
+ specify {expect(shift_and_add_to).to eq(1_999_999_999)}
112
+ end
113
+ end
114
+
115
+ end
116
+
117
+ end