Class: Kennitala

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

Overview

The main Kennitala class

Constant Summary collapse

VERSION =

The current version of the gem

'0.1.2'

Instance Method Summary collapse

Constructor Details

#initialize(kt_string = false, is_company = false) ⇒ Kennitala

Note:

The string provided may include spaces, hyphens and alphabetical characters, which will then be erased from the resulting string.

Initialize a Kennitala object

Examples:

Initialize a Kennitala object based on an unsanitized String

Kennitala.new(' 010130-2989')
=> #<Kennitala:0x007fe35d041bc0 @value="0101302989">

Invalid strings are rejected with an argument error

Kennitala.new('010130-2979')
=> ArgumentError: Kennitala is invalid

If no kennitala string is specified, a random one will be generated

Kennitala.new
=> #<Kennitala:0x007fc589339f18 @value="2009155509">

Parameters:

  • kt_string (String, Boolean) (defaults to: false)

    Either Kennitala String or Boolean false.

  • is_company (Boolean) (defaults to: false)

    Wether the randomly generated kennitala is for a company

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kennitala.rb', line 32

def initialize(kt_string = false, is_company = false)
  kt_string = fake_kt_string(is_company) if kt_string == false
  unless kt_string.instance_of?(String)
    raise ArgumentError, 'Kennitala needs to be provided as a String or ' \
                         'Boolean (false)'
  end
  sanitised_kt = sanitize(kt_string)
  raise ArgumentError, 'Kennitala is invalid' if sanitised_kt.nil?

  @value = sanitised_kt
end

Instance Method Details

#ageFixnum

Get the age of entity in years. Useful when dealing with age restrictions.

Examples:

Get the current age of the entity

k = Kennitala.new('0101302989')
=> #<Kennitala:0x007fe35d041bc0 @value="0101302989">
k.age
=> 86

Returns:

  • (Fixnum)


124
125
126
127
128
129
130
131
132
# File 'lib/kennitala.rb', line 124

def age
  year_diff = Date.today.year - to_date.year
  month_diff = Date.today.month - to_date.month
  day_diff = Date.today.month - to_date.month

  return year_diff -= 1 if month_diff.negative? || (month_diff.zero? && day_diff.negative?)

  year_diff
end

#company?Boolean

Check if the entity is a company

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/kennitala.rb', line 62

def company?
  date_integer = @value[0, 2].to_i
  return true if (date_integer > 40) && (date_integer < 72)

  false
end

#dayFixnum

Get the day of the month of birth or registration

Returns:

  • (Fixnum)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/kennitala.rb', line 92

def day
  day_from_value = @value[0, 2].to_s[0, 2].to_i
  day_from_value -= 40 if day_from_value > 40

  # There are examples of a date overlapping the number of days in the
  # given month. Those are considered to be from the last day of that month
  # instad of the specified out-of-bounds day of the month.
  # This is common for the year 1969 as new registrations were being done en
  # masse.
  last_day_of_month = Date.new(year, month, -1).day

  return last_day_of_month if day_from_value > last_day_of_month

  day_from_value
end

#entity_typeString

Get the type of entity - If it is a person or an organization

Examples:

Get the entity type (results in ‘person’ or ‘company’)

k = Kennitala.new('0101302989')
=> #<Kennitala:0x007fe35d041bc0 @value="0101302989">
k.entity_type
=> "person"

Returns:

  • (String)

    Either ‘person’ or ‘company’



53
54
55
56
57
# File 'lib/kennitala.rb', line 53

def entity_type
  date_integer = @value[0, 2].to_i
  return 'person' if date_integer < 32
  return 'company' if (date_integer > 40) && (date_integer < 72)
end

#monthFixnum

Get a numeric representation of the month of birth or registration

Returns:

  • (Fixnum)


111
112
113
# File 'lib/kennitala.rb', line 111

def month
  @value[2, 2].to_i
end

#person?Boolean

Check if the entity is a person

Returns:

  • (Boolean)


72
73
74
75
76
77
# File 'lib/kennitala.rb', line 72

def person?
  date_integer = @value[0, 2].to_i
  return true if date_integer < 32

  false
end

#pp(spacer = ' ') ⇒ String

Pretty print a kennitala

Adds a space between the 6th and the 7th digits for readability.

Examples:

Pretty print a Kennitala it its default form

k = Kennitala.new('0101302989')
=> #<Kennitala:0x007fc589339f18 @value="2009155509">
k.pp
=> "010130 2989"

Use a hyphen as a spacer instead of the default single space

k = Kennitala.new('0101302989')
=> #<Kennitala:0x007fc589339f18 @value="2009155509">
k.pp('-')
=> "010130-2989"

Parameters:

  • spacer (String) (defaults to: ' ')

    A single space by default

Returns:



172
173
174
175
176
# File 'lib/kennitala.rb', line 172

def pp(spacer = ' ')
  raise ArgumentError 'Spacer must be a string' unless spacer.instance_of?(String)

  @value[0, 6] + spacer + @value[6, 9]
end

#to_dateDate

Cast the kennitala to a Date object

Examples:

Cast the kennitala to a Date object

k = Kennitala.new('0101302989')
=> #<Kennitala:0x007fe35d041bc0 @value="0101302989">
k.to_date
=> #<Date: 1930-01-01 ((2425978j,0s,0n),+0s,2299161j)>

Returns:

  • (Date)


143
144
145
# File 'lib/kennitala.rb', line 143

def to_date
  Date.new(year, month, day)
end

#to_sString

Cast the kennitala to a String object

Returns:



150
151
152
# File 'lib/kennitala.rb', line 150

def to_s
  @value.to_s
end

#yearFixnum

Get the year of birth or registration

Returns:

  • (Fixnum)


82
83
84
85
86
87
# File 'lib/kennitala.rb', line 82

def year
  century = (10 + @value[9].to_i) * 100
  year = @value[4, 2].to_i
  return century + year if (1800..1900).cover?(century)
  return 2000 + year if century == 1000
end