Class: Identification::Document Abstract

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

Overview

This class is abstract.

Subclass and implement a #parse method (setting the fields and @validity)

Represents an identity document. This class carries base methods that will appear in all document types.

and alter #initialize to implement a custom Document class. See other classes for examples.

Direct Known Subclasses

DVLA, Passport

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Document

Creates an instance of a documents. Will automatically parse if the drivers number is given, and will automatically generate if all necessary fields are set.

Parameters:

  • details (Hash)

    of the paramaters

  • opts (Hash)

    a customizable set of options



17
18
19
20
21
# File 'lib/identification/document.rb', line 17

def initialize(params = {})
  @last_name = params[:last_name]
  @date_of_birth = params[:date_of_birth]
  @gender = params[:gender]
end

Instance Attribute Details

#date_of_birthObject

Returns the value of attribute date_of_birth.



8
9
10
# File 'lib/identification/document.rb', line 8

def date_of_birth
  @date_of_birth
end

#genderObject

Returns the value of attribute gender.



8
9
10
# File 'lib/identification/document.rb', line 8

def gender
  @gender
end

#last_nameObject

Returns the value of attribute last_name.



8
9
10
# File 'lib/identification/document.rb', line 8

def last_name
  @last_name
end

Instance Method Details

#ageBoolean

Returns the age of the individual. Requires a date of birth to be set before it can be called.

Returns:

  • (Boolean)

    age of the individual

Raises:

  • (RuntimeError)

    if no date of birth is set



41
42
43
44
45
46
47
48
49
# File 'lib/identification/document.rb', line 41

def age
  if !@date_of_birth.nil?
    now = Time.now.utc.to_date
    dob = @date_of_birth
    return now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
  else
    fail 'No date of birth has been set.'
  end
end

#over_18?Boolean

Returns true if the individual is over 18 (18+) Requires a date of birth to be set before it can be called.

Returns:

  • (Boolean)

    whether or not the individual is over 18

Raises:

  • (RuntimeError)

    if no date of birth is set



56
57
58
59
60
61
62
63
64
# File 'lib/identification/document.rb', line 56

def over_18?
  if !@date_of_birth.nil?
    return true if age >= 18
  else
    fail 'No date of birth has been set.'
  end

  false
end

#over_21?Boolean

Returns true if the individual is over 21 (21+) Requires a date of birth to be set before it can be called.

Returns:

  • (Boolean)

    whether or not the individual is over 21

Raises:

  • (RuntimeError)

    if no date of birth is set



71
72
73
74
75
76
77
78
79
# File 'lib/identification/document.rb', line 71

def over_21?
  if !@date_of_birth.nil?
    return true if age >= 21
  else
    fail 'No date of birth has been set.'
  end

  false
end

#valid?Boolean

Returns true if the drivers license is valid. Requires the driver number to be parsed before it can be called.

Returns:

  • (Boolean)

    whether or not the passport is valid

Raises:

  • (RuntimeError)

    if no mrz has been parsed yet.



28
29
30
31
32
33
34
# File 'lib/identification/document.rb', line 28

def valid?
  if defined? @validity
    return @validity
  else
    fail 'No document number has been parsed.'
  end
end