Class: MaisPersonClient::Person

Inherits:
Object
  • Object
show all
Defined in:
lib/mais_person_client/person.rb

Overview

Model for a Person from the MAIS Person API

Defined Under Namespace

Classes: Address, AffData, Affiliation, Department, Email, Identifier, Location, PersonName, Place, Telephone, Url

Constant Summary collapse

HOME_ADDRESS_TYPES =
%w[home permanent].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Person

Returns a new instance of Person.



26
27
28
29
# File 'lib/mais_person_client/person.rb', line 26

def initialize(xml)
  @xml = Nokogiri::XML(xml)
  @xml.remove_namespaces!
end

Instance Attribute Details

#xmlObject (readonly)

Returns the value of attribute xml.



24
25
26
# File 'lib/mais_person_client/person.rb', line 24

def xml
  @xml
end

Instance Method Details

#academic_council?Boolean

indicates if a person is a member of the academic council

Returns:

  • (Boolean)


188
189
190
191
192
193
194
195
196
197
# File 'lib/mais_person_client/person.rb', line 188

def academic_council?
  # If there are no affiliations, the person is not a member of the academic council
  return false if affiliations.empty?

  affiliations.any? do |affiliation|
    affiliation.affdata.any? do |affdata|
      affdata.type == 'academic_council' && affdata.value&.downcase == 'member of academic council'
    end
  end
end

#affiliationsObject

Affiliations (multiple possible)



158
159
160
# File 'lib/mais_person_client/person.rb', line 158

def affiliations
  xml.xpath('//affiliation').map { |aff_node| build_affiliation(aff_node) }
end

#cardObject

Root attributes



32
33
34
# File 'lib/mais_person_client/person.rb', line 32

def card
  xml.root['card']
end

#directory_idObject



241
242
243
# File 'lib/mais_person_client/person.rb', line 241

def directory_id
  identifier_by_type('directory')
end

#display_nameObject



81
82
83
# File 'lib/mais_person_client/person.rb', line 81

def display_name
  names.find { |name| name.type == 'display' }
end

#eduperson_affiliationsObject



225
226
227
# File 'lib/mais_person_client/person.rb', line 225

def eduperson_affiliations
  xml.xpath('//edupersonaffiliation').map(&:text)
end

#eduperson_primary_affiliationObject

eduPerson attributes



221
222
223
# File 'lib/mais_person_client/person.rb', line 221

def eduperson_primary_affiliation
  xml.at_xpath('//edupersonprimaryaffiliation')&.text
end

#emailsObject

Emails (multiple possible)



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/mais_person_client/person.rb', line 114

def emails
  xml.xpath('//email').map do |email_node|
    Email.new(
      email_node['type'],
      email_node['visibility'],
      email_node.children.first&.text&.strip,
      email_node.at_xpath('user')&.text,
      email_node.at_xpath('host')&.text
    )
  end
end

#first_nameObject

Name component helpers



86
87
88
# File 'lib/mais_person_client/person.rb', line 86

def first_name
  registered_name&.first_name
end

#homepageObject



141
142
143
# File 'lib/mais_person_client/person.rb', line 141

def homepage
  urls.find { |url| url.type == 'homepage' }&.url
end

#identifier_by_type(type) ⇒ Object



211
212
213
# File 'lib/mais_person_client/person.rb', line 211

def identifier_by_type(type)
  identifiers.find { |id| id.type == type }&.value
end

#identifiersObject

Identifiers (multiple)



200
201
202
203
204
205
206
207
208
209
# File 'lib/mais_person_client/person.rb', line 200

def identifiers
  xml.xpath('//identifier').map do |id_node|
    Identifier.new(
      id_node['type'],
      id_node['visibility'],
      id_node['nval'],
      id_node.text
    )
  end
end

#job_titleObject



109
110
111
# File 'lib/mais_person_client/person.rb', line 109

def job_title
  titles.find { |title| title[:type] == 'job' }&.[](:title)
end

#last_nameObject



94
95
96
# File 'lib/mais_person_client/person.rb', line 94

def last_name
  registered_name&.last
end

#listingObject



36
37
38
# File 'lib/mais_person_client/person.rb', line 36

def listing
  xml.root['listing']
end

#locationsObject

Locations (multiple possible)



146
147
148
149
150
151
152
153
154
155
# File 'lib/mais_person_client/person.rb', line 146

def locations
  xml.xpath('//location').map do |loc_node|
    Location.new(
      loc_node['code'],
      loc_node['type'],
      loc_node['visibility'],
      loc_node.text
    )
  end
end

#middle_nameObject



90
91
92
# File 'lib/mais_person_client/person.rb', line 90

def middle_name
  registered_name&.middle
end

#mobile_phoneObject



233
234
235
# File 'lib/mais_person_client/person.rb', line 233

def mobile_phone
  telephones.find { |tel| tel.type == 'mobile' }
end

#name_attrObject



40
41
42
# File 'lib/mais_person_client/person.rb', line 40

def name_attr
  xml.root['name']
end

#namesObject

Names (multiple possible)



73
74
75
# File 'lib/mais_person_client/person.rb', line 73

def names
  xml.xpath('//name').map { |name_node| build_person_name(name_node) }
end

#orcidObject



237
238
239
# File 'lib/mais_person_client/person.rb', line 237

def orcid
  identifier_by_type('orcid')
end

#primary_effective_dateObject

returns the effective_date for the primary affiliation (affnum == '1')



178
179
180
181
182
183
184
185
# File 'lib/mais_person_client/person.rb', line 178

def primary_effective_date
  # Find the affiliation with affnum == '1' and return the effective date
  aff = affiliations.find { |a| a.affnum == '1' }
  return nil unless aff

  # effective date
  aff.effective
end

#primary_emailObject



126
127
128
# File 'lib/mais_person_client/person.rb', line 126

def primary_email
  emails.find { |email| email.type == 'primary' }&.full_email
end

#primary_org_codeObject

returns the org_id for the primary affiliation (affnum == '1')



168
169
170
171
172
173
174
175
# File 'lib/mais_person_client/person.rb', line 168

def primary_org_code
  # Find the affiliation with affnum == '1' and return the department's organization adminid
  aff = affiliations.find { |a| a.affnum == '1' }
  return nil unless aff

  # department may be nil; department.adminid holds the org code
  aff.department&.adminid
end

#primary_roleObject

returns the primary role/type for the person (from affiliation with affnum 1)



163
164
165
# File 'lib/mais_person_client/person.rb', line 163

def primary_role
  affiliations.find { |aff| aff.affnum == '1' }&.type
end

#privgroupsObject

Privacy groups



216
217
218
# File 'lib/mais_person_client/person.rb', line 216

def privgroups
  xml.xpath('//privgroup').map(&:text)
end

#regidObject



44
45
46
# File 'lib/mais_person_client/person.rb', line 44

def regid
  xml.root['regid']
end

#registered_nameObject



77
78
79
# File 'lib/mais_person_client/person.rb', line 77

def registered_name
  names.find { |name| name.type == 'registered' }
end

#relationshipObject



48
49
50
# File 'lib/mais_person_client/person.rb', line 48

def relationship
  xml.root['relationship']
end

#sourceObject



52
53
54
# File 'lib/mais_person_client/person.rb', line 52

def source
  xml.root['source']
end

#stanford_end_dateObject



64
65
66
67
68
69
70
# File 'lib/mais_person_client/person.rb', line 64

def stanford_end_date
  value = xml.root['stanfordenddate']
  return nil unless value

  stripped = value.strip
  stripped.empty? ? nil : stripped
end

#sunetidObject



56
57
58
# File 'lib/mais_person_client/person.rb', line 56

def sunetid
  xml.root['sunetid']
end

#titlesObject

Titles (multiple possible)



99
100
101
102
103
104
105
106
107
# File 'lib/mais_person_client/person.rb', line 99

def titles
  xml.xpath('//title').map do |title_node|
    {
      type: title_node['type'],
      visibility: title_node['visibility'],
      title: title_node.text
    }
  end
end

#unividObject



60
61
62
# File 'lib/mais_person_client/person.rb', line 60

def univid
  xml.root['univid']
end

#urlsObject

URLs (multiple possible)



131
132
133
134
135
136
137
138
139
# File 'lib/mais_person_client/person.rb', line 131

def urls
  xml.xpath('//url').map do |url_node|
    Url.new(
      url_node['type'],
      url_node['visibility'],
      url_node.text
    )
  end
end

#work_phoneObject



229
230
231
# File 'lib/mais_person_client/person.rb', line 229

def work_phone
  telephones.find { |tel| tel.type == 'work' }
end