Class: SwedishPIN::Parser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/swedish_pin/parser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Parser for Personnummer.

Please use parse or valid? instead.

Instance Method Summary collapse

Constructor Details

#initialize(input, now = Time.now) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Setup a new parser.



24
25
26
27
28
29
30
31
32
# File 'lib/swedish_pin/parser.rb', line 24

def initialize(input, now = Time.now)
  unless input.is_a?(String)
    raise ArgumentError, "Expected String, got #{input.inspect}"
  end

  @input = input
  @now = now
  @matches = MATCHER.match(input.strip)
end

Instance Method Details

#parseObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return Hash of parsed values to be used with SwedishPIN::Personnummer#initialize.



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/swedish_pin/parser.rb', line 50

def parse
  validate

  {
    year: full_year,
    month: month,
    day: day,
    sequence_number: sequence_number,
    control_digit: control_digit
  }
end

#valid?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check validity without raising.

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/swedish_pin/parser.rb', line 42

def valid?
  validate
  true
rescue
  false
end

#validateObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raise SwedishPIN::ParseError if anything in the input isn’t valid.



35
36
37
38
39
# File 'lib/swedish_pin/parser.rb', line 35

def validate
  validate_match
  validate_luhn
  validate_date
end