Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- require 'open-uri'
- require 'nokogiri'
- require 'cgi'
- module QDB
- class QuoteDoesNotExistError < StandardError; end
- class Base
- attr_accessor :id
- attr_reader :lines
- attr_reader :url
- @base_url = nil
- def initialize params={}
- id = params[:id]||nil;
- lines = params[:lines]||4;
- raise "@base_url must be set in #{self.class.name}#initialize." if @base_url.nil?
- @base_url.freeze # This prevents the developer from screwing around with the variable.
- @id = (:"#{id}" == :latest || id.nil? ? self.retrieve_latest_quote_id : id)
- @lines = lines
- @url = "#{@base_url}?#{@id}"
- end
- def retrieve_latest_quote_id
- raise "retrieve_latest_quote_id must be overridden."
- end
- def retrieve_quote params={}
- raise "retrieve_quote must be overridden."
- end
- def retrieve_meta params={}
- raise "retrieve_meta must be overridden."
- end
- def to_hsh lines = 4
- {
- :quote => self.retrieve_quote(:id => @id, :lines => lines),
- :meta => self.retrieve_meta(:id => @id),
- :id => @id,
- :lines => @lines,
- :url => @url,
- }
- end
- end
- class Bash < Base
- def initialize *args
- @base_url = "http://bash.org/"
- super
- end
- def retrieve_latest_quote_id
- url = "#{@base_url}?latest"
- o = Nokogiri::HTML(open(url));
- id = CGI.unescape_html o.at(".quote a b").children.to_s.strip.gsub("\r","").gsub("#","")
- id.to_i
- end
- def retrieve_quote params={}
- id = params[:id]||:latest;
- lines = params[:lines]||4;
- @id = (:"#{id}" == :latest || id.nil? ? self.retrieve_latest_quote_id : id)
- @url = "#{@base_url}?#{id}"
- o = Nokogiri::HTML(open(@url))
- raise QDB::QuoteDoesNotExistError, "Quote ##{@id} does not exist." if o.at(".qt").nil?
- quotes = CGI.unescape_html o.at(".qt").children.to_s.gsub(/[\r\n]/,"")
- quotes = quotes.split(/<br *\/?>/i)
- @lines = quotes.size
- quotes[0..lines-1]
- end
- def retrieve_meta params={}
- id = params[:id]||:latest;
- @id = (:"#{id}" == :latest || id.nil? ? self.retrieve_latest_quote_id : id)
- @url = "#{@base_url}?#{id}"
- o = Nokogiri::HTML(open(@url))
- raise QDB::QuoteDoesNotExistError, "Quote ##{@id} does not exist." if o.at(".quote").nil?
- rating = o.at(".quote").children.to_s.match(/\((-?\d+)\)/)[1]
- "Rating: #{rating}"
- end
- end
- end
- ## Begin testing.
- def test_method bash
- output = []
- output << "Bash quote ##{bash[:id]} (#{bash[:meta]}):"
- output << bash[:quote]
- footer = ["View"]
- footer << (bash[:lines] > bash[:quote].size ? "more from" : nil)
- footer << "this quote at #{bash[:url]}."
- output << footer.reject(&:nil?).join(" ")
- puts "\t" + output.reject(&:nil?).join("\n\t");
- end
- puts "*** creating initial QDB:Bash object..."
- bash = QDB::Bash.new
- puts "\n*** Test #1: no ID when instantiated."
- test_method(bash.to_hsh)
- puts "\n*** Test #2: ID changed to <\"latest\">"
- bash.id = "latest"
- test_method(bash.to_hsh)
- puts "\n*** Test #3: ID changed to <:latest>"
- bash.id = :latest
- test_method(bash.to_hsh)
- puts "\n*** Test #4: ID changed to <100>"
- bash.id = 100
- test_method(bash.to_hsh)
- puts "\n*** Test #5: ID changed to <\"3400\">"
- bash.id = "3400"
- test_method(bash.to_hsh)
- puts "\n*** Test #6: ID changed to <1000000000>; error catching."
- begin
- bash.id = 1000000000
- test_method(bash.to_hsh)
- rescue
- puts "error: #{$!}"
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement