lib/alma/user.rb in alma-0.2.4 vs lib/alma/user.rb in alma-0.2.5

- old
+ new

@@ -1,141 +1,177 @@ module Alma - class User < AlmaRecord - extend Alma::Api + class User + extend Forwardable - attr_accessor :id + 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 + end - def post_initialize - @id = response['primary_id'].to_s + # 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.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 end + def response + @response + end + + def id + self['primary_id'] + 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 + + def respond_to_missing?(name, include_private = false) + has_key?(name.to_s) || super + 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) + get_body_from(response) + end + + def fines - self.class.get_fines({user_id: self.id}) + 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 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)) + end + + def loans unless @loans && !recheck_loans? - @loans = self.class.get_loans({user_id: self.id}) + @loans = send_loans_request @recheck_loans = false end @loans end def renew_loan(loan_id) - response = self.class.renew_loan({user_id: self.id, loan_id: loan_id}) + response = send_loan_renewal_request({user_id: id, loan_id: loan_id}) if response.renewed? @recheck_loans ||= true end - response + get_body_from response end + 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 requests - self.class.get_requests({user_id:self.id}) + + def preferred_email + self["contact_info"]["email"].select { |k, v| k["preferred"] }.first["email_address"] end - class << self - # Static methods that do the actual querying - def find(args = {}) - #TODO Handle Search Queries - #TODO Handle Pagination - #TODO Handle looping through all results + def email + self["contact_info"]["email"].map { |e| e["email_address"] } + end - return find_by_id(user_id: args[:user_id]) if args.fetch(:user_id, nil) - params = query_merge args - response = resources.almaws_v1_users.get(params) - Alma::UserSet.new(response) - end - def find_by_id(user_id_hash) - params = query_merge user_id_hash - response = resources.almaws_v1_users.user_id.get(params) - User.new(response['user']) - end + private - def get_fines(args) - #TODO Handle Additional Parameters - #TODO Handle Pagination - #TODO Handle looping through all results - params = query_merge args - response = resources.almaws_v1_users.user_id_fees.get(params) - Alma::FineSet.new(response) - end + def send_loans_request + #TODO Handle Additional Parameters + #TODO Handle Pagination + #TODO Handle looping through all results + response = HTTParty.get("#{users_base_path}/#{id}/loans", headers: headers) + Alma::LoanSet.new(get_body_from(response)) + end - def get_loans(args) - #TODO Handle Additional Parameters - #TODO Handle Pagination - #TODO Handle looping through all results - params = query_merge args - response = resources.almaws_v1_users.user_id_loans.get(params) - Alma::LoanSet.new(response) - end + # 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 + def send_loan_renewal_request(args) + user_id = args.delete(:user_id) { raise ArgumentError } + loan_id = args.delete(:loan_id) { raise ArgumentError } + params = {op: 'renew'} + response = HTTParty.post("#{users_base_path}/#{id}/loans/#{loan_id}", query: params, headers: headers) + RenewalResponse.new(get_body_from(response)) + end - def get_requests(args) - #TODO Handle Additional Parameters - #TODO Handle Pagination - #TODO Handle looping through all results - params = query_merge args - response = resources.almaws_v1_users.user_id_requests.get(params) - Alma::RequestSet.new(response) - end - def authenticate(args) - # Authenticates a Alma user with their Alma Password - args.merge!({op: 'auth'}) - params = query_merge args - response = resources.almaws_v1_users.user_id.post(params) - response.code == 204 - end + def get_body_from(response) + JSON.parse(response.body) + end - # 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 - def renew_loan(args) - args.merge!({op: 'renew'}) - params = query_merge args - response = resources.almaws_v1_users.user_id_loans_loan_id.post(params) - RenewalResponse.new(response) - end - # Attempts to renew a 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 Array of loan ids - # @option args [String] :user_id_type Type of identifier being used to search. OPTIONAL - # @return [Array<RenewalResponse>] Object indicating the renewal message - def renew_multiple_loans(args) + def self.users_base_path + "https://api-na.hosted.exlibrisgroup.com/almaws/v1/users" + end - if args.fetch(:loans_ids, nil).respond_to? :map - args.delete(:loan_ids).map do |loan_id| - renew_loan(args.merge(loan_id: loan_id)) - end - else - [] - end - end + 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 set_wadl_filename - 'user.wadl' - end + + def self.apikey + Alma.configuration.apikey end + + end end -end \ No newline at end of file