Skip to content

Instantly share code, notes, and snippets.

@gunn
Forked from anonymous/directory_web_service.rb
Created January 29, 2011 10:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gunn/801738 to your computer and use it in GitHub Desktop.
Save gunn/801738 to your computer and use it in GitHub Desktop.
Refactor of some code from a blog post.
require 'mechanize'
require 'nokogiri'
require 'sinatra'
get '/:psuid' do
find_name params[:psuid]
end
def find_name(psuid)
page = Mechanize.new.get('http://psu.edu/ph')
search_result = page.form_with(:name => 'search') { |search|
search.uid = psuid
}.submit
word = Nokogiri::HTML(search_result.content).css('html body form table tr td b')[0].text
word[21,word.length].split(" ").map {
|n| n.downcase.capitalize
}.join(" ")
rescue
"There was an error trying to fetch ID: '#{psuid}'"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment