Sha256: 042449583c6fccf995b0de9cec951b30e9e484b9bb72a168f618cb2ebc4c57b4

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

module TrackingNumber
  class Base
    attr_accessor :tracking_number
    attr_accessor :original_number

    def initialize(tracking_number)
      @original_number = tracking_number
      @tracking_number = tracking_number.strip.gsub(" ", "").upcase
    end

    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

    def self.scan(body)
      patterns = [self.const_get("SEARCH_PATTERN")].flatten
      possibles = patterns.collect do |pattern|
        body.scan(pattern).uniq.flatten
      end

      possibles.flatten.compact.uniq
    end

    def valid?
      return false unless valid_format?
      return false unless valid_checksum?
      return true
    end

    def valid_format?
      !matches.nil? && !matches.empty?
    end

    def decode
      {}
    end

    def matches
      []
    end

    def valid_checksum?
      false
    end

    def to_s
      self.tracking_number
    end

    def inspect
      "#<%s:%#0x %s>" % [self.class.to_s, self.object_id, tracking_number]
    end
  end

  class Unknown < Base
    def carrier
      :unknown
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tracking_number-0.10.5 lib/tracking_number/base.rb
tracking_number-0.10.4 lib/tracking_number/base.rb
tracking_number-0.10.3 lib/tracking_number/base.rb
tracking_number-0.10.1 lib/tracking_number/base.rb
tracking_number-0.10.0 lib/tracking_number/base.rb