Class: CMU::Person

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

Overview

A CMU::Person class

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(andrew_id) ⇒ Person

::nodoc


33
34
35
36
37
38
# File 'lib/cmu-person/person.rb', line 33

def initialize(andrew_id)
  ldap = Net::LDAP.new(:host => 'ldap.andrew.cmu.edu')
   @data = ldap.search(:base => 'ou=Person,dc=cmu,dc=edu', 
               :filter => 'cmuAndrewId=' + andrew_id).first
   raise CMU::RecordNotFound if @data.nil?
end

Class Method Details

.find(andrew_id) ⇒ CMU::Person

Attempts to create and return a new CMU::Person from the CMU LDAP directory.

Raises:



13
14
15
# File 'lib/cmu-person/person.rb', line 13

def self.find(andrew_id)
  CMU::Person.new(andrew_id)
end

.find_by_andrew_id(andrew_id) ⇒ CMU::Person



23
24
25
26
27
28
29
# File 'lib/cmu-person/person.rb', line 23

def self.find_by_andrew_id(andrew_id)
 begin
   find(andrew_id)
 rescue
   nil
  end
end

Instance Method Details

#andrew_idString

Returns the Andrew ID of the CMU Person



43
44
45
# File 'lib/cmu-person/person.rb', line 43

def andrew_id
  @andrew_id ||= @data[:cmuAndrewId].last
end

#departmentString?

Returns department for the CMU Person



133
134
135
# File 'lib/cmu-person/person.rb', line 133

def department
 @department ||= @data[:cmudepartment].last
end

#emailString

Returns the email of the CMU Person. If the CMU Person has

listed a preferred email, it will be displayed. Otherwise,
the official Andrew Email will be used.


73
74
75
76
77
78
79
# File 'lib/cmu-person/person.rb', line 73

def email
  if @data.attribute_names.include?(:cmupreferredmail)
  @email ||= @data[:cmupreferredmail].last
 else
    @email ||= @data[:mail].last
  end
end

#first_nameString

Returns the first name of the CMU Person



64
65
66
# File 'lib/cmu-person/person.rb', line 64

def first_name
@first_name ||= @data[:givenname].last
end

#gradeString?

Returns the String representation of the grade of the CMU Person



121
122
123
124
125
126
127
# File 'lib/cmu-person/person.rb', line 121

def grade
  if @data.attribute_names.include?(:cmustudentclass)
    @grade ||= @data[:cmustudentclass].last
  else
    @grade ||= nil
  end
end

#inspectObject

::nodoc


147
148
149
150
# File 'lib/cmu-person/person.rb', line 147

def inspect
  fields = %w(andrew_id first_name last_name email phone type title)
  @inspect ||= "<CMU::Person #{fields.collect{|f| f + ': ' + self.send(f.to_sym).inspect}.join(', ')}>"
end

#last_nameString

Returns the last name of the CMU Person



57
58
59
# File 'lib/cmu-person/person.rb', line 57

def last_name
@last_name ||= @data[:sn].last
end

#nameString

Returns the full name of the CMU Person



50
51
52
# File 'lib/cmu-person/person.rb', line 50

def name
@name ||= @data[:cn].last
end

#phoneString?

Returns the phone number for the CMU Person



85
86
87
88
89
90
91
# File 'lib/cmu-person/person.rb', line 85

def phone
  if @data.attribute_names.include?(:cmupreferredtelephone)
    @phone ||= @data[:cmupreferredtelephone].last.gsub(/[^0-9]/,'')
  else
    @phone ||= nil
  end
end

#schoolString?

Returns the “college” for the CMU Person



140
141
142
143
# File 'lib/cmu-person/person.rb', line 140

def school
  filters = ['Student Employment', 'Undergraduate Admission and Student Aid', 'VP For Campus Affairs']
 @school ||= @data[:edupersonschoolcollegename].reject{|c| filters.include?(c)}.last
end

#titleString?

Returns the official title for the CMU Person



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cmu-person/person.rb', line 104

def title
    if @data.attribute_names.include?(:title)
    @title ||= @data[:title].last
  else
    if @data.attribute_names.include?(:cmutitle)
      @title ||= @data[:cmutitle].last
    else
      @title ||= nil
    end
  end    
end

#to_sObject

::nodoc


154
155
156
# File 'lib/cmu-person/person.rb', line 154

def to_s
   @to_s ||= "<CMU::Person \"#{self.send(:name)} (#{self.send(:andrew_id)})\">"
end

#typeString

Returns the type for the CMU Person



96
97
98
# File 'lib/cmu-person/person.rb', line 96

def type 
  @type ||= @data[:edupersonaffiliation].last
end