[QUIZ] Finding Quiz Responses (#200)

Discussion in 'Ruby' started by Daniel Moore, Apr 10, 2009.

  1. Daniel Moore

    Daniel Moore Guest

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

    The three rules of Ruby Quiz:

    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 Quiz by submitting ideas and responses
    as often as you can!
    Visit: <http://rubyquiz.strd6.com/suggestions>

    3. Enjoy!

    Suggestion: A [QUIZ] in the subject of emails about the problem
    helps everyone on Ruby Talk 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!
    --
    -Daniel
    http://rubyquiz.strd6.com
     
    Daniel Moore, Apr 10, 2009
    #1
    1. Advertising

  2. Daniel Moore

    Daniel Moore Guest

    [QUIZ][SUMMARY] Finding Quiz Responses (#200)

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

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

    --
    -Daniel
    http://rubyquiz.strd6.com
     
    Daniel Moore, Apr 24, 2009
    #2
    1. Advertising

  3. Daniel Moore

    Luke Cowell Guest

    Re: Finding Quiz Responses (#200)

    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 = "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 Gray"]

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Luke Cowell, Apr 25, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tigerhillside
    Replies:
    3
    Views:
    499
    Joe Smith
    Feb 23, 2004
  2. PJ

    Watch Http Request/Responses

    PJ, Aug 16, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,385
    Ken Cox [Microsoft MVP]
    Aug 16, 2003
  3. Steven Livingstone

    http pipline, ihttpmodule and filtering responses

    Steven Livingstone, Oct 16, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    466
  4. Luiz Vianna
    Replies:
    2
    Views:
    425
    Luiz Vianna
    Nov 13, 2003
  5. Ricky K. Rasmussen

    Why do I sometimes get empty aspx responses?

    Ricky K. Rasmussen, May 17, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    505
    Steven Cheng[MSFT]
    May 21, 2004
Loading...

Share This Page