lib/alma/user.rb in alma-0.2.8 vs lib/alma/user.rb in alma-0.3.1
- old
+ new
@@ -1,48 +1,75 @@
module Alma
class User
+ class ResponseError < Alma::StandardError
+ end
extend Forwardable
+ extend Alma::ApiDefaults
- def self.find(user_id)
- response = HTTParty.get("#{self.users_base_path}/#{user_id}", headers: headers)
- if response.code == 200
- Alma::User.new JSON.parse(response.body)
- else
- raise StandardError, parse(response.body)
- end
+ 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
# 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
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)
+ response = HTTParty.post("#{users_base_path}/#{user_id}", query: args, headers: headers, timeout: timeout)
response.code == 204
end
# The User object can respond directly to Hash like access of attributes
def_delegators :response, :[], :[]=, :has_key?, :keys, :to_json
- def initialize(response_body)
- @response = response_body
- @recheck_loans = true
+ def initialize(response)
+ @raw_response = response
+ @response = response.parsed_response
+ validate(response)
end
+ def loggable
+ { uri: @raw_response&.request&.uri.to_s
+ }.select { |k, v| !(v.nil? || v.empty?) }
+ end
+
+ 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
+
def response
@response
end
def id
self['primary_id']
end
+ def total_fines
+ response.dig('fees','value') || "0"
+ end
+ def total_requests
+ response.dig('requests','value') || "0"
+ end
+
+ def total_loans
+ response.dig('loans','value') || "0"
+ end
+
+
# Access the top level JSON attributes as object methods
def method_missing(name)
return response[name.to_s] if has_key?(name.to_s)
super.method_missing name
end
@@ -52,39 +79,26 @@
end
# Persist the user in it's current state back to Alma
def save!
- response = HTTParty.put("#{users_base_path}/#{id}", headers: headers, body: to_json)
+ response = HTTParty.put("#{users_base_path}/#{id}", timeout: timeout, headers: headers, body: to_json)
get_body_from(response)
end
def fines
- response = HTTParty.get("#{users_base_path}/#{id}/fees", headers: headers)
- if response.code == 200
- Alma::FineSet.new get_body_from(response)
- else
- raise StandardError, get_body_from(response)
- end
+ Alma::Fine.where_user(id)
end
def requests
- #TODO Handle Additional Parameters
- #TODO Handle Pagination
- #TODO Handle looping through all results
- response = HTTParty.get("#{users_base_path}/#{id}/requests", headers: headers)
- Alma::RequestSet.new(get_body_from(response))
+ Alma::UserRequest.where_user(id)
end
def loans(args={})
- unless @loans && !recheck_loans?
- @loans = send_loans_request(args)
- @recheck_loans = false
- end
- @loans
+ @loans ||= Alma::Loan.where_user(id, args)
end
def renew_loan(loan_id)
response = self.class.send_loan_renewal_request({user_id: id, loan_id: loan_id})
if response.renewed?
@@ -95,42 +109,49 @@
def renew_multiple_loans(loan_ids)
loan_ids.map { |id| renew_loan(id) }
end
-
def renew_all_loans
renew_multiple_loans(loans.map(&:loan_id))
end
-
- def recheck_loans?
- @recheck_loans
- end
-
-
def preferred_email
self["contact_info"]["email"].select { |k, v| k["preferred"] }.first["email_address"]
end
-
def email
self["contact_info"]["email"].map { |e| e["email_address"] }
end
+ def preferred_first_name
+ pref_first = self["pref_first_name"] unless self["pref_first_name"] == ""
+ pref_first || self["first_name"] || ""
+ end
- private
+ def preferred_middle_name
+ pref_middle = self["pref_middle_name"] unless self["pref_middle_name"] == ""
+ pref_middle || self["middle_name"] || ""
+ end
- def send_loans_request(args={})
- #TODO Handle looping through all results
+ def preferred_last_name
+ pref_last = self["pref_last_name"] unless self["pref_last_name"] == ""
+ pref_last || self["last_name"]
+ end
- # Always expand renewable unless you really don't want to
- args["expand"] ||= "renewable"
- response = HTTParty.get("#{users_base_path}/#{id}/loans", query: args, headers: headers)
- Alma::LoanSet.new(get_body_from(response))
+ def preferred_suffix
+ self["pref_name_suffix"] || ""
end
+ def preferred_name
+ "#{preferred_first_name} #{preferred_middle_name} #{preferred_last_name} #{preferred_suffix}"
+ end
+
+
+
+ private
+
# 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
@@ -138,11 +159,11 @@
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(JSON.parse(response.body))
+ 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
@@ -165,23 +186,10 @@
def users_base_path
self.class.users_base_path
end
- def self.headers
- { "Authorization": "apikey #{self.apikey}",
- "Accept": "application/json",
- "Content-Type": "application/json" }
- end
-
-
def headers
self.class.headers
end
-
-
- def self.apikey
- Alma.configuration.apikey
- end
-
end
end