Class: Barcodes::Symbology::Postnet
- Defined in:
- lib/barcodes/symbology/postnet.rb
Overview
This class represents the POSTNET symbology POSTNET can encode only numbers 0-9
More info: en.wikipedia.org/wiki/POSTNET
Instance Attribute Summary
Attributes inherited from Base
#alpha, #bar_height, #bar_width, #caption_height, #caption_size, #captioned, #color, #data
Class Method Summary collapse
-
.charset ⇒ Object
POSTNET character set.
-
.valueset ⇒ Object
POSTNET values set.
Instance Method Summary collapse
-
#checksum ⇒ Object
Calculates the checksum using the provided data.
-
#encoded_data ⇒ Object
Returns the barcode data encoded as 1’s and 0’s.
-
#formatted_data ⇒ Object
Returns data + checksum.
-
#height ⇒ Object
Returns the overall height of the barcode in mils.
-
#initialize(args = {}) ⇒ Postnet
constructor
Creates a new Postnet instance.
-
#valid? ⇒ Boolean
Determines whether or not the barcode data to be encoded is valid.
-
#width ⇒ Object
Returns the overall width of the barcode in mils.
Methods inherited from Base
#caption_data, #quiet_zone_width
Constructor Details
#initialize(args = {}) ⇒ Postnet
Creates a new Postnet instance
33 34 35 36 37 38 39 |
# File 'lib/barcodes/symbology/postnet.rb', line 33 def initialize(args={}) unless args.has_key? :data args[:data] = '01234' end super(args) end |
Class Method Details
.charset ⇒ Object
POSTNET character set
19 20 21 |
# File 'lib/barcodes/symbology/postnet.rb', line 19 def self.charset ['0','1','2','3','4','5','6','7','8','9'].collect {|c| c.bytes.to_a[0] } end |
.valueset ⇒ Object
POSTNET values set
24 25 26 27 28 29 30 |
# File 'lib/barcodes/symbology/postnet.rb', line 24 def self.valueset [ '11000','00011','00101','00110', '01001','01010','01100','10001', '10010','10100' ] end |
Instance Method Details
#checksum ⇒ Object
Calculates the checksum using the provided data
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/barcodes/symbology/postnet.rb', line 61 def checksum if self.valid? sum = 0 @data.each_char do |char| sum += char.to_i end value = 10 - (sum % 10) if value == 10 value = 0 end if (0..9).include? value return value.to_s end end end |
#encoded_data ⇒ Object
Returns the barcode data encoded as 1’s and 0’s.
50 51 52 53 54 55 56 57 58 |
# File 'lib/barcodes/symbology/postnet.rb', line 50 def encoded_data if self.valid? encoded_data = '' self.formatted_data.each_byte do |char| encoded_data += self._encode_character char end return '1' + encoded_data + '1' end end |
#formatted_data ⇒ Object
Returns data + checksum
42 43 44 45 46 47 |
# File 'lib/barcodes/symbology/postnet.rb', line 42 def formatted_data checksum = self.checksum unless checksum.nil? @data + checksum end end |
#height ⇒ Object
Returns the overall height of the barcode in mils.
88 89 90 |
# File 'lib/barcodes/symbology/postnet.rb', line 88 def height 125 end |
#valid? ⇒ Boolean
Determines whether or not the barcode data to be encoded is valid. Data length must be 5, 9 or 11 digits
95 96 97 98 99 100 101 102 103 |
# File 'lib/barcodes/symbology/postnet.rb', line 95 def valid? @data.each_byte do |char| if self._encode_character(char).nil? return false end end return @data.length == 5 || @data.length == 9 || @data.length == 11 end |
#width ⇒ Object
Returns the overall width of the barcode in mils.
80 81 82 83 84 85 |
# File 'lib/barcodes/symbology/postnet.rb', line 80 def width if self.valid? return (((self.encoded_data.length * 2) - 1) * 20) end return 0 end |