Class: Alma::User
Defined Under Namespace
Classes: ResponseError
Class Method Summary
collapse
Instance Method Summary
collapse
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
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: , 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: , timeout: timeout)
Alma::User.new response
end
|
Instance Method Details
#email ⇒ Object
122
123
124
|
# File 'lib/alma/user.rb', line 122
def email
self["contact_info"]["email"].map { |e| e["email_address"] }
end
|
#id ⇒ Object
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
|
#loggable ⇒ Object
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_email ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
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_suffix ⇒ Object
141
142
143
|
# File 'lib/alma/user.rb', line 141
def preferred_suffix
self["pref_name_suffix"] || ""
end
|
#renew_all_loans ⇒ Object
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
|
#respond_to_missing?(name, include_private = false) ⇒ 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
|
#response ⇒ Object
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: , body: to_json)
get_body_from(response)
end
|
#total_fines ⇒ Object
58
59
60
|
# File 'lib/alma/user.rb', line 58
def total_fines
response.dig('fees','value') || "0"
end
|
#total_loans ⇒ Object
66
67
68
|
# File 'lib/alma/user.rb', line 66
def total_loans
response.dig('loans','value') || "0"
end
|
#total_requests ⇒ Object
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
|