class CPRClient::Client

Public Class Methods

new(user, pass, endpoint) click to toggle source

Returns a new Client.

@param user your cpr username @param pass your current cpr password @param endpoint the full URI to the cpr gctp service

# File lib/cpr_client/client.rb, line 16
def initialize(user, pass, endpoint)
  @user, @pass, @endpoint = user, pass, endpoint
end

Public Instance Methods

login() click to toggle source

Performs login for client.

@return true @raise LoginError if login failed

# File lib/cpr_client/client.rb, line 45
def login
  code = receipt_code(post(login_body))
  code == 900 or raise LoginError, code
end
lookup(cpr) click to toggle source

Returns a Record object or nil if the record could not be found. If the client is not logged in, a login is performed before a retry.

The cpr parameter is stripped of any non-digits.

@param cpr a string @return a #Record or nil if no record was found

# File lib/cpr_client/client.rb, line 27
def lookup(cpr)
  xml_doc = post_auto_login(stamp_body(digits(cpr)))
  case receipt_code(xml_doc)
    when 0
      Record.new(xml_doc)
    when 172, 52
      nil
    else
      raise ClientError, "Unexpected STAMP resp: #{xml_doc}"
  end
end
Also aliased as: stamp
new_password(new_password) click to toggle source

Performs change of password.

@return true @raise NewPasswordError if password change failed

# File lib/cpr_client/client.rb, line 55
def new_password(new_password)
  code = receipt_code(post(new_password_body(new_password)))
  code == 900 or raise NewPasswordError, code
end
stamp(cpr)
Alias for: lookup

Protected Instance Methods

digits(arg) click to toggle source

Removes all non digit characters from string

# File lib/cpr_client/client.rb, line 74
def digits(arg)
  arg.to_s.gsub(/\D+/,'')
end
http() click to toggle source

Returns the underlying HTTPClient.

@return a HTTPClient

# File lib/cpr_client/client.rb, line 107
def http
  @http ||= HTTPClient.new(
      agent_name: "CPRClient/#{CPRClient::VERSION}",
      default_header: { 'Content-Type' => 'text/xml' }
  )
end
post(body) click to toggle source

Posts a request to the service with xml as content type.

@param body a string of xml @return a Nokogiri::XML object @raise ClientError if the response status was not 200

# File lib/cpr_client/client.rb, line 98
def post(body)
  resp = http.post(@endpoint, body)
  raise ClientError, 'Bad response' if resp.status != 200
  Nokogiri::XML(resp.body)
end
post_auto_login(body) click to toggle source

Posts xml to the server.

A login is performed if the client is not logged in, or if the login is expired.

@param body a string of xml @return a Nokogiri::XML object @raise ClientError if the response status was not 200 @raise LoginError if login failed

# File lib/cpr_client/client.rb, line 87
def post_auto_login(body)
  xml_doc = post(body)
  xml_doc = login && post(body) if receipt_code(xml_doc) == 901
  xml_doc
end
receipt_code(xml_doc) click to toggle source

Returns the gctp status code of the given xml_doc.

If a receipt is not preset then -1 is returned.

@param xml_doc a Nokogiri::XML object @return a Fixnum gctp status code

# File lib/cpr_client/client.rb, line 68
def receipt_code(xml_doc)
  node = xml_doc && xml_doc.at_css('Kvit')
  node && node['v'] ? node['v'].to_i : -1
end

Private Instance Methods

login_body() click to toggle source
# File lib/cpr_client/client.rb, line 116
    def login_body
      <<-DATA
      <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
      <root xmlns="http://www.cpr.dk">
        <Gctp v=”1.0”>
          <Sik function="signon" userid="#{@user}"  password="#{@pass}"/>
        </Gctp>
      </root>
      DATA
    end
new_password_body(new_password) click to toggle source
# File lib/cpr_client/client.rb, line 146
    def new_password_body(new_password)
      <<-DATA
      <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
      <root xmlns="http://www.cpr.dk">
        <Gctp v="1.0">
          <Sik function="newpass" userid="#{@user}" password="#{@pass}" newpass1="#{new_password}"/>
        </Gctp>
      </root>
      DATA
    end
stamp_body(cpr) click to toggle source
# File lib/cpr_client/client.rb, line 127
    def stamp_body(cpr)
      <<-DATA
      <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
      <root xmlns="http://www.cpr.dk">
        <Gctp v="1.0">
          <System r="CprSoeg">
            <Service r="STAMP">
              <CprServiceHeader r="STAMP">
                <Key>
                  <Field r="PNR" v="#{cpr}"/>
                </Key>
              </CprServiceHeader>
            </Service>
          </System>
        </Gctp>
      </root>
      DATA
    end