Class: CPRClient::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(user, pass, endpoint) ⇒ Client

Returns a new Client.

Parameters:

  • user

    your cpr username

  • pass

    your current cpr password

  • endpoint

    the full URI to the cpr gctp service



16
17
18
19
20
21
22
# File 'lib/cpr_client/client.rb', line 16

def initialize(user, pass, endpoint)
  @user, @pass, @endpoint = user, pass, endpoint
  @http = HTTPClient.new(
      agent_name: "CPRClient/#{CPRClient::VERSION}",
      default_header: { 'Content-Type' => 'text/xml' }
  )
end

Instance Method Details

#loginObject

Performs login for client.

Returns:

  • true

Raises:

  • LoginError if login failed



49
50
51
52
# File 'lib/cpr_client/client.rb', line 49

def 
  code = receipt_code(post())
  code == 900 or raise LoginError, code
end

#lookup(cpr) ⇒ Object Also known as: stamp

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.

Parameters:

  • cpr

    a string

Returns:

  • a #Record or nil if no record was found



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cpr_client/client.rb', line 31

def lookup(cpr)
  xml_doc = (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