# Finding Quiz Responses (#200)

**URL:** <https://www.ruby-forum.com/t/finding-quiz-responses-200/165249>\
**Category:** Ruby\
**Created:** [April 10, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/finding-quiz-responses-200/165249 "2009-04-10T18:53:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [April 10, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/finding-quiz-responses-200/165249/1 "2009-04-10T18:53:17Z")

</div>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q.:

1. Please do not post any solutions or spoiler discussion for this  
quiz until 48 hours have elapsed from the time this message was  
sent.

2. Support Ruby Q. by submitting ideas and responses  
as often as you can!  
Visit: [http://rubyquiz.strd6.com/suggestions](http://rubyquiz.strd6.com/suggestions)

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem  
helps everyone on Ruby T. follow the discussion. Please reply to  
the original quiz message, if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Finding Quiz Responses (#200)

Hola Rubyists,

The new Rubyquiz website is perfect… almost. It has the quizzes, it  
has the summaries, but one thing that it is missing are the links to  
the mailing list responses.

This week’s quiz is to write a program to gather the responses for  
each quiz on the ruby-talk mailing list. The data needed for the  
responses  
are:

- The quiz the response is for
- The name of the person who submitted the response
- A URL to link to the response

Have Fun!

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [April 24, 2009, 6:41am UTC](https://www.ruby-forum.com/t/finding-quiz-responses-200/165249/2 "2009-04-24T06:41:38Z")

</div>

Finding all the responses to this week’s quiz was easy: no responses!

In other news, Luke C. made significant contributions to the Ruby  
Quiz site by adding an RSS feed  
([http://rubyquiz.strd6.com/quizzes.rss](http://rubyquiz.strd6.com/quizzes.rss)) and adding a prominent link to  
the current quiz. Thank you Luke, for making Ruby Q. even better!

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [April 25, 2009, 2:40am UTC](https://www.ruby-forum.com/t/finding-quiz-responses-200/165249/3 "2009-04-25T02:40:28Z")

</div>

It’s late, but it’s here… my very first ruby quiz submission:

#!/usr/bin/env ruby

require ‘rubygems’  
require ‘hpricot’  
require ‘open-uri’  
require ‘cgi’

def build\_quiz\_uri(quiz\_num)  
query = CGI::escape(“subject:(+#{quiz\_num}) +quiz”)  
base\_url = “[Search results for '' - Ruby-Forum](http://www.ruby-forum.com/search?query=)”  
uri\_string = (base\_url + query)  
URI.parse(uri\_string)  
end

def get\_quiz\_response\_url(uri\_string)  
doc = Hpricot(open(uri\_string))  
quiz\_table = doc.at(“table[@class='topics list]”)  
quizzes = quiz\_table.search(“tr[@class$='new-posts ']”)

highest\_replies = 0  
link = “”

#find the quiz that has the right subject and has the most replies  
quizzes.each do |quiz|  
subject = quiz.at(“td[@class=‘subject’]/a”).innerHTML  
quiz\_subject = “#” + Quiz\_num.to\_s

```
if(subject.include?(quiz_subject))
  replies = quiz.at("td[@class='replies']").innerHTML.to_i
  if(replies > highest_replies)
    highest_replies = replies
    link = quiz.at("td[@class='subject']/a")[:href]
  end
end

```

end

#trim the excess anchor  
link.gsub(/#.\*$/, “”)  
end

def get\_responses(url)  
doc = Hpricot(open(url))

response\_elements = doc.search(“.post”)

responses = []

response\_elements.inject([]) do |responses, response|  
link = url + response.search(“div[@class='subject]”).at(“a”)[:href]  
author =  
response.search(“span[@class]=‘name’”).text.split(“(”).first.strip  
responses \<\< {:link =\> link, :author =\> author}  
end

#remove the first quiz (which is from the quizmaster)  
responses.delete\_at(0)

#if the summary has been posted, we want to remove that too  
#it’s difficult to know if we should actually remove the response…  
if(responses.length \> 0)  
if(QuizMaster\_Name.include?(responses.last[:author]))  
responses.pop  
end  
end

responses  
end

Quiz\_num = 100  
QuizMaster\_Name = [“Di Mo”, “James G.”]

query\_uri = build\_quiz\_uri(Quiz\_num)

response\_path = get\_quiz\_response\_url(query\_uri)

response\_url = query\_uri.scheme + “://” + query\_uri.host + response\_path

puts get\_responses(response\_url).to\_yaml
