Class: BorrowDirect::Authentication

Inherits:
Request
  • Object
show all
Defined in:
lib/borrow_direct/authentication.rb

Overview

The BD Authorization API borrowdirect.pbworks.com/w/file/83346673/Authorization%20Service.docx

For now, always calls with ‘patron’ type.

Constant Summary collapse

@@api_path =
"/portal-service/user/authentication"

Instance Attribute Summary collapse

Attributes inherited from Request

#auth_id, #expected_error_codes, #http_client, #http_method, #last_request_json, #last_request_response, #last_request_time, #last_request_uri, #timeout

Instance Method Summary collapse

Methods inherited from Request

#fetch_auth_id!, #need_auth_id, #request, #request_headers, #with_auth_id

Constructor Details

#initialize(patron_barcode, patron_library_symbol = Defaults.library_symbol, api_key = Defaults.api_key) ⇒ Authentication

BorrowDirect::Authentication.new(barcode) BorrowDirect::Authentication.new(barcode, library_symbol)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/borrow_direct/authentication.rb', line 16

def initialize(patron_barcode, 
               patron_library_symbol = Defaults.library_symbol,
               api_key = Defaults.api_key)
  super(@@api_path)

  @patron_barcode = patron_barcode
  @patron_library_symbol = patron_library_symbol

  @api_key = api_key

  unless @api_key
    raise ArgumentError, "BorrowDirect::Authentication requires an api key as third paramter or set in BorrowDirect::Defaults.api_key"
  end
end

Instance Attribute Details

#patron_barcodeObject (readonly)

Returns the value of attribute patron_barcode.



10
11
12
# File 'lib/borrow_direct/authentication.rb', line 10

def patron_barcode
  @patron_barcode
end

#patron_library_symbolObject (readonly)

Returns the value of attribute patron_library_symbol.



10
11
12
# File 'lib/borrow_direct/authentication.rb', line 10

def patron_library_symbol
  @patron_library_symbol
end

Instance Method Details

#authentication_requestObject

Returns raw Hash results of the Authentication request See also #get_auth_id



33
34
35
# File 'lib/borrow_direct/authentication.rb', line 33

def authentication_request
  self.request authentication_request_hash(self.patron_barcode, self.patron_library_symbol)
end

#authentication_request_hash(patron_barcode, library_symbol) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/borrow_direct/authentication.rb', line 51

def authentication_request_hash(patron_barcode, library_symbol)
  {
    "ApiKey"        => @api_key,
    "PartnershipId" => BorrowDirect::Defaults.partnership_id,
    "UserGroup"     => "patron",
    "LibrarySymbol" => library_symbol,
    "PatronId"      => patron_barcode
  } 
end

#get_auth_idObject

Makes a request and returns the “AId” value which is used as input “AuthorizationId” in other API calls.

If one can’t be obtained for some reason, will raise BorrowDirect::Error



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

def get_auth_id
  response = authentication_request

  if response["AuthorizationId"]
    return response["AuthorizationId"]
  else
    raise BorrowDirect::Error.new("Could not obtain AId from Authorization API call: #{response.inspect}")
  end
end