Advertisement
Azure

QDB ruby script test 2

Jul 22nd, 2011
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.16 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 Base
  9.         attr_accessor :id
  10.         attr_reader :lines
  11.         attr_reader :url
  12.        
  13.         def initialize id = nil, lines = 4
  14.             @id = id
  15.             @lines = lines
  16.            
  17.             if !@id.nil?
  18.                 @id = retrieve_latest_quote_id if @id.to_sym === :latest
  19.                 self.retrieve_quote
  20.                 self.retrieve_meta
  21.             end
  22.         end
  23.    
  24.         def retrieve_latest_quote_id
  25.             raise "retrieve_latest_quote_id must be overridden."
  26.         end
  27.  
  28.         def retrieve_quote params={}
  29.             raise "retrieve_quote must be overridden."
  30.         end
  31.    
  32.         def retrieve_meta params={}
  33.             raise "retrieve_meta must be overridden."
  34.         end
  35.        
  36.         def format lines = 4
  37.             {
  38.                 :id => @id,
  39.                 :quote => self.retrieve_quote(:id => @id, :lines => lines),
  40.                 :meta => self.retrieve_meta(:id => @id),
  41.                 :lines => @lines,
  42.                 :url => @url,
  43.             }
  44.         end
  45.     end
  46.  
  47.     class Bash < Base
  48.         @@_base_url = "http://bash.org/"
  49.        
  50.         def retrieve_latest_quote_id
  51.             url = "#{@@_base_url}?latest"
  52.             o = Nokogiri::HTML(open(url));
  53.             id = CGI.unescape_html o.at(".quote a b").children.to_s.strip.gsub("\r","").gsub("#","")
  54.             id.to_i
  55.         end
  56.    
  57.         def retrieve_quote params={}
  58.             id = params[:id]||:latest;
  59.             lines = params[:lines]||4;
  60.            
  61.             @url = "#{@@_base_url}?#{id}"
  62.             o = Nokogiri::HTML(open(@url))
  63.             quotes = CGI.unescape_html o.at(".qt").children.to_s.gsub(/[\r\n]/,"")
  64.             quotes = quotes.split(/<br *\/?>/i)
  65.             @lines = quotes.size
  66.            
  67.             quotes[0..lines-1]
  68.         end
  69.    
  70.         def retrieve_meta params={}
  71.             id = params[:id]||:latest;
  72.             @url = "#{@@_base_url}?#{id}"
  73.            
  74.             o = Nokogiri::HTML(open(@url))
  75.             rating = o.at(".quote").children.to_s.match(/\((-?\d+)\)/)[1]
  76.            
  77.             "Rating: #{rating}"
  78.         end
  79.     end
  80. end
  81.  
  82. ## Begin testing.
  83.  
  84. bash = QDB::Bash.new
  85. bash.id = 61129
  86. format_hash = bash.format
  87.  
  88. output = []
  89. output << "Bash quote ##{format_hash[:id]} (#{format_hash[:meta]}):"
  90. output << format_hash[:quote]
  91.  
  92. footer = ["View"]
  93. footer << (format_hash[:lines] > format_hash[:quote].size ? "more from" : nil)
  94. footer << "this quote at #{format_hash[:url]}."
  95. output << footer.reject(&:nil?).join(" ")
  96.  
  97. output.reject(&:nil?).join("\n");
  98.    
  99. puts output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement