Created
January 2, 2012 10:27
-
-
Save mrnugget/1550196 to your computer and use it in GitHub Desktop.
Access Bash.org quotes via ruby
This file contains 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
#!/usr/bin/env ruby -wKU | |
require "nokogiri" | |
require "open-uri" | |
class BashOrg | |
def self.random_quote | |
get_quote("random") | |
end | |
def self.quote_by_id(id) | |
get_quote(id.to_s) | |
end | |
def self.top5 | |
get_top_voted_quotes(5) | |
end | |
def self.top10 | |
get_top_voted_quotes(10) | |
end | |
private | |
def self.get_quote(quote) | |
results = bash_request(quote) | |
results.first.text | |
end | |
def self.get_top_voted_quotes(count) | |
results = bash_request("top") | |
results[0..count] | |
end | |
def self.bash_request(request) | |
result = Nokogiri::HTML(open("http://www.bash.org/?#{request}")) | |
result.xpath('//p[@class = "qt"]') | |
end | |
end | |
puts BashOrg.random_quote | |
puts "-----" | |
puts BashOrg.quote_by_id(1863) | |
puts "-----" | |
BashOrg.top10.each {|q| puts "-----\n" + q } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment