Sha256: 1039dd3c47e69f58d180f71699cd2ecc3d9e1c9ce2f262c715b80d20f803c5f6

Contents?: true

Size: 1.28 KB

Versions: 7

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

7 entries across 7 versions & 1 rubygems

Version Path
tracking_number-0.9.1 lib/tracking_number/base.rb
tracking_number-0.9.0 lib/tracking_number/base.rb
tracking_number-0.8.6 lib/tracking_number/base.rb
tracking_number-0.8.5 lib/tracking_number/base.rb
tracking_number-0.8.0 lib/tracking_number/base.rb
tracking_number-0.7.0 lib/tracking_number/base.rb
tracking_number-0.6.0 lib/tracking_number/base.rb