Class: Heatwave::TrackingNumber::Spec
- Inherits:
-
Object
- Object
- Heatwave::TrackingNumber::Spec
- Defined in:
- app/lib/heatwave/tracking_number/spec.rb
Overview
One barcode family from the owned courier JSON.
Class Method Summary collapse
Instance Method Summary collapse
- #checksum? ⇒ Boolean
- #courier_code ⇒ Symbol
- #courier_name ⇒ String?
-
#decode(number) ⇒ Hash{Symbol => String}
Named capture groups, underscored, whitespace stripped.
- #id ⇒ String
- #initialize(payload) ⇒ void constructor
- #invalid_examples ⇒ Array<String>
-
#result(number) ⇒ Result?
Nil when the number does not satisfy this spec.
- #tracking_url_template ⇒ String?
- #valid?(number) ⇒ Boolean
- #valid_examples ⇒ Array<String>
Constructor Details
#initialize(payload) ⇒ void
12 13 14 15 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 12 def initialize(payload) @payload = payload @pattern = compile_pattern(payload[:regex]) end |
Class Method Details
.normalize(number) ⇒ String
84 85 86 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 84 def self.normalize(number) number.to_s.strip.gsub(/\s+/, '').upcase end |
Instance Method Details
#checksum? ⇒ Boolean
80 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 80 def checksum? = validation[:checksum].present? |
#courier_code ⇒ Symbol
21 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 21 def courier_code = @payload[:courier_code].to_s.to_sym |
#courier_name ⇒ String?
24 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 24 def courier_name = @payload[:courier_name] |
#decode(number) ⇒ Hash{Symbol => String}
Named capture groups, underscored, whitespace stripped.
45 46 47 48 49 50 51 52 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 45 def decode(number) match = format_match(self.class.normalize(number)) return {} unless match match.named_captures.to_h do |name, value| [name.underscore.to_sym, value&.gsub(/\s/, '')] end end |
#id ⇒ String
18 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 18 def id = @payload[:id]&.to_s |
#invalid_examples ⇒ Array<String>
77 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 77 def invalid_examples = Array(@payload.dig(:test_numbers, :invalid)) |
#result(number) ⇒ Result?
Returns nil when the number does not satisfy this spec.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 56 def result(number) normalized = self.class.normalize(number) return unless valid?(normalized) extras = additional_lookup(format_match(normalized)) Result.new( tracking_number: normalized, courier_code:, courier_name: extras.dig('Courier', :courier) || courier_name, service_type: extras.dig('Service Type', :name), tracking_url: url_for(normalized, extras), spec_id: id, checksum: checksum?, valid: true ) end |
#tracking_url_template ⇒ String?
27 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 27 def tracking_url_template = @payload[:tracking_url] |
#valid?(number) ⇒ Boolean
31 32 33 34 35 36 37 38 39 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 31 def valid?(number) normalized = self.class.normalize(number) match = format_match(normalized) return false unless match return false unless checksum_ok?(match, normalized) return false unless additional_exists?(match) true end |
#valid_examples ⇒ Array<String>
74 |
# File 'app/lib/heatwave/tracking_number/spec.rb', line 74 def valid_examples = Array(@payload.dig(:test_numbers, :valid)) |