Class: Alma::User

Inherits:
Object
  • Object
show all
Extended by:
ApiDefaults, Forwardable
Defined in:
lib/alma/user.rb

Defined Under Namespace

Classes: ResponseError

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiDefaults

apikey, bibs_base_path, region, timeout

Constructor Details

#initialize(response) ⇒ User

Returns a new instance of User.



31
32
33
34
35
# File 'lib/alma/user.rb', line 31

def initialize(response)
  @raw_response = response
  @response = response.parsed_response
  validate(response)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

Access the top level JSON attributes as object methods



72
73
74
75
# File 'lib/alma/user.rb', line 72

def method_missing(name)
  return response[name.to_s] if has_key?(name.to_s)
  super.method_missing name
end

Class Method Details

.authenticate(args) ⇒ Boolean

Authenticates a Alma user with their Alma Password

Parameters:

  • args (Hash)

Options Hash (args):

  • :user_id (String)

    The unique id of the user

  • :password (String)

    The users local alma password

Returns:

  • (Boolean)

    Whether or not the user Successfully authenticated



20
21
22
23
24
25
# File 'lib/alma/user.rb', line 20

def self.authenticate(args)
  user_id = args.delete(:user_id) { raise ArgumentError }
  args.merge!({op: 'auth'})
  response = HTTParty.post("#{users_base_path}/#{user_id}", query: args, headers: headers, timeout: timeout)
  response.code == 204
end

.find(user_id, args = {}) ⇒ Object



8
9
10
11
12
13
# File 'lib/alma/user.rb', line 8

def self.find(user_id, args={})
  args[:expand] ||= "fees,requests,loans"
  response = HTTParty.get("#{self.users_base_path}/#{user_id}", query: args, headers: headers, timeout: timeout)

  Alma::User.new response
end

Instance Method Details

#emailObject



122
123
124
# File 'lib/alma/user.rb', line 122

def email
  self["contact_info"]["email"].map { |e| e["email_address"] }
end

#finesObject



89
90
91
# File 'lib/alma/user.rb', line 89

def fines
  Alma::Fine.where_user(id)
end

#idObject



54
55
56
# File 'lib/alma/user.rb', line 54

def id
  self['primary_id']
end

#loans(args = {}) ⇒ Object



98
99
100
# File 'lib/alma/user.rb', line 98

def loans(args={})
    @loans ||= Alma::Loan.where_user(id, args)
end

#loggableObject



37
38
39
40
# File 'lib/alma/user.rb', line 37

def loggable
  { uri: @raw_response&.request&.uri.to_s
  }.select { |k, v| !(v.nil? || v.empty?) }
end

#preferred_emailObject



118
119
120
# File 'lib/alma/user.rb', line 118

def preferred_email
  self["contact_info"]["email"].select { |k, v| k["preferred"] }.first["email_address"]
end

#preferred_first_nameObject



126
127
128
129
# File 'lib/alma/user.rb', line 126

def preferred_first_name
  pref_first = self["pref_first_name"] unless self["pref_first_name"] == ""
  pref_first || self["first_name"] || ""
end

#preferred_last_nameObject



136
137
138
139
# File 'lib/alma/user.rb', line 136

def preferred_last_name
  pref_last = self["pref_last_name"] unless self["pref_last_name"] == ""
  pref_last || self["last_name"]
end

#preferred_middle_nameObject



131
132
133
134
# File 'lib/alma/user.rb', line 131

def preferred_middle_name
  pref_middle = self["pref_middle_name"] unless self["pref_middle_name"] == ""
  pref_middle  || self["middle_name"] || ""
end

#preferred_nameObject



145
146
147
# File 'lib/alma/user.rb', line 145

def preferred_name
  "#{preferred_first_name} #{preferred_middle_name} #{preferred_last_name} #{preferred_suffix}"
end

#preferred_suffixObject



141
142
143
# File 'lib/alma/user.rb', line 141

def preferred_suffix
  self["pref_name_suffix"] || ""
end

#renew_all_loansObject



114
115
116
# File 'lib/alma/user.rb', line 114

def renew_all_loans
  renew_multiple_loans(loans.map(&:loan_id))
end

#renew_loan(loan_id) ⇒ Object



102
103
104
105
106
107
# File 'lib/alma/user.rb', line 102

def renew_loan(loan_id)
  response = self.class.send_loan_renewal_request({user_id: id, loan_id: loan_id})
  if response.renewed?
    @recheck_loans ||= true
  end
end

#renew_multiple_loans(loan_ids) ⇒ Object



110
111
112
# File 'lib/alma/user.rb', line 110

def renew_multiple_loans(loan_ids)
  loan_ids.map { |id| renew_loan(id) }
end

#requestsObject



93
94
95
# File 'lib/alma/user.rb', line 93

def requests
  Alma::UserRequest.where_user(id)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/alma/user.rb', line 77

def respond_to_missing?(name, include_private = false)
  has_key?(name.to_s) || super
end

#responseObject



50
51
52
# File 'lib/alma/user.rb', line 50

def response
  @response
end

#save!Object

Persist the user in it’s current state back to Alma



83
84
85
86
# File 'lib/alma/user.rb', line 83

def save!
  response = HTTParty.put("#{users_base_path}/#{id}", timeout: timeout, headers: headers, body: to_json)
  get_body_from(response)
end

#total_finesObject



58
59
60
# File 'lib/alma/user.rb', line 58

def total_fines
  response.dig('fees','value') || "0"
end

#total_loansObject



66
67
68
# File 'lib/alma/user.rb', line 66

def total_loans
  response.dig('loans','value') || "0"
end

#total_requestsObject



62
63
64
# File 'lib/alma/user.rb', line 62

def total_requests
  response.dig('requests','value') || "0"
end

#validate(response) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/alma/user.rb', line 42

def validate(response)
  if response.code != 200
    log = loggable.merge(response.parsed_response)
    error = "The user was not found."
    raise ResponseError.new(error, log)
  end
end