Skip to content

Instantly share code, notes, and snippets.

@ashbb
Created August 26, 2011 13:35
Show Gist options
  • Save ashbb/1173411 to your computer and use it in GitHub Desktop.
Save ashbb/1173411 to your computer and use it in GitHub Desktop.
Participant's countries of Ruby with Shoes course 8th batch at RubyLearning.org
require 'green_shoes'
require 'mechanize'
begin
require 'hpricot'
rescue LoadError
require File.join(Shoes::DIR, 'ext/hpricot')
end
include Hpricot
course = 'Ruby with Shoes 8th Batch'
agent = Mechanize.new
login_page = agent.get "http://rubylearning.org/class/login/index.php"
login_form = login_page.forms.first
login_form['username'] = "xxxxx" # input your username for RubyLearning.org
login_form['password'] = "xxxxx" # input your password for RubyLearning.org
agent.submit login_form
main_page = agent.get 'http://rubylearning.org/class/course/index.php'
course_link = main_page.link_with text: course
course_page = agent.get course_link.href
participants_link = course_page.link_with text: 'Participants'
participants_page = agent.get participants_link.href
show_all_text = participants_page.search("//div[@id='showall']").inner_text
show_all_link = participants_page.link_with text: show_all_text
show_all_page = agent.get show_all_link.href
list, countries = [], []
doc = Hpricot show_all_page.body
doc.search("//td[@class='cell c3']").each do |e|
n = e.inner_text.index(',')
n = 0 unless n
list << e.inner_text[0..n-1]
end
list.sort!
pn = list.length
until list.empty?
country, rest = list.partition{|e| list.first == e}
countries << [country.first, country.length]
list = rest
end
countries = countries.sort_by{|c, n| n}.reverse
cn = countries.length
Shoes.app title: course, width: 380, height: 500 do
stack width: 1.0, height: 22 * cn + 50 do
background orangered..green, angle: 30
para strong(' Participants: ', pn, ', Countries: ', cn), stroke: white
countries.each_with_index do |e, i|
y = 22 * i + 30
nostroke
rect width: 330, height: 20, left: 10, top: y, fill: rgb(75, 0, 130, 0.2), curve: 5
para strong(e.first), left: 20, top: y, stroke: white
para strong(e.last), left: 300, top: y, stroke: white
end
end
end
require 'mechanize'
course = 'Ruby with Shoes 8th Batch'
agent = Mechanize.new
login_page = agent.get "http://rubylearning.org/class/login/index.php"
login_form = login_page.forms.first
login_form['username'] = "xxxxx" # input your username for RubyLearning.org
login_form['password'] = "xxxxx" # input your password for RubyLearning.org
agent.submit login_form
main_page = agent.get 'http://rubylearning.org/class/course/index.php'
course_link = main_page.link_with text: course
course_page = agent.get course_link.href
participants_link = course_page.link_with text: 'Participants'
participants_page = agent.get participants_link.href
show_all_text = participants_page.search("//div[@id='showall']").inner_text
show_all_link = participants_page.link_with text: show_all_text
show_all_page = agent.get show_all_link.href
open(course + '.html', 'w'){|f| f.puts show_all_page.body}
require 'hpricot'
course = 'Ruby with Shoes 8th Batch'
list, countries = [], []
filename = course + '.html'
doc = open(filename){|f| Hpricot(f)}
doc.search("//td[@class='cell c3']").each do |e|
n = e.inner_text.index(',')
n = 0 unless n
list << e.inner_text[0..n-1]
end
list.sort!
pn = list.length
until list.empty?
country, rest = list.partition{|e| list.first == e}
countries << [country.first, country.length]
list = rest
end
countries = countries.sort_by{|c, n| n}.reverse
cn = countries.length
titlename = 'Ruby with Shoes 8th Batch'
Shoes.app title: titlename, width: 380, height: 500 do
stack width: 1.0, height: 22 * cn + 50 do
background orangered.to_s..green.to_s, angle: 30
para strong('Participants: ', pn, ', Countries: ', cn), stroke: white
countries.each_with_index do |e, i|
y = 22 * i + 30
nostroke
rect width: 330, height: 20, left: 10, top: y, fill: rgb(75, 0, 130, 0.2), curve: 5
para strong(e.first), left: 20, top: y, stroke: white
para strong(e.last), left: 300, top: y, stroke: white
end
end
end
@ashbb
Copy link
Author

ashbb commented Aug 27, 2011

Uploaded Green Shoes version.
Usage: just only run ruby green_shoes_participants.rb

The differences between Red Shoes and Green Shoes are,

  • the way to require Hpricot
  • there is no need to add to_s for color gradation in Green Shoes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment