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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 239fb7cda63e325a8d915502dbdc1c067d91fde0
4
+ data.tar.gz: 8e356a3daa59ced936401d0a0c8e61a4ec3ace98
5
+ SHA512:
6
+ metadata.gz: 4246dcea3fdfd322e15f0a3cae0a52e94caa66db9f467c1e48964f6e2b12ff4066541ca4aeabddb962a4ee5d9dd055af0a2a87d372376fb0b9ce45483813b77e
7
+ data.tar.gz: 232ac8d641c696b678eac49bf6a7bc7d763fcc67c25c0926ebef445f10663b3f4bc290502de5c7f73302c661e38a8aad8dc2972b58bd390adc180f63509ae76f
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - rbx-2.5
4
+ - jruby-1.7
5
+ - ruby-1.9.3
6
+ - ruby-2.0
7
+ - ruby-2.1
8
+ - ruby-2.2
@@ -0,0 +1,67 @@
1
+ # 0.3.0
2
+
3
+ * Ruby 2.1 and 2.2 supported
4
+ * Ruby 1.8.7 no longer supported
5
+ * Renamed gem from USPS-intelligent-barcode to usps_intelligent_barcode
6
+
7
+ # 0.2.5
8
+
9
+ Enhancements
10
+
11
+ * Support MRI 2.0
12
+
13
+ # 0.2.4
14
+
15
+ Bug fixes
16
+
17
+ * Correct author's email address
18
+
19
+ Enhancements
20
+
21
+ * Refactoring
22
+
23
+ # 0.2.3
24
+
25
+ Enhancements
26
+
27
+ * Switch docs from rdoc to yard
28
+
29
+ # 0.2.2
30
+
31
+ Enhancements
32
+
33
+ * Added code coverage to test suite
34
+
35
+ # 0.2.1
36
+
37
+ * Removed autoload
38
+
39
+ # 0.2.0
40
+
41
+ Enhancements
42
+
43
+ * Many comments
44
+ * Refactoring
45
+
46
+ # 0.1.2
47
+
48
+ Bug fixes
49
+
50
+ * Corrected Ryan Taylor Long's name in README
51
+
52
+ # 0.1.1
53
+
54
+ Bug fixes
55
+
56
+ * Correct example in README
57
+ * Fix example for 1.8.7
58
+
59
+ API Changes
60
+
61
+ * Renamed imb to USPS-intelligent-barcode to be consistent with gem name
62
+
63
+ # 0.1.0
64
+
65
+ Enhancements
66
+
67
+ * Turned into a gem
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'andand', '~> 1.3'
4
+ gem 'memoizer', '~> 1.0'
5
+
6
+ group :development do
7
+ gem 'jeweler', '~> 2.0'
8
+ gem 'rspec', '~> 3.3'
9
+ gem 'rspec-its', '~> 1.0'
10
+ gem 'simplecov', '~> 0.10.0'
11
+ gem 'yard', '~> 0.8.5'
12
+ gem 'rake', '~> 10.0'
13
+ end
@@ -0,0 +1,90 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.8)
5
+ andand (1.3.3)
6
+ builder (3.2.2)
7
+ descendants_tracker (0.0.4)
8
+ thread_safe (~> 0.3, >= 0.3.1)
9
+ diff-lcs (1.2.5)
10
+ docile (1.1.5)
11
+ faraday (0.9.1)
12
+ multipart-post (>= 1.2, < 3)
13
+ git (1.2.9.1)
14
+ github_api (0.12.3)
15
+ addressable (~> 2.3)
16
+ descendants_tracker (~> 0.0.4)
17
+ faraday (~> 0.8, < 0.10)
18
+ hashie (>= 3.3)
19
+ multi_json (>= 1.7.5, < 2.0)
20
+ nokogiri (~> 1.6.3)
21
+ oauth2
22
+ hashie (3.4.2)
23
+ highline (1.7.2)
24
+ jeweler (2.0.1)
25
+ builder
26
+ bundler (>= 1.0)
27
+ git (>= 1.2.5)
28
+ github_api
29
+ highline (>= 1.6.15)
30
+ nokogiri (>= 1.5.10)
31
+ rake
32
+ rdoc
33
+ json (1.8.3)
34
+ jwt (1.5.0)
35
+ memoizer (1.0.1)
36
+ mini_portile (0.6.2)
37
+ multi_json (1.11.1)
38
+ multi_xml (0.5.5)
39
+ multipart-post (2.0.0)
40
+ nokogiri (1.6.6.2)
41
+ mini_portile (~> 0.6.0)
42
+ nokogiri (1.6.6.2-java)
43
+ oauth2 (1.0.0)
44
+ faraday (>= 0.8, < 0.10)
45
+ jwt (~> 1.0)
46
+ multi_json (~> 1.3)
47
+ multi_xml (~> 0.5)
48
+ rack (~> 1.2)
49
+ rack (1.6.1)
50
+ rake (10.4.2)
51
+ rdoc (4.2.0)
52
+ json (~> 1.4)
53
+ rspec (3.3.0)
54
+ rspec-core (~> 3.3.0)
55
+ rspec-expectations (~> 3.3.0)
56
+ rspec-mocks (~> 3.3.0)
57
+ rspec-core (3.3.0)
58
+ rspec-support (~> 3.3.0)
59
+ rspec-expectations (3.3.0)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.3.0)
62
+ rspec-its (1.2.0)
63
+ rspec-core (>= 3.0.0)
64
+ rspec-expectations (>= 3.0.0)
65
+ rspec-mocks (3.3.0)
66
+ diff-lcs (>= 1.2.0, < 2.0)
67
+ rspec-support (~> 3.3.0)
68
+ rspec-support (3.3.0)
69
+ simplecov (0.10.0)
70
+ docile (~> 1.1.0)
71
+ json (~> 1.8)
72
+ simplecov-html (~> 0.10.0)
73
+ simplecov-html (0.10.0)
74
+ thread_safe (0.3.5)
75
+ thread_safe (0.3.5-java)
76
+ yard (0.8.7.6)
77
+
78
+ PLATFORMS
79
+ java
80
+ ruby
81
+
82
+ DEPENDENCIES
83
+ andand (~> 1.3)
84
+ jeweler (~> 2.0)
85
+ memoizer (~> 1.0)
86
+ rake (~> 10.0)
87
+ rspec (~> 3.3)
88
+ rspec-its (~> 1.0)
89
+ simplecov (~> 0.10.0)
90
+ yard (~> 0.8.5)
@@ -0,0 +1,11 @@
1
+ Copyright 2012 Ryan Taylor Long
2
+ Copyright 2012 Wayne Conrad
3
+
4
+ This software is distributed under the [MIT License](http://opensource.org/licenses/MIT):
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11
+
@@ -0,0 +1,64 @@
1
+ USPS-intelligent-barcode is a pure ruby gem to generate a USPS IMB
2
+ (Intelligent Mail Barcode). More specifically, it generates the
3
+ string of characters you should print using one of the [USPS
4
+ Intelligent Barcode
5
+ fonts](https://ribbs.usps.gov/onecodesolution/download.cfm).
6
+
7
+ # ORIGIN
8
+
9
+ This project was forked from Ryan Taylor's
10
+ https://github.com/rtlong/USPS-intelligent-barcode Long, in order to
11
+ add tests and refactor. It is _not_ a drop-in replacement: I renamed
12
+ most methods and classes, and eliminated the #draw method.
13
+
14
+ # INSTALL
15
+
16
+ $ gem install USPS-intelligent-barcode
17
+
18
+ # EXAMPLE
19
+
20
+ #!/usr/bin/env ruby
21
+
22
+ require 'rubygems'
23
+ require 'USPS-intelligent-barcode'
24
+
25
+ barcode_id = '01'
26
+ service_type = '234'
27
+ mailer_id = '567094'
28
+ serial_number = '987654321'
29
+ routing_code = '01234567891'
30
+ barcode = Imb::Barcode.new(barcode_id,
31
+ service_type,
32
+ mailer_id,
33
+ serial_number,
34
+ routing_code)
35
+ p barcode.barcode_letters
36
+ # => "AADTFFDFTDADTAADAATFDTDDAAADDTDTTDAFADADDDTFFFDDTTTADFAAADFTDAADA"
37
+
38
+ # STANDARD
39
+
40
+ This gem is based upon standard
41
+ [USPS-B-3200G](https://ribbs.usps.gov/intelligentmail_mailpieces/documents/tech_guides/SPUSPSG.pdf)
42
+
43
+ # RUBY VERSIONS
44
+
45
+ These Ruby versions are supported (these are the rvm platform names):
46
+
47
+ * jruby-1.7
48
+ * ruby-1.9.3
49
+ * ruby-2.0
50
+ * ruby-2.1
51
+ * ruby-2.2
52
+
53
+ For ruby-1.8.7 please see the deprecated
54
+ [USPS-intelligent-barcode](https://rubygems.org/gems/USPS-intelligent-barcode)
55
+ gem.
56
+
57
+ # WHOAMI
58
+
59
+ Wayne Conrad <kf7qga@gmail.com>
60
+
61
+ # CREDITS
62
+
63
+ Thanks to Ryan Taylor Long for his original work, without which I
64
+ would have been lost in the USPS specification.
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ require 'bundler'
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+ require 'rake'
14
+
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "usps_intelligent_barcode"
19
+ gem.homepage = "http://github.com/wconrad/usps_intelligent_barcode"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Generates a USPS Intelligent Mail Barcode.}
22
+ gem.description =
23
+ ("A pure Ruby library to generate a USPS Intelligent "\
24
+ "Mail barcode. It generates the string of characters "\
25
+ "to print with one of the USPS Intelligent Mail barcode "\
26
+ "fonts.")
27
+ gem.email = "wconrad@yagni.com"
28
+ gem.authors = ["Wayne Conrad"]
29
+ # dependencies defined in Gemfile
30
+ end
31
+ Jeweler::RubygemsDotOrgTasks.new
32
+
33
+ require 'rspec/core/rake_task'
34
+ RSpec::Core::RakeTask.new(:spec)
35
+
36
+ task :default => :spec
37
+
38
+ require 'yard'
39
+ YARD::Rake::YardocTask.new do |t|
40
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'USPS-intelligent-barcode'
5
+
6
+ barcode_id = '01'
7
+ service_type = '234'
8
+ mailer_id = '567094'
9
+ serial_number = '987654321'
10
+ routing_code = '01234567891'
11
+ barcode = Imb::Barcode.new(barcode_id,
12
+ service_type,
13
+ mailer_id,
14
+ serial_number,
15
+ routing_code)
16
+ p barcode.barcode_letters
17
+ # => "AADTFFDFTDADTAADAATFDTDDAAADDTDTTDAFADADDDTFFFDDTTTADFAAADFTDAADA"
@@ -0,0 +1,17 @@
1
+ require 'andand'
2
+ require 'memoizer'
3
+ require 'yaml'
4
+
5
+ require 'USPS-intelligent-barcode/bar_map'
6
+ require 'USPS-intelligent-barcode/bar_position'
7
+ require 'USPS-intelligent-barcode/bar_symbol'
8
+ require 'USPS-intelligent-barcode/barcode'
9
+ require 'USPS-intelligent-barcode/barcode_id'
10
+ require 'USPS-intelligent-barcode/character_position'
11
+ require 'USPS-intelligent-barcode/codeword_map'
12
+ require 'USPS-intelligent-barcode/crc'
13
+ require 'USPS-intelligent-barcode/mailer_id'
14
+ require 'USPS-intelligent-barcode/numeric_conversions'
15
+ require 'USPS-intelligent-barcode/routing_code'
16
+ require 'USPS-intelligent-barcode/serial_number'
17
+ require 'USPS-intelligent-barcode/service_type'
@@ -0,0 +1,52 @@
1
+ require 'USPS-intelligent-barcode/character_position'
2
+
3
+ # @!group Internal
4
+
5
+ module Imb
6
+
7
+ # Maps intelligent barcode "characters" to codes that indicate what
8
+ # type of bar to print at each given position.
9
+
10
+ class BarMap
11
+
12
+ def initialize
13
+ @mapping = load_mapping
14
+ end
15
+
16
+ # Given an array of intelligent barcode "characters", return an
17
+ # the symbols for each position.
18
+ # @param [[Integer]] characters array of characters
19
+ # @return [[BarSymbol]] array of symbols
20
+
21
+ def symbols(characters)
22
+ @mapping.map do |bar_position|
23
+ bar_position.map(characters)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def load_mapping
30
+ convert_mapping_data(load_mapping_data)
31
+ end
32
+
33
+ def convert_mapping_data(mapping_data)
34
+ mapping_data.map do |descender, ascender|
35
+ descender_character_position = CharacterPosition.new(*descender)
36
+ ascender_character_position = CharacterPosition.new(*ascender)
37
+ BarPosition.new(descender_character_position,
38
+ ascender_character_position)
39
+ end
40
+ end
41
+
42
+ def load_mapping_data
43
+ YAML.load_file(mapping_path)
44
+ end
45
+
46
+ def mapping_path
47
+ File.expand_path('bar_to_character_mapping.yml', File.dirname(__FILE__))
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,40 @@
1
+ module Imb
2
+
3
+ # @!group Internal
4
+
5
+ # Represents a position (one line) in the barcode. This class is
6
+ # internal and may change.
7
+
8
+ class BarPosition
9
+
10
+ # @param [CharacterPosition] descender_character_position
11
+ # @param [CharacterPosition] ascender_character_position
12
+
13
+ def initialize(descender_character_position, ascender_character_position)
14
+ @descender_character_position = descender_character_position
15
+ @ascender_character_position = ascender_character_position
16
+ end
17
+
18
+ # Given an array of characters, return a symbol for this
19
+ # barcode position.
20
+ # @param [[Integer]] characters character codes
21
+ # @return [BarSymbol] symbol code
22
+
23
+ def map(characters)
24
+ BarSymbol.make(ascender_bit(characters),
25
+ descender_bit(characters))
26
+ end
27
+
28
+ private
29
+
30
+ def descender_bit(characters)
31
+ @descender_character_position.extract_bit_from_characters(characters)
32
+ end
33
+
34
+ def ascender_bit(characters)
35
+ @ascender_character_position.extract_bit_from_characters(characters)
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,49 @@
1
+ module Imb
2
+
3
+ # @!group Internal
4
+
5
+ # Represents a symbol in the barcode.
6
+
7
+ class BarSymbol
8
+
9
+ # @param [Integer] ascender_bit (0..1)
10
+ # @param [Integer] descender_bit (0..1)
11
+ # @return [BarSymbol]
12
+
13
+ def self.make(ascender_bit, descender_bit)
14
+ case [ascender_bit, descender_bit]
15
+ when [0, 0]
16
+ TRACKER
17
+ when [0, 1]
18
+ DESCENDER
19
+ when [1, 0]
20
+ ASCENDER
21
+ when [1, 1]
22
+ FULL
23
+ end
24
+ end
25
+
26
+ # @return [Integer] the code for this symbol
27
+ attr_reader :code
28
+
29
+ # @return [String] the letter for this symbol
30
+ attr_reader :letter
31
+
32
+ # @param [Integer] code (0..3)
33
+ # @param [String] letter
34
+
35
+ def initialize(code, letter)
36
+ @code = code
37
+ @letter = letter
38
+ end
39
+
40
+ private
41
+
42
+ TRACKER = BarSymbol.new(0, 'T')
43
+ DESCENDER = BarSymbol.new(1, 'D')
44
+ ASCENDER = BarSymbol.new(2, 'A')
45
+ FULL = BarSymbol.new(3, 'F')
46
+
47
+ end
48
+
49
+ end