Class: Alma::User
Defined Under Namespace
Classes: ResponseError
Class Method Summary
collapse
Instance Method Summary
collapse
apikey, bibs_base_path, configuration_base_path, items_base_path, region, timeout
Constructor Details
#initialize(response) ⇒ User
Returns a new instance of User.
32
33
34
35
36
|
# File 'lib/alma/user.rb', line 32
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
22
23
24
25
26
27
|
# File 'lib/alma/user.rb', line 22
def self.authenticate(args)
user_id = args.delete(:user_id) { raise ArgumentError }
args.merge!({ op: "auth" })
response = Net.post("#{users_base_path}/#{user_id}", query: args, headers:, timeout:)
response.code == 204
end
|
.find(user_id, args = {}) ⇒ Object
10
11
12
13
14
15
|
# File 'lib/alma/user.rb', line 10
def self.find(user_id, args = {})
args[:expand] ||= "fees,requests,loans"
response = Net.get("#{self.users_base_path}/#{user_id}", query: args.compact_blank, headers:, timeout:)
Alma::User.new response
end
|
Instance Method Details
#email ⇒ Object
118
119
120
|
# File 'lib/alma/user.rb', line 118
def email
self["contact_info"]["email"].map { |e| e["email_address"] }
end
|
#id ⇒ Object
55
56
57
|
# File 'lib/alma/user.rb', line 55
def id
self["primary_id"]
end
|
#loans(args = {}) ⇒ Object
95
96
97
|
# File 'lib/alma/user.rb', line 95
def loans(args = {})
@loans ||= Alma::Loan.where_user(id, args)
end
|
#loggable ⇒ Object
38
39
40
41
|
# File 'lib/alma/user.rb', line 38
def loggable
{ uri: @raw_response&.request&.uri.to_s
}.select { |k, v| !(v.nil? || v.empty?) }
end
|
#preferred_email ⇒ Object
114
115
116
|
# File 'lib/alma/user.rb', line 114
def preferred_email
self["contact_info"]["email"].select { |k, v| k["preferred"] }.first["email_address"]
end
|
#preferred_first_name ⇒ Object
122
123
124
125
|
# File 'lib/alma/user.rb', line 122
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
132
133
134
135
|
# File 'lib/alma/user.rb', line 132
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
127
128
129
130
|
# File 'lib/alma/user.rb', line 127
def preferred_middle_name
pref_middle = self["pref_middle_name"] unless self["pref_middle_name"] == ""
pref_middle || self["middle_name"] || ""
end
|
#preferred_name ⇒ Object
141
142
143
|
# File 'lib/alma/user.rb', line 141
def preferred_name
"#{preferred_first_name} #{preferred_middle_name} #{preferred_last_name} #{preferred_suffix}"
end
|
#preferred_suffix ⇒ Object
137
138
139
|
# File 'lib/alma/user.rb', line 137
def preferred_suffix
self["pref_name_suffix"] || ""
end
|
#renew_all_loans ⇒ Object
110
111
112
|
# File 'lib/alma/user.rb', line 110
def renew_all_loans
renew_multiple_loans(loans.map(&:loan_id))
end
|
#renew_loan(loan_id) ⇒ Object
99
100
101
102
103
104
|
# File 'lib/alma/user.rb', line 99
def renew_loan(loan_id)
response = self.class.send_loan_renewal_request({ user_id: id, loan_id: })
if response.renewed?
@recheck_loans ||= true
end
end
|
#renew_multiple_loans(loan_ids) ⇒ Object
106
107
108
|
# File 'lib/alma/user.rb', line 106
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
51
52
53
|
# File 'lib/alma/user.rb', line 51
def response
@response
end
|
#save! ⇒ Object
Persist the user in it’s current state back to Alma
82
83
84
85
|
# File 'lib/alma/user.rb', line 82
def save!
response = Net.put("#{users_base_path}/#{id}", timeout:, headers:, body: to_json)
get_body_from(response)
end
|
#total_fines ⇒ Object
59
60
61
|
# File 'lib/alma/user.rb', line 59
def total_fines
response.dig("fees", "value") || 0.0
end
|
#total_loans ⇒ Object
67
68
69
|
# File 'lib/alma/user.rb', line 67
def total_loans
response.dig("loans", "value") || "0"
end
|
#total_requests ⇒ Object
63
64
65
|
# File 'lib/alma/user.rb', line 63
def total_requests
response.dig("requests", "value") || "0"
end
|
#validate(response) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/alma/user.rb', line 43
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
|