Advertisement
Azure

QDB ruby script test 4

Jul 22nd, 2011
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.39 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. require 'open-uri'
  4. require 'nokogiri'
  5. require 'cgi'
  6.  
  7. module QDB
  8.     class QuoteDoesNotExistError < StandardError; end
  9.  
  10.     class Base
  11.         attr_accessor :id
  12.         attr_reader :lines
  13.         attr_reader :url
  14.        
  15.         @base_url = nil
  16.        
  17.         def initialize params={}
  18.             id = params[:id]||nil;
  19.             lines = params[:lines]||4;
  20.            
  21.             raise "@base_url must be set in #{self.class.name}#initialize." if @base_url.nil?
  22.             @base_url.freeze # This prevents the developer from screwing around with the variable.
  23.            
  24.             @id = (:"#{id}" == :latest || id.nil? ? self.retrieve_latest_quote_id : id)
  25.             @lines = lines
  26.             @url = "#{@base_url}?#{@id}"
  27.         end
  28.        
  29.         def retrieve_latest_quote_id
  30.             raise "retrieve_latest_quote_id must be overridden."
  31.         end
  32.  
  33.         def retrieve_quote params={}
  34.             raise "retrieve_quote must be overridden."
  35.         end
  36.    
  37.         def retrieve_meta params={}
  38.             raise "retrieve_meta must be overridden."
  39.         end
  40.        
  41.         def to_hsh lines = 4
  42.             {
  43.                 :quote => self.retrieve_quote(:id => @id, :lines => lines),
  44.                 :meta => self.retrieve_meta(:id => @id),
  45.                 :id => @id,
  46.                 :lines => @lines,
  47.                 :url => @url,
  48.             }
  49.         end
  50.    
  51.     end
  52.  
  53.     class Bash < Base
  54.         def initialize *args
  55.             @base_url = "http://bash.org/"
  56.             super
  57.         end
  58.        
  59.         def retrieve_latest_quote_id
  60.             url = "#{@base_url}?latest"
  61.             o = Nokogiri::HTML(open(url));
  62.             id = CGI.unescape_html o.at(".quote a b").children.to_s.strip.gsub("\r","").gsub("#","")
  63.             id.to_i
  64.         end
  65.    
  66.         def retrieve_quote params={}
  67.             id = params[:id]||:latest;
  68.             lines = params[:lines]||4;
  69.             @id = (:"#{id}" == :latest || id.nil? ? self.retrieve_latest_quote_id : id)
  70.            
  71.             @url = "#{@base_url}?#{id}"
  72.             o = Nokogiri::HTML(open(@url))
  73.             raise QDB::QuoteDoesNotExistError, "Quote ##{@id} does not exist." if o.at(".qt").nil?
  74.             quotes = CGI.unescape_html o.at(".qt").children.to_s.gsub(/[\r\n]/,"")
  75.             quotes = quotes.split(/<br *\/?>/i)
  76.             @lines = quotes.size
  77.            
  78.             quotes[0..lines-1]
  79.         end
  80.    
  81.         def retrieve_meta params={}
  82.             id = params[:id]||:latest;
  83.             @id = (:"#{id}" == :latest || id.nil? ? self.retrieve_latest_quote_id : id)
  84.            
  85.             @url = "#{@base_url}?#{id}"
  86.             o = Nokogiri::HTML(open(@url))
  87.             raise QDB::QuoteDoesNotExistError, "Quote ##{@id} does not exist." if o.at(".quote").nil?
  88.             rating = o.at(".quote").children.to_s.match(/\((-?\d+)\)/)[1]
  89.            
  90.             "Rating: #{rating}"
  91.         end
  92.     end
  93. end
  94.  
  95. ## Begin testing.
  96.  
  97. def test_method bash
  98.     output = []
  99.     output << "Bash quote ##{bash[:id]} (#{bash[:meta]}):"
  100.     output << bash[:quote]
  101.  
  102.     footer = ["View"]
  103.     footer << (bash[:lines] > bash[:quote].size ? "more from" : nil)
  104.     footer << "this quote at #{bash[:url]}."
  105.     output << footer.reject(&:nil?).join(" ")
  106.        
  107.     puts "\t" + output.reject(&:nil?).join("\n\t");
  108. end
  109.  
  110. puts "*** creating initial QDB:Bash object..."
  111. bash = QDB::Bash.new
  112. puts "\n*** Test #1: no ID when instantiated."
  113. test_method(bash.to_hsh)
  114.  
  115. puts "\n*** Test #2: ID changed to <\"latest\">"
  116. bash.id = "latest"
  117. test_method(bash.to_hsh)
  118.  
  119. puts "\n*** Test #3: ID changed to <:latest>"
  120. bash.id = :latest
  121. test_method(bash.to_hsh)
  122.  
  123. puts "\n*** Test #4: ID changed to <100>"
  124. bash.id = 100
  125. test_method(bash.to_hsh)
  126.  
  127. puts "\n*** Test #5: ID changed to <\"3400\">"
  128. bash.id = "3400"
  129. test_method(bash.to_hsh)
  130.  
  131. puts "\n*** Test #6: ID changed to <1000000000>; error catching."
  132. begin
  133.     bash.id = 1000000000
  134.     test_method(bash.to_hsh)
  135. rescue
  136.     puts "error: #{$!}"
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement