<commit_before>module TrackingNumber
  class FedEx < Base
    def carrier
      :fedex
    end
  end

  class FedExExpress < FedEx
    SEARCH_PATTERN = /(\b([0-9]\s*){12,12}\b)/
    VERIFY_PATTERN = /^([0-9]{11,11})([0-9])$/
    LENGTH = 12

    def matches
      self.tracking_number.scan(VERIFY_PATTERN).flatten
    end

    def valid_checksum?
      sequence, check_digit = matches
      total = 0
      sequence.chars.to_a.zip([3,1,7,3,1,7,3,1,7,3,1]).each do |(a,b)|
        total += a.to_i * b
      end
      return (total % 11 % 10) == check_digit.to_i
    end
  end

  class FedExSmartPost < FedEx
    SEARCH_PATTERN = /(\b(?:9\s*2\s*)?([0-9]\s*){20}\b)/
    VERIFY_PATTERN = /^((?:92)?[0-9]{5}[0-9]{14})([0-9])$/

    def matches
      self.tracking_number.scan(VERIFY_PATTERN).flatten
    end

    def valid_checksum?
      # http://stackoverflow.com/questions/15744704/how-to-calculate-a-fedex-smartpost-tracking-number-check-digit

      @tracking_number = "92#{tracking_number}" unless tracking_number =~ /^92/
      sequence = @tracking_number.chars.map(&:to_i)
      check_digit = sequence.pop

      total = 0
      sequence.reverse.each_with_index do |x, i|
        x *= 3 if i.even?
        total += x
      end

      check = total % 10
      check = (10 - check) unless (check.zero?)

      return true if check == check_digit.to_i
    end
  end

  #TODO Fix these FedEx ground numberss

  class FedExGround96 < FedEx
    SEARCH_PATTERN = /(\b9\s*6\s*([0-9]\s*){20,20}\b)/
    VERIFY_PATTERN = /^96[0-9]{5,5}([0-9]{14,14})([0-9])$/
    LENGTH = 22

    def matches
      self.tracking_number.scan(VERIFY_PATTERN).flatten
    end

    def decode
      {:application_id => self.tracking_number.to_s.slice(0...2),<commit_msg>Update fedex.rb<commit_after>module TrackingNumber
  class FedEx < Base
    def carrier
      :fedex
    end
  end

  class FedExExpress < FedEx
    SEARCH_PATTERN = /(\b([0-9]\s*){12,12}\b)/
    VERIFY_PATTERN = /^([0-9]{11,11})([0-9])$/
    LENGTH = 12

    def matches
      self.tracking_number.scan(VERIFY_PATTERN).flatten
    end

    def valid_checksum?
      sequence, check_digit = matches
      total = 0
      sequence.chars.to_a.zip([3,1,7,3,1,7,3,1,7,3,1]).each do |(a,b)|
        total += a.to_i * b
      end
      return (total % 11 % 10) == check_digit.to_i
    end
  end

  class FedExSmartPost < FedEx
    SEARCH_PATTERN = /(\b(?:9\s*2\s*)?([0-9]\s*){20}\b)/
    VERIFY_PATTERN = /^((?:92)?[0-9]{5}[0-9]{14})([0-9])$/

    def matches
      self.tracking_number.scan(VERIFY_PATTERN).flatten
    end

    def valid_checksum?
      # http://stackoverflow.com/questions/15744704/how-to-calculate-a-fedex-smartpost-tracking-number-check-digit

      @tracking_number = "92#{tracking_number}" unless tracking_number =~ /^92/
      sequence = @tracking_number.chars.map(&:to_i)
      check_digit = sequence.pop

      total = 0
      sequence.reverse.each_with_index do |x, i|
        x *= 3 if i.even?
        total += x
      end

      check = total % 10
      check = (10 - check) unless (check.zero?)

      return true if check == check_digit.to_i
    end
  end

  class FedExGround96 < FedEx
    SEARCH_PATTERN = /(\b9\s*6\s*([0-9]\s*){20,20}\b)/
    VERIFY_PATTERN = /^96[0-9]{5,5}([0-9]{14,14})([0-9])$/
    LENGTH = 22

    def matches
      self.tracking_number.scan(VERIFY_PATTERN).flatten
    end

    def decode
      {:application_id => self.tracking_number.to_s.slice(0...2),
{'fraction_non_alphanumeric': 0.15897882216420076, 'fraction_numerical': 0.05512039454598201, 'mean_word_length': 2.5327868852459017, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '95224', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1308, 'n_words': 230}