-
-
Save gunn/801738 to your computer and use it in GitHub Desktop.
Refactor of some code from a blog post.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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