class CPRClient::Record

Attributes

fields[R]
timestamp[R]

Public Class Methods

new(xml_doc) click to toggle source

Returns a new Response.

@param xml_doc a Nokogiri::XML object

# File lib/cpr_client/record.rb, line 9
def initialize(xml_doc)
  @timestamp = xml_doc.at_css("CprServiceHeader[r='STAMP']")['ts']
  @fields    = extract_fields(xml_doc)
end

Public Instance Methods

[](name, value = 'v')
Alias for: get
address() click to toggle source

Returns the record's address if present.

The address will be a string of the fields STADR, POSTNR and POSTNR's t attribute.

Fx. Boulevarden 101, 1. mf, 6800 Varde

@return string with address or nil

# File lib/cpr_client/record.rb, line 47
def address
  street_and_number = "#{get(:vejkod, :t)} #{get_clean(:husnr)}"
  postal_district   = "#{get(:postnr)} #{get(:postnr, :t)}"
  door  = get_clean(:sidedoer)
  floor = get_clean(:etage)

  if floor and door
    "#{street_and_number}, #{floor}. #{door}, #{postal_district}"
  elsif floor
    "#{street_and_number}, #{floor}., #{postal_district}"
  else
    "#{street_and_number}, #{postal_district}"
  end
end
birthday() click to toggle source

Returns the birthday as Date.

@return Date with date of birth

# File lib/cpr_client/record.rb, line 35
def birthday
  Date.parse(get(:foeddato))
end
get(name, value = 'v') click to toggle source

Gets the value of a field with the given name.

@param name the name of the target field. @param value the name of the value attribute

# File lib/cpr_client/record.rb, line 18
def get(name, value = 'v')
  field = @fields[name.to_s.upcase]
  field[value.to_s.downcase] if field
end
Also aliased as: []
protected?() click to toggle source

Returns true if the person has name/address protection.

@return true if protected, false otherwise

# File lib/cpr_client/record.rb, line 28
def protected?
  get(:beskyt) == '1'
end

Private Instance Methods

extract_fields(xml_doc) click to toggle source
# File lib/cpr_client/record.rb, line 69
def extract_fields(xml_doc)
  Hash[xml_doc.css("Praes[r='STAMPNR'] Field").reduce([]) { |a, f|
    attrs = Hash[f.keys.zip(f.values)]
    key = attrs.delete('r')
    attrs.empty? ? a : a << [key, attrs]
  }]
end
get_clean(name, value = 'v') click to toggle source
# File lib/cpr_client/record.rb, line 64
def get_clean(name, value = 'v')
  value = get(name, value)
  value.sub(/^[ 0\-]*(.*?)\s*/, '\1') if value
end