Class: QDB::Shakesoda

Inherits:
Base
  • Object
show all
Defined in:
lib/cinch/plugins/qdb/shakesoda.rb

Overview

Inheriting Base

Instance Attribute Summary

Attributes inherited from Base

#id_path_template, #latest_path, #name, #random_path, #shortname, #url

Instance Method Summary collapse

Constructor Details

#initializeShakesoda

attr_reader :name, :shortname, :url, :id_path_template, :random_path, :latest_path


8
9
10
11
# File 'lib/cinch/plugins/qdb/shakesoda.rb', line 8

def initialize
  @name, @shortname, @url = "#shakesoda", "ss", "http://www.shakesoda.org/qdb/"
  @id_path_template = "view/%<id>s"
end

Instance Method Details

#by_id(id) ⇒ Object

Raises:


13
14
15
16
17
18
# File 'lib/cinch/plugins/qdb/shakesoda.rb', line 13

def by_id(id)
  o = Nokogiri::HTML(open(@url + (@id_path_template % {id: id})))
  raise QDB::Error::QuoteNotFound.new(id), "Quote ##{id} does not exist." if o.at(".quote").nil?
  quotes = CGI.unescape_html o.at(".quote-content").children.to_s.gsub("\r","")
  Quote.new(id, quotes.split(/<br *\/?>/i))
end

#latestObject


26
27
28
# File 'lib/cinch/plugins/qdb/shakesoda.rb', line 26

def latest
  self.by_id(get_first_id(@url))
end

#randomObject


20
21
22
23
24
# File 'lib/cinch/plugins/qdb/shakesoda.rb', line 20

def random
  o = Nokogiri::HTML(open(@url))
  random = o.css(".quote .quote-header a[href*=\"qdb/view\"]").to_a.sample.inner_text.gsub("#",'')
  self.by_id(random)
end