cmu_person 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,48 @@
1
+ # rcov generated
2
+ coverage
3
+
4
+ # rdoc generated
5
+ rdoc
6
+
7
+ # yard generated
8
+ doc
9
+ .yardoc
10
+
11
+ # bundler
12
+ .bundle
13
+
14
+ # jeweler generated
15
+ pkg
16
+
17
+ # Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
18
+ #
19
+ # * Create a file at ~/.gitignore
20
+ # * Include files you want ignored
21
+ # * Run: git config --global core.excludesfile ~/.gitignore
22
+ #
23
+ # After doing this, these files will be ignored in all your git projects,
24
+ # saving you from having to 'pollute' every project you touch with them
25
+ #
26
+ # Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
27
+ #
28
+ # For MacOS:
29
+ #
30
+ #.DS_Store
31
+
32
+ # For TextMate
33
+ #*.tmproj
34
+ #tmtags
35
+
36
+ # For emacs:
37
+ #*~
38
+ #\#*
39
+ #.\#*
40
+
41
+ # For vim:
42
+ #*.swp
43
+
44
+ # For redcar:
45
+ #.redcar
46
+
47
+ # For rubinius:
48
+ #*.rbc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cmu_person (0.0.1)
5
+ net-ldap
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.1.3)
11
+ net-ldap (0.2.2)
12
+ redcarpet (2.0.1)
13
+ rspec (2.7.0)
14
+ rspec-core (~> 2.7.0)
15
+ rspec-expectations (~> 2.7.0)
16
+ rspec-mocks (~> 2.7.0)
17
+ rspec-core (2.7.1)
18
+ rspec-expectations (2.7.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.7.0)
21
+ yard (0.7.4)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ cmu_person!
28
+ redcarpet
29
+ rspec
30
+ yard
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 Seth Vargo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,41 @@
1
+ CMU Person
2
+ ==========
3
+
4
+ Originally inspired by [David Taylor](https://github.com/tinystatemachine)'s [andrewid](https://github.com/tinystatemachine/andrewid), CMU Person is a simple rubygem used for searching Carnegie Mellon's LDAP servers for student information.
5
+
6
+ Installation
7
+ ------------
8
+ If you want to use this gem for personal use in your terminal, just type the following command in bash:
9
+ ```bash
10
+ gem install cmu_person
11
+ ```
12
+
13
+ If you plan to use this in a Rails project (or other project managed by Bundler), add the following to your `Gemfile`:
14
+
15
+ gem 'cmu_person'
16
+
17
+ Don't forget to run the `bundle` command to install the gem!
18
+
19
+ Usage
20
+ -----
21
+
22
+ user = CMU::Person.find('svargo')
23
+ user.first_name #=> "Seth"
24
+ user.grade #=> "Junior"
25
+
26
+  
27
+
28
+ user = CMU::Person.new('i_dont_exist')
29
+ #=> CMU::RecordNotFound Exception
30
+
31
+ For a complete list of methods, view the [YARD Documentation](http://rubydoc.info/gems/cmu_person/0.0.3/frames)
32
+
33
+ License
34
+ -------
35
+ Copyright (c) 2012 Seth Vargo
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'cmu_person'
6
+ s.version = '0.0.1'
7
+ s.author = 'Seth Vargo'
8
+ s.email = 'sethvargo@gmail.com'
9
+ s.homepage = 'https://github.com/sethvargo/cmu_person'
10
+ s.summary = %q{A simple interface for searching CMU LDAP directory}
11
+ s.description = %q{This tool is used to search Carnegie Mellon's LDAP directory. Provide an Andrew ID and this will automatically parse the results and return them in a readable format.}
12
+
13
+ s.rubyforge_project = 'cmu_person'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # development dependencies
21
+ s.add_development_dependency 'redcarpet'
22
+ s.add_development_dependency 'rspec'
23
+ s.add_development_dependency 'yard'
24
+
25
+ # runtime dependencies
26
+ s.add_runtime_dependency 'net-ldap'
27
+ end
@@ -0,0 +1,6 @@
1
+ # ::nodoc::
2
+ # @private
3
+ module CMU
4
+ # Simple error
5
+ class RecordNotFound < StandardError; end
6
+ end
@@ -0,0 +1,158 @@
1
+ require 'net/ldap'
2
+
3
+ # ::nodoc::
4
+ # @private
5
+ module CMU
6
+ # A CMU::Person class
7
+ class Person
8
+ # Attempts to create and return a new CMU::Person from the CMU LDAP directory.
9
+ #
10
+ # @param [String] the Andrew ID to search for
11
+ # @raise [CMU::RecordNotFound] if the Andrew ID does not exist
12
+ # @return [CMU::Person] a `CMU::Person` object
13
+ def self.find(andrew_id)
14
+ CMU::Person.new(andrew_id)
15
+ end
16
+
17
+ # Attempts to create and return a new CMU::Person from the CMU LDAP directory.
18
+ # Unlink (see #find), this method will not raise an exception if the Andrew
19
+ # ID is not found.
20
+ #
21
+ # @param [String] the Andrew ID to search for
22
+ # @return [CMU::Person] a `CMU::Person` object
23
+ def self.find_by_andrew_id(andrew_id)
24
+ begin
25
+ find(andrew_id)
26
+ rescue
27
+ nil
28
+ end
29
+ end
30
+
31
+ # ::nodoc::
32
+ # @private
33
+ def initialize(andrew_id)
34
+ ldap = Net::LDAP.new(:host => 'ldap.andrew.cmu.edu')
35
+ @data = ldap.search(:base => 'ou=Person,dc=cmu,dc=edu',
36
+ :filter => 'cmuAndrewId=' + andrew_id).first
37
+ raise CMU::RecordNotFound if @data.nil?
38
+ end
39
+
40
+ # Returns the Andrew ID of the CMU Person
41
+ #
42
+ # @return [String] the Andrew ID of the CMU Person
43
+ def andrew_id
44
+ @andrew_id ||= @data[:cmuAndrewId].last
45
+ end
46
+
47
+ # Returns the full name of the CMU Person
48
+ #
49
+ # @return [String] the full name of the current CMU Person
50
+ def name
51
+ @name ||= @data[:cn].last
52
+ end
53
+
54
+ # Returns the last name of the CMU Person
55
+ #
56
+ # @return [String] the last name of the current CMU Person
57
+ def last_name
58
+ @last_name ||= @data[:sn].last
59
+ end
60
+
61
+ # Returns the first name of the CMU Person
62
+ #
63
+ # @return [String] the first name of the current CMU Person
64
+ def first_name
65
+ @first_name ||= @data[:givenname].last
66
+ end
67
+
68
+ # Returns the email of the CMU Person. If the CMU Person has
69
+ # listed a preferred email, it will be displayed. Otherwise,
70
+ # the official Andrew Email will be used.
71
+ #
72
+ # @return [String] the email of the current CMU Person
73
+ def email
74
+ if @data.attribute_names.include?(:cmupreferredmail)
75
+ @email ||= @data[:cmupreferredmail].last
76
+ else
77
+ @email ||= @data[:mail].last
78
+ end
79
+ end
80
+
81
+ # Returns the phone number for the CMU Person
82
+ #
83
+ # @return [String, nil] the phone number of the current CMU Person
84
+ # or nil if the person did not provide a phone number
85
+ def phone
86
+ if @data.attribute_names.include?(:cmupreferredtelephone)
87
+ @phone ||= @data[:cmupreferredtelephone].last.gsub(/[^0-9]/,'')
88
+ else
89
+ @phone ||= nil
90
+ end
91
+ end
92
+
93
+ # Returns the type for the CMU Person
94
+ #
95
+ # @return [String] the type or role of the CMU Person
96
+ def type
97
+ @type ||= @data[:edupersonaffiliation].last
98
+ end
99
+
100
+ # Returns the official title for the CMU Person
101
+ #
102
+ # @return [String, nil] the official title for the CMU Person or
103
+ # nil if the CMU Person has not provided a title
104
+ def title
105
+ if @data.attribute_names.include?(:title)
106
+ @title ||= @data[:title].last
107
+ else
108
+ if @data.attribute_names.include?(:cmutitle)
109
+ @title ||= @data[:cmutitle].last
110
+ else
111
+ @title ||= nil
112
+ end
113
+ end
114
+ end
115
+
116
+ # Returns the String representation of the grade of the CMU Person
117
+ #
118
+ # @return [String, nil] the String representation of the grade of
119
+ # the CMU Person ('Freshman', 'Sophomore', 'Junior', 'Senior',
120
+ # 'Masters', 'Doctorate') or nil for Faculty and Staff
121
+ def grade
122
+ if @data.attribute_names.include?(:cmustudentclass)
123
+ @grade ||= @data[:cmustudentclass].last
124
+ else
125
+ @grade ||= nil
126
+ end
127
+ end
128
+
129
+ # Returns department for the CMU Person
130
+ #
131
+ # @return [String, nil] the department for the current CMU Person
132
+ # or nil if the CMU Person is a student and/or has no department
133
+ def department
134
+ @department ||= @data[:cmudepartment].last
135
+ end
136
+
137
+ # Returns the "college" for the CMU Person
138
+ #
139
+ # @return [String, nil] the actual college the CMU Person belongs to
140
+ def school
141
+ filters = ['Student Employment', 'Undergraduate Admission and Student Aid', 'VP For Campus Affairs']
142
+ @school ||= @data[:edupersonschoolcollegename].reject{|c| filters.include?(c)}.last
143
+ end
144
+
145
+ # ::nodoc::
146
+ # @private
147
+ def inspect
148
+ fields = %w(andrew_id first_name last_name email phone type title)
149
+ @inspect ||= "<CMU::Person #{fields.collect{|f| f + ': ' + self.send(f.to_sym).inspect}.join(', ')}>"
150
+ end
151
+
152
+ # ::nodoc::
153
+ # @private
154
+ def to_s
155
+ @to_s ||= "<CMU::Person \"#{self.send(:name)} (#{self.send(:andrew_id)})\">"
156
+ end
157
+ end
158
+ end
data/lib/cmu_person.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'cmu_person/person'
2
+ require 'cmu_person/exceptions'
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe CMU::Person do
4
+ before(:all) do
5
+ @seth = CMU::Person.find('svargo')
6
+ @bryan = CMU::Person.find('bleach')
7
+ @dano = CMU::Person.find('dorbegoz')
8
+ @profh = CMU::Person.find('lheimann')
9
+ @kaycee = CMU::Person.find('kpalko')
10
+ end
11
+
12
+ it 'should fetch a valid user' do
13
+ @seth.first_name.should eql('Seth')
14
+ @seth.last_name.should eql('Vargo')
15
+ @seth.andrew_id.should eql('svargo')
16
+ end
17
+
18
+ it 'should fetch another valid user' do
19
+ @bryan.first_name.should eql('Bryan')
20
+ @bryan.last_name.should eql('Leach')
21
+ @bryan.andrew_id.should eql('bleach')
22
+ end
23
+
24
+ it 'new/find should raise an exception for an invalid user' do
25
+ lambda { CMU::Person.new('not_a_real_user') }.should raise_exception(CMU::RecordNotFound)
26
+ lambda { CMU::Person.find('not_a_real_user') }.should raise_exception(CMU::RecordNotFound)
27
+ end
28
+
29
+ it 'find_by_andrew_id not raise an exception for an an invalid user' do
30
+ lambda { CMU::Person.find_by_andrew_id('not_a_real_user') }.should_not raise_exception
31
+ end
32
+
33
+ it 'should return the user\'s email' do
34
+ @seth.email.should eql('svargo@andrew.cmu.edu')
35
+ end
36
+
37
+ it 'should return the user\'s phone' do
38
+ @seth.phone.should eql('8144211811')
39
+ end
40
+
41
+ it 'should return nil if phone is blank' do
42
+ @bryan.phone.should be_nil
43
+ end
44
+
45
+ it 'should return the correct student type' do
46
+ @seth.type.should eql('Student')
47
+ end
48
+
49
+ it 'should return the correct faculty type' do
50
+ @profh.type.should eql('Faculty')
51
+ end
52
+
53
+ it 'should return the correct staff type' do
54
+ @kaycee.type.should eql('Staff')
55
+ end
56
+
57
+ it 'should return the correct title' do
58
+ @seth.title.should eql('Head Grader')
59
+ @profh.title.should eql('Teaching Professor')
60
+ @kaycee.title.should eql('Housefellow/cdtr. of Student Activities')
61
+ end
62
+
63
+ it 'should return nil if title is blank' do
64
+ @dano.title.should be_nil
65
+ end
66
+
67
+ it 'should return the user\'s grade' do
68
+ @seth.grade.should eql('Senior')
69
+ @bryan.grade.should eql('Sophomore')
70
+ end
71
+
72
+ it 'should return nil if grade is blank' do
73
+ @profh.grade.should be_nil
74
+ @kaycee.grade.should be_nil
75
+ end
76
+
77
+ it 'should return the correct department' do
78
+ @profh.department.should eql('Is Program In Dietrich')
79
+ @kaycee.department.should eql('Student Campus Activities')
80
+ @seth.department.should eql('H&SS Interdisciplinary')
81
+ @bryan.department.should eql('Mechanical Engineering')
82
+ @dano.department.should eql('Civil & Environmental Engineering')
83
+ end
84
+
85
+ it 'should return the correct school' do
86
+ @profh.school.should eql('Dietrich College of Humanities and Social Sciences')
87
+ @seth.school.should eql('Dietrich College of Humanities and Social Sciences')
88
+ @bryan.school.should eql('Carnegie Institute of Technology')
89
+ end
90
+
91
+ it 'should return nil if school is blank' do
92
+ @kaycee.school.should be_nil
93
+ end
94
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default)
4
+
5
+ require 'cmu_person'
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmu_person
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Seth Vargo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redcarpet
16
+ requirement: &70290382060060 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70290382060060
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70290382059580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70290382059580
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ requirement: &70290382059120 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70290382059120
47
+ - !ruby/object:Gem::Dependency
48
+ name: net-ldap
49
+ requirement: &70290382058640 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70290382058640
58
+ description: This tool is used to search Carnegie Mellon's LDAP directory. Provide
59
+ an Andrew ID and this will automatically parse the results and return them in a
60
+ readable format.
61
+ email: sethvargo@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .rspec
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE
71
+ - README.markdown
72
+ - Rakefile
73
+ - cmu_person.gemspec
74
+ - lib/cmu_person.rb
75
+ - lib/cmu_person/exceptions.rb
76
+ - lib/cmu_person/person.rb
77
+ - spec/cmu_person_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/sethvargo/cmu_person
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project: cmu_person
99
+ rubygems_version: 1.8.15
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: A simple interface for searching CMU LDAP directory
103
+ test_files:
104
+ - spec/cmu_person_spec.rb
105
+ - spec/spec_helper.rb
106
+ has_rdoc: