module CWRUDirectory
Constants
- URL
- VERSION
Public Class Methods
advanced(params = {})
click to toggle source
Perform an advanced search with various params. Ones supported by the CWRU directory are:
* surname: last name * givenname: first name * department: not listed for most entries * location: ??? * category: one of 'STUDENT', 'FACULTY', 'STAFF', or 'EMERITI' * search_text: same as the argument to the simple search * search_method: same as the argument to the simple search
Returns a hash, exactly like the simple method (see above)
# File lib/cwru_directory.rb, line 38 def advanced(params = {}) search(params) end
authenticate!()
click to toggle source
# File lib/cwru_directory/authentication.rb, line 3 def authenticate! return if @authenticated # Surely this will return a result, then we can click 'MORE INFO' # which will take us to the Single Sign On page = @agent.get(URL + '?' + to_query_string(default_search_params.merge({surname: 'Mason', givenname: 'Andrew'}))) # Fill out the SSO form login_page = page.link_with(text: 'MORE INFO').click login_page.form_with(name: nil) do |form| form.field_with(name: 'username').value = @config.case_id form.field_with(name: 'password').value = @config.password end.submit @authenticated = true end
configure() { |config| ... }
click to toggle source
# File lib/cwru_directory/config.rb, line 23 def configure(&block) @config ||= Configuration.new yield @config if block_given? end
default_search_params()
click to toggle source
# File lib/cwru_directory.rb, line 104 def default_search_params { search_text: '', surname: '', givenname: '', department: '', location: '', category: :all, search_method: :regular } end
parse_results(rows)
click to toggle source
# File lib/cwru_directory.rb, line 48 def parse_results(rows) header = nil results = {} index = 0 while index < rows.length # We need more fine-grained control over the looping # because each result is actually 2 rows in the table, # but header rows are only one row. This is the worst row = rows[index] if row.children[0].get_attribute('class') == 'dirhdr' # This is a new header section. header = row.children[0].children[0].text results[header] = [] elsif row.children[0].get_attribute('class') == 'breaker' # This row is just for spacing. Ignore it. index += 1 next else # This is a result row. Process it and the next row results[header] << process_result(row, rows[index + 1]) index += 1 end index += 1 end if @config.get_all_info authenticate! # we need to be authenticated for this results.values.flatten.each do |result| process_more_info(result, @agent.get(result[:more_info_link])) if result[:more_info_link] end end results end
process_more_info(result, more_info_page)
click to toggle source
# File lib/cwru_directory.rb, line 92 def process_more_info(result, more_info_page) html = more_info_page.parser # The first row is the person's name and section, so we can skip that. html.xpath('//table[@class="longlisting"]/tr').drop(1).each do |more_info_row| attribute = more_info_row.children[0].text # There is a td of padding between the attribute and the value value = more_info_row.children[2].text result[attribute] = value end end
process_result(first_row, second_row)
click to toggle source
# File lib/cwru_directory.rb, line 82 def process_result(first_row, second_row) { name: first_row.children[0].text, phone_number: first_row.children[1].text, more_info_link: first_row.children[2].children[0].get_attribute('href'), email: second_row.children[0].text, department: second_row.children[1].text } end
search(params)
click to toggle source
# File lib/cwru_directory.rb, line 42 def search(params) page = @agent.get(URL + '?' + to_query_string(default_search_params.merge(params))).parser rows = page.xpath('//table[@class="dirresults"]/tr') parse_results(rows) end
simple(search_text, search_method = :regular)
click to toggle source
Perform a simple search. Default search method is regular. Other option is phonetic, which actually seems to sort of work.
Returns a hash, which maps each category (see the comments on the advanced method) to the list of results for that category. Each result is itself a hash, with the keys:
* :name * :phone_number * :email * :department
# File lib/cwru_directory.rb, line 23 def simple(search_text, search_method = :regular) search({search_text: search_text, search_method: search_method}) end
to_query_string(params)
click to toggle source
# File lib/cwru_directory.rb, line 116 def to_query_string(params) params.map { |k,v| "#{k}=#{v}" }.join('&') end