Class: Imb::RoutingCode

Inherits:
Object
  • Object
show all
Defined in:
lib/USPS-intelligent-barcode/routing_code.rb

Overview

Represents a routing code

Instance Attribute Summary collapse

Internal collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zip, plus4, delivery_point) ⇒ RoutingCode

Create a RoutingCode. Arguments are:

  • zip - Integer zip (or nil)

  • plus4 - Integer plus4 (or nil)

  • delivery_point - Integer delivery poitn (or nil)



47
48
49
50
51
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 47

def initialize(zip, plus4, delivery_point)
  @zip = arg_to_i(zip)
  @plus4 = arg_to_i(plus4)
  @delivery_point = arg_to_i(delivery_point)
end

Instance Attribute Details

#delivery_pointInteger

Returns The delivery point (or nil).

Returns:

  • (Integer)

    The delivery point (or nil)



40
41
42
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 40

def delivery_point
  @delivery_point
end

#plus4Integer

Returns The plus4 (or nil).

Returns:

  • (Integer)

    The plus4 (or nil)



37
38
39
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 37

def plus4
  @plus4
end

#zipInteger

Returns The ZIP (or nil).

Returns:

  • (Integer)

    The ZIP (or nil)



34
35
36
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 34

def zip
  @zip
end

Class Method Details

.coerce(o) ⇒ RoutingCode

Turn the argument into a RoutingCode if possible. Accepts:

  • Imb::RoutingCode

  • nil (no routing code)

  • String of length:

    • 0 - no routing code

    • 5 - zip

    • 9 - zip + plus4

    • 11 - zip + plus4 + delivery point

  • Array of [zip, plus4, delivery point]

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 18

def self.coerce(o)
  case o
  when nil
    coerce('')
  when RoutingCode
    o
  when Array
    RoutingCode.new(*o)
  when String
    RoutingCode.new(*string_to_array(o))
  else
    raise ArgumentError, 'Cannot coerce to RoutingCode'
  end
end

.string_to_array(s) ⇒ Object

Convert a string representation of a routing code into an array that can be passed to the constructor. s is a string of length:

  • 0 - no routing code

  • 5 - zip

  • 9 - zip + plus4

  • 11 - zip + plus4 + delivery point

The result is an array of [zip, zip4, delivery point]



73
74
75
76
77
78
79
80
81
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 73

def self.string_to_array(s)
  s = s.gsub(/[\D]/, '')
  match = /^(?:(\d{5})(?:(\d{4})(\d{2})?)?)?$/.match(s)
  unless match
    raise ArgumentError, "Bad routing code: #{s.inspect}"
  end
  zip, plus4, delivery_point = match.to_a[1..-1]
  [zip, plus4, delivery_point]
end

Instance Method Details

#==(o) ⇒ Object

Return true if this object is equal to o

Parameters:

  • o (Object)

    Any object acceptable to coerce



56
57
58
59
60
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 56

def ==(o)
  RoutingCode.coerce(o).to_a == to_a
rescue ArgumentError
  false
end

#shift_and_add_to(target, long_mailer_id) ⇒ Integer

Add this object’s value to target, shifting it left as many digts as are needed to make room.

Parameters:

  • target (Integer)

    The target to be shifted and added to

  • long_mailer_id

    truthy if the mailer ID is long (9 digits).

Returns:

  • (Integer)

    The new value of the target



96
97
98
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 96

def shift_and_add_to(target, long_mailer_id)
  target * 10 ** NUM_DIGITS + convert
end

#validate(long_mailer_id) ⇒ Object

Validate the value.

Parameters:

  • long_mailer_id

    truthy if the mailer ID is long (9 digits).

Raises:

  • ArgumentError if invalid



87
88
# File 'lib/USPS-intelligent-barcode/routing_code.rb', line 87

def validate(long_mailer_id)
end