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
# 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.

Parameters:

  • the (String)

    Andrew ID to search for

Returns:

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

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

Unlink (see #find), this method will not raise an exception if the Andrew
ID is not found.

Parameters:

  • the (String)

    Andrew ID to search for

Returns:



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

Returns:

  • (String)

    the Andrew ID of the CMU Person



42
43
44
# File 'lib/cmu_person/person.rb', line 42

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

#departmentString?

Returns department for the CMU Person

Returns:

  • (String, nil)

    the department for the current CMU Person or nil if the CMU Person is a student and/or has no department



132
133
134
# File 'lib/cmu_person/person.rb', line 132

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.

Returns:

  • (String)

    the email of the current CMU Person



72
73
74
75
76
77
78
# File 'lib/cmu_person/person.rb', line 72

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

Returns:

  • (String)

    the first name of the current CMU Person



63
64
65
# File 'lib/cmu_person/person.rb', line 63

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

#gradeString?

Returns the String representation of the grade of the CMU Person

Returns:

  • (String, nil)

    the String representation of the grade of the CMU Person (‘Freshman’, ‘Sophomore’, ‘Junior’, ‘Senior’, ‘Masters’, ‘Doctorate’) or nil for Faculty and Staff



120
121
122
123
124
125
126
# File 'lib/cmu_person/person.rb', line 120

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

#inspectObject

::nodoc


146
147
148
149
# File 'lib/cmu_person/person.rb', line 146

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

Returns:

  • (String)

    the last name of the current CMU Person



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

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

#nameString

Returns the full name of the CMU Person

Returns:

  • (String)

    the full name of the current CMU Person



49
50
51
# File 'lib/cmu_person/person.rb', line 49

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

#phoneString?

Returns the phone number for the CMU Person

Returns:

  • (String, nil)

    the phone number of the current CMU Person or nil if the person did not provide a phone number



84
85
86
87
88
89
90
# File 'lib/cmu_person/person.rb', line 84

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

Returns:

  • (String, nil)

    the actual college the CMU Person belongs to



139
140
141
142
# File 'lib/cmu_person/person.rb', line 139

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

Returns:

  • (String, nil)

    the official title for the CMU Person or nil if the CMU Person has not provided a title



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

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


153
154
155
# File 'lib/cmu_person/person.rb', line 153

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

#typeString

Returns the type for the CMU Person

Returns:

  • (String)

    the type or role of the CMU Person



95
96
97
# File 'lib/cmu_person/person.rb', line 95

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