Class: Alma::User

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/alma/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_body) ⇒ User

Returns a new instance of User.



30
31
32
33
# File 'lib/alma/user.rb', line 30

def initialize(response_body)
  @response = response_body
  @recheck_loans = true
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



45
46
47
48
# File 'lib/alma/user.rb', line 45

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

Parameters:

  • args (Hash)

Options Hash (args):

  • :user_id (String)

    The unique id of the user

  • :password (String)

    The users local alma password

Returns:

  • (Boolean)

    Whether or not the user Successfully authenticated



19
20
21
22
23
24
# File 'lib/alma/user.rb', line 19

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

.find(user_id) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/alma/user.rb', line 5

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

Instance Method Details

#emailObject



116
117
118
# File 'lib/alma/user.rb', line 116

def email
  self["contact_info"]["email"].map { |e| e["email_address"] }
end

#finesObject



62
63
64
65
66
67
68
69
# File 'lib/alma/user.rb', line 62

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
end

#idObject



39
40
41
# File 'lib/alma/user.rb', line 39

def id
  self['primary_id']
end

#loans(args = {}) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/alma/user.rb', line 80

def loans(args={})
  unless @loans && !recheck_loans?
    @loans = send_loans_request(args)
    @recheck_loans = false
  end
  @loans
end

#preferred_emailObject



111
112
113
# File 'lib/alma/user.rb', line 111

def preferred_email
  self["contact_info"]["email"].select { |k, v| k["preferred"] }.first["email_address"]
end

#recheck_loans?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/alma/user.rb', line 106

def recheck_loans?
  @recheck_loans
end

#renew_all_loansObject



101
102
103
# File 'lib/alma/user.rb', line 101

def renew_all_loans
  renew_multiple_loans(loans.map(&:loan_id))
end

#renew_loan(loan_id) ⇒ Object



88
89
90
91
92
93
# File 'lib/alma/user.rb', line 88

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



96
97
98
# File 'lib/alma/user.rb', line 96

def renew_multiple_loans(loan_ids)
  loan_ids.map { |id| renew_loan(id) }
end

#requestsObject



71
72
73
74
75
76
77
# File 'lib/alma/user.rb', line 71

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

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/alma/user.rb', line 50

def respond_to_missing?(name, include_private = false)
  has_key?(name.to_s) || super
end

#responseObject



35
36
37
# File 'lib/alma/user.rb', line 35

def response
  @response
end

#save!Object

Persist the user in it’s current state back to Alma



56
57
58
59
# File 'lib/alma/user.rb', line 56

def save!
  response = HTTParty.put("#{users_base_path}/#{id}", headers: headers, body: to_json)
  get_body_from(response)
end