Class: TrackingNumber::Base
- Inherits:
-
Object
- Object
- TrackingNumber::Base
- Defined in:
- lib/tracking_number/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#original_number ⇒ Object
Returns the value of attribute original_number.
-
#tracking_number ⇒ Object
Returns the value of attribute tracking_number.
Class Method Summary collapse
Instance Method Summary collapse
- #check_digit ⇒ Object
- #courier_code ⇒ Object (also: #carrier, #carrier_code)
- #courier_info ⇒ Object
- #courier_name ⇒ Object (also: #carrier_name)
- #decode ⇒ Object
- #destination_zip ⇒ Object
- #info ⇒ Object
-
#initialize(tracking_number) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #matching_additional ⇒ Object
- #package_type ⇒ Object
- #serial_number ⇒ Object
- #service_description ⇒ Object
- #service_type ⇒ Object
- #shipper_id ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
- #valid_checksum? ⇒ Boolean
- #valid_format? ⇒ Boolean
- #valid_optional_checks? ⇒ Boolean
Constructor Details
#initialize(tracking_number) ⇒ Base
Returns a new instance of Base.
10 11 12 13 |
# File 'lib/tracking_number/base.rb', line 10 def initialize(tracking_number) @original_number = tracking_number @tracking_number = tracking_number.strip.gsub(" ", "").upcase end |
Instance Attribute Details
#original_number ⇒ Object
Returns the value of attribute original_number.
8 9 10 |
# File 'lib/tracking_number/base.rb', line 8 def original_number @original_number end |
#tracking_number ⇒ Object
Returns the value of attribute tracking_number.
7 8 9 |
# File 'lib/tracking_number/base.rb', line 7 def tracking_number @tracking_number end |
Class Method Details
.scan(body) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tracking_number/base.rb', line 26 def self.scan(body) # matches with match groups within the match data matches = [] body.scan(self.const_get(:SEARCH_PATTERN)){ #get the match data instead, which is needed with these types of regexes matches << $~ } if matches matches.collect { |m| m[0] } else [] end end |
.search(body) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tracking_number/base.rb', line 15 def self.search(body) valids = self.scan(body).uniq.collect { |possible| new(possible) }.select { |t| t.valid? } uniques = {} valids.each do |t| uniques[t.tracking_number] = t unless uniques.has_key?(t.tracking_number) end uniques.values end |
Instance Method Details
#check_digit ⇒ Object
59 60 61 |
# File 'lib/tracking_number/base.rb', line 59 def check_digit match_group("CheckDigit") end |
#courier_code ⇒ Object Also known as: carrier, carrier_code
123 124 125 |
# File 'lib/tracking_number/base.rb', line 123 def courier_code self.class.const_get(:COURIER_CODE).to_sym end |
#courier_info ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/tracking_number/base.rb', line 141 def courier_info basics = {:name => courier_name, :code => courier_code} if info = matching_additional["Courier"] basics.merge!(:name => info[:courier], :url => info[:courier_url], :country => info[:country]) end @courier ||= Info.new(basics) end |
#courier_name ⇒ Object Also known as: carrier_name
127 128 129 130 131 132 133 134 135 |
# File 'lib/tracking_number/base.rb', line 127 def courier_name if matching_additional["Courier"] matching_additional["Courier"][:courier] else if self.class.constants.include?(:COURIER_INFO) self.class.const_get(:COURIER_INFO)[:name] end end end |
#decode ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/tracking_number/base.rb', line 63 def decode decoded = {} (self.matches.try(:names) || []).each do |name| sym = name.underscore.to_sym decoded[sym] = self.matches[name] end decoded end |
#destination_zip ⇒ Object
169 170 171 |
# File 'lib/tracking_number/base.rb', line 169 def destination_zip match_group("DestinationZip") end |
#info ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/tracking_number/base.rb', line 111 def info Info.new({ :courier => courier_info, :service_type => service_type, :service_description => service_description, :destination_zip => destination_zip, :shipper_id => shipper_id, :package_type => package_type, :package_description => package_description }) end |
#inspect ⇒ Object
107 108 109 |
# File 'lib/tracking_number/base.rb', line 107 def inspect "#<%s:%#0x %s>" % [self.class.to_s, self.object_id, tracking_number] end |
#matching_additional ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/tracking_number/base.rb', line 177 def matching_additional additional = self.class.const_get(:ADDITIONAL) || [] relevant_sections = {} additional.each do |info| if self.matches && self.matches.length > 0 if value = self.matches[info[:regex_group_name]].gsub(/\s/, "") # has matching value matches = info[:lookup].find do |i| if i[:matches] value == i[:matches] elsif i[:matches_regex] value =~ Regexp.new(i[:matches_regex]) end end relevant_sections[info[:name]] = matches end end end relevant_sections end |
#package_type ⇒ Object
163 164 165 166 167 |
# File 'lib/tracking_number/base.rb', line 163 def package_type if matching_additional["ContainerType"] @package_type ||= Info.new(matching_additional["Container Type"]).name end end |
#serial_number ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tracking_number/base.rb', line 42 def serial_number return match_group("SerialNumber") unless self.class.const_get("VALIDATION") format_info = self.class.const_get(:VALIDATION)[:serial_number_format] raw_serial = match_group("SerialNumber") if format_info if format_info[:prepend_if] && raw_serial.match(Regexp.new(format_info[:prepend_if][:matches_regex])) return "#{format_info[:prepend_if][:content]}#{raw_serial}" elsif format_info[:prepend_if_missing] end end return raw_serial end |
#service_description ⇒ Object
157 158 159 160 161 |
# File 'lib/tracking_number/base.rb', line 157 def service_description if matching_additional["Service Type"] @service_description ||= Info.new(matching_additional["Service Type"]).description end end |
#service_type ⇒ Object
151 152 153 154 155 |
# File 'lib/tracking_number/base.rb', line 151 def service_type if matching_additional["Service Type"] @service_type ||= Info.new(matching_additional["Service Type"]).name end end |
#shipper_id ⇒ Object
173 174 175 |
# File 'lib/tracking_number/base.rb', line 173 def shipper_id match_group("ShipperId") end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/tracking_number/base.rb', line 103 def to_s self.tracking_number end |
#valid? ⇒ Boolean
73 74 75 76 77 78 |
# File 'lib/tracking_number/base.rb', line 73 def valid? return false unless valid_format? return false unless valid_checksum? return false unless valid_optional_checks? return true end |
#valid_checksum? ⇒ Boolean
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/tracking_number/base.rb', line 92 def valid_checksum? return false unless self.valid_format? checksum_info = self.class.const_get(:VALIDATION)[:checksum] return true unless checksum_info name = checksum_info[:name] method_name = "validates_#{name}?" ChecksumValidations.send(method_name, serial_number, check_digit, checksum_info) end |
#valid_format? ⇒ Boolean
80 81 82 |
# File 'lib/tracking_number/base.rb', line 80 def valid_format? !matches.nil? end |
#valid_optional_checks? ⇒ Boolean
84 85 86 87 88 89 90 |
# File 'lib/tracking_number/base.rb', line 84 def valid_optional_checks? additional_check = self.class.const_get("VALIDATION")[:additional] return true unless additional_check exist_checks = (additional_check[:exists] ||= []) exist_checks.all? { |w| matching_additional[w] } end |