luhn 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/luhn/civic_number.rb +26 -22
- data/lib/luhn/version.rb +1 -1
- data/spec/spec_civic_number.rb +11 -3
- metadata +1 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: eece57290c12c551a1673bac68bbaf0b9674c5b2
         | 
| 4 | 
            +
              data.tar.gz: e86edca6abea3b65d71edddef8114d65e1b2015c
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 9149f14748ef4a961500f622dd114702f8c100c0425f75bddd0f1fae842a443139102598417aef83fc3d0d0dd9cec4d0bb0e26d037e6555e278d82ad8489cec3
         | 
| 7 | 
            +
              data.tar.gz: 03dd2de39b1698ec77d947db29dc297cf4eb4d2ceec23e31170cd72047f67196b8da5e7f09f2ee48ba89a0b68cf92206272c4bd8f070d550b1a0b16d6efb4917
         | 
    
        data/lib/luhn/civic_number.rb
    CHANGED
    
    | @@ -2,14 +2,14 @@ | |
| 2 2 | 
             
            require 'ostruct'
         | 
| 3 3 | 
             
            module Luhn
         | 
| 4 4 | 
             
              class CivicNumber
         | 
| 5 | 
            -
                attr_reader : | 
| 5 | 
            +
                attr_reader :value
         | 
| 6 6 |  | 
| 7 | 
            -
                def initialize | 
| 8 | 
            -
                   | 
| 7 | 
            +
                def initialize string
         | 
| 8 | 
            +
                  self.value = string
         | 
| 9 9 | 
             
                end
         | 
| 10 10 |  | 
| 11 11 | 
             
                def valid?
         | 
| 12 | 
            -
                  @valid ||=  | 
| 12 | 
            +
                  @valid ||= value.length == 10 && valid_date? && Luhn.valid?(value)
         | 
| 13 13 | 
             
                end
         | 
| 14 14 |  | 
| 15 15 | 
             
                def valid_date?
         | 
| @@ -17,11 +17,11 @@ module Luhn | |
| 17 17 | 
             
                end
         | 
| 18 18 |  | 
| 19 19 | 
             
                def control_digit
         | 
| 20 | 
            -
                  @control_digit ||= Luhn.control_digit( | 
| 20 | 
            +
                  @control_digit ||= Luhn.control_digit(value[0...9])
         | 
| 21 21 | 
             
                end
         | 
| 22 22 |  | 
| 23 23 | 
             
                def sex
         | 
| 24 | 
            -
                   | 
| 24 | 
            +
                  valid? ? (value[8...9].to_i.even? ? 'female' : 'male') : 'unknown'
         | 
| 25 25 | 
             
                end
         | 
| 26 26 |  | 
| 27 27 | 
             
                def female?
         | 
| @@ -33,34 +33,38 @@ module Luhn | |
| 33 33 | 
             
                end
         | 
| 34 34 |  | 
| 35 35 | 
             
                def birth_date
         | 
| 36 | 
            -
                   | 
| 37 | 
            -
                    :year  =>  | 
| 38 | 
            -
                    :month =>  | 
| 39 | 
            -
                    :day   =>  | 
| 36 | 
            +
                  OpenStruct.new({
         | 
| 37 | 
            +
                    :year  => value[0...2].to_i,
         | 
| 38 | 
            +
                    :month => value[2...4].to_i,
         | 
| 39 | 
            +
                    :day   => value[4...6].to_i
         | 
| 40 40 | 
             
                  })
         | 
| 41 41 | 
             
                end
         | 
| 42 42 |  | 
| 43 43 | 
             
                def formatted
         | 
| 44 | 
            -
                   | 
| 44 | 
            +
                  return value if value.length < 10
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                  value.insert(value.length - 4, "-")
         | 
| 45 47 | 
             
                end
         | 
| 46 48 |  | 
| 47 49 | 
             
                def to_s
         | 
| 48 | 
            -
                   | 
| 50 | 
            +
                  value
         | 
| 49 51 | 
             
                end
         | 
| 50 52 |  | 
| 51 | 
            -
                 | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
                    Luhn.generate(10, :prefix => date.strftime("%y%m%d"))
         | 
| 55 | 
            -
                  end
         | 
| 53 | 
            +
                # For backwards compability
         | 
| 54 | 
            +
                def civic_number
         | 
| 55 | 
            +
                  value
         | 
| 56 56 | 
             
                end
         | 
| 57 57 |  | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
                  string.gsub!(/\D/, '')
         | 
| 62 | 
            -
                  string.length == 12 ? string[2...12] : string
         | 
| 58 | 
            +
                def self.generate
         | 
| 59 | 
            +
                  date = Time.local(Time.now.year - rand(100) - 1, rand(12) + 1, rand(31) + 1)
         | 
| 60 | 
            +
                  Luhn.generate(10, :prefix => date.strftime("%y%m%d"))
         | 
| 63 61 | 
             
                end
         | 
| 64 62 |  | 
| 63 | 
            +
                private
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                def value= string
         | 
| 66 | 
            +
                  val = string.to_s.gsub(/\D/, '')
         | 
| 67 | 
            +
                  @value = val.length == 12 ? val[2...12] : val
         | 
| 68 | 
            +
                end
         | 
| 65 69 | 
             
              end
         | 
| 66 70 | 
             
            end
         | 
    
        data/lib/luhn/version.rb
    CHANGED
    
    
    
        data/spec/spec_civic_number.rb
    CHANGED
    
    | @@ -46,10 +46,18 @@ describe Luhn::CivicNumber do | |
| 46 46 | 
             
                civic_number.birth_date.day.must_equal 1
         | 
| 47 47 | 
             
              end
         | 
| 48 48 |  | 
| 49 | 
            -
               | 
| 50 | 
            -
                 | 
| 49 | 
            +
              describe "#formatted" do
         | 
| 50 | 
            +
                it "formats the civic number" do
         | 
| 51 | 
            +
                  civic_number = Luhn::CivicNumber.new('3001018194')
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  civic_number.formatted.must_equal "300101-8194"
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                it "returns the origial if the civic number is to short" do
         | 
| 57 | 
            +
                  civic_number = Luhn::CivicNumber.new('300101819')
         | 
| 51 58 |  | 
| 52 | 
            -
             | 
| 59 | 
            +
                  civic_number.formatted.must_equal "300101819"
         | 
| 60 | 
            +
                end
         | 
| 53 61 | 
             
              end
         | 
| 54 62 |  | 
| 55 63 | 
             
              it 'generates a valid random civic number' do
         |