Class: ParseResult
- Inherits:
-
Object
- Object
- ParseResult
- Defined in:
- lib/parse_result.rb
Constant Summary collapse
- RESULT_QUERY_URL =
'http://www.rusa.org/cgi-bin/results.pl'
- PARMS =
match parm key with parameter we will pass
{'clubs' => 'club', 'ids' => 'mid', 'lnames' => 'Last_Name', 'fnames' => 'First_Name', 'years' => 'searchyear', }
Instance Method Summary collapse
- #parse(member, doc) ⇒ Object
- #results(member, year = Time.now.year, url = RESULT_QUERY_URL) ⇒ Object
Instance Method Details
#parse(member, doc) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/parse_result.rb', line 46 def parse member, doc ## # sanity check-- the li items gives us search terms. compare to member data doc.css('li').each_with_index do |li, i| if li.text =~ /first name=(.*)/i && member.first_name.downcase != $1.downcase then raise "first name #{$1} in results does not match #{member.first_name}" end if li.text =~ /last name=(.*)/i && member.last_name.downcase != $1.downcase then raise "last name #{$1} in results does not match #{member.last_name}" end end # year is only displayed in results, not as parm anywhere year_results = nil ## # the data we care about starts in a <td> that lists the count of matches found doc.css('table table tr').each do |row| result_data = row.css('td') next if result_data.size.zero? member.results<< Result.new({ :certificate => result_data[0].text, :type => result_data[1].text, :distance => result_data[2].text, # Time.parse needs slashes, not dashes :date => Time.parse(result_data[3].text.gsub("-","/")), :organizer => result_data[4].text, } ) end year_results = member.results.last.date.year unless member.results.empty? member.awards[year_results] = [] unless year_results.nil? or member.awards.has_key? year_results ## # find the awards for this year doc.css('table tr td').each do |row| next unless row.text =~ /Award status: (.*)/i $1.split(",").each do |award_type| member.awards[year_results]<< Award.new({ :type => award_type.strip, :year => year_results.to_i, }) end end end |
#results(member, year = Time.now.year, url = RESULT_QUERY_URL) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/parse_result.rb', line 29 def results member, year=Time.now.year, url=RESULT_QUERY_URL if block_given? then f = yield url else parms = {'Last_Name' => member.last_name, 'First_Name' => member.first_name, 'searchyear' => year } f = Net::HTTP.post_form(URI.parse(url), parms).body end doc = Nokogiri::HTML(f) parse(member, doc) end |