class Alma::User
Public Class Methods
Authenticates a Alma
user with their Alma
Password @param [Hash] args @option args [String] :user_id The unique id of the user @option args [String] :password The users local alma password @return [Boolean] Whether or not the user Successfully authenticated
# File lib/alma/user.rb, line 22 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
# File lib/alma/user.rb, line 10 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
# File lib/alma/user.rb, line 33 def initialize(response) @raw_response = response @response = response.parsed_response validate(response) end
Private Class Methods
Attempts to renew a single item for a user @param [Hash] args @option args [String] :user_id The unique id of the user @option args [String] :loan_id The unique id of the loan @option args [String] :user_id_type Type of identifier being used to search. OPTIONAL @return [RenewalResponse] Object indicating the renewal message
# File lib/alma/user.rb, line 161 def self.send_loan_renewal_request(args) loan_id = args.delete(:loan_id) { raise ArgumentError } user_id = args.delete(:user_id) { raise ArgumentError } params = { op: "renew" } response = HTTParty.post("#{users_base_path}/#{user_id}/loans/#{loan_id}", query: params, headers: headers) RenewalResponse.new(response) end
Attempts to renew multiple items for a user @param [Hash] args @option args [String] :user_id The unique id of the user @option args [Array<String>] :loan_ids The unique ids of the loans @option args [String] :user_id_type Type of identifier being used to search. OPTIONAL @return [Array<RenewalResponse>] Array of Objects indicating the renewal messages
# File lib/alma/user.rb, line 175 def self.send_multiple_loan_renewal_requests(args) loan_ids = args.delete(:loan_ids) { raise ArgumentError } loan_ids.map { |id| Alma::User.send_loan_renewal_request(args.merge(loan_id: id)) } end
# File lib/alma/user.rb, line 185 def self.users_base_path "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users" end
Public Instance Methods
# File lib/alma/user.rb, line 124 def email self["contact_info"]["email"].map { |e| e["email_address"] } end
# File lib/alma/user.rb, line 91 def fines Alma::Fine.where_user(id) end
# File lib/alma/user.rb, line 56 def id self["primary_id"] end
# File lib/alma/user.rb, line 100 def loans(args = {}) @loans ||= Alma::Loan.where_user(id, args) end
# File lib/alma/user.rb, line 39 def loggable { uri: @raw_response&.request&.uri.to_s }.select { |k, v| !(v.nil? || v.empty?) } end
Access the top level JSON attributes as object methods
# File lib/alma/user.rb, line 74 def method_missing(name) return response[name.to_s] if has_key?(name.to_s) super.method_missing name end
# File lib/alma/user.rb, line 120 def preferred_email self["contact_info"]["email"].select { |k, v| k["preferred"] }.first["email_address"] end
# File lib/alma/user.rb, line 128 def preferred_first_name pref_first = self["pref_first_name"] unless self["pref_first_name"] == "" pref_first || self["first_name"] || "" end
# File lib/alma/user.rb, line 138 def preferred_last_name pref_last = self["pref_last_name"] unless self["pref_last_name"] == "" pref_last || self["last_name"] end
# File lib/alma/user.rb, line 133 def preferred_middle_name pref_middle = self["pref_middle_name"] unless self["pref_middle_name"] == "" pref_middle || self["middle_name"] || "" end
# File lib/alma/user.rb, line 147 def preferred_name "#{preferred_first_name} #{preferred_middle_name} #{preferred_last_name} #{preferred_suffix}" end
# File lib/alma/user.rb, line 143 def preferred_suffix self["pref_name_suffix"] || "" end
# File lib/alma/user.rb, line 116 def renew_all_loans renew_multiple_loans(loans.map(&:loan_id)) end
# File lib/alma/user.rb, line 104 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
# File lib/alma/user.rb, line 112 def renew_multiple_loans(loan_ids) loan_ids.map { |id| renew_loan(id) } end
# File lib/alma/user.rb, line 95 def requests Alma::UserRequest.where_user(id) end
# File lib/alma/user.rb, line 79 def respond_to_missing?(name, include_private = false) has_key?(name.to_s) || super end
# File lib/alma/user.rb, line 52 def response @response end
Persist the user in it's current state back to Alma
# File lib/alma/user.rb, line 85 def save! response = HTTParty.put("#{users_base_path}/#{id}", timeout: timeout, headers: headers, body: to_json) get_body_from(response) end
# File lib/alma/user.rb, line 60 def total_fines response.dig("fees", "value") || "0" end
# File lib/alma/user.rb, line 68 def total_loans response.dig("loans", "value") || "0" end
# File lib/alma/user.rb, line 64 def total_requests response.dig("requests", "value") || "0" end
# File lib/alma/user.rb, line 44 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
Private Instance Methods
# File lib/alma/user.rb, line 180 def get_body_from(response) JSON.parse(response.body) end
# File lib/alma/user.rb, line 193 def headers self.class.headers end
# File lib/alma/user.rb, line 189 def users_base_path self.class.users_base_path end