class CPRClient::Client
Public Class Methods
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
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
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
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
Protected Instance Methods
Removes all non digit characters from string
# File lib/cpr_client/client.rb, line 74 def digits(arg) arg.to_s.gsub(/\D+/,'') end
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
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
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
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
# 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
# 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
# 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