Class: QDB::MIT

Inherits:
Base
  • Object
show all
Defined in:
lib/cinch/plugins/qdb/mit.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

#initializeMIT

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


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

def initialize
  @name, @shortname, @url = "qdb.mit", "mit", "http://qdb.mit.edu/"
  @id_path_template = "%<id>d"
  @random_path = "rss/random"
  @latest_path = "rss/browse"
end

Instance Method Details

#by_id(id) ⇒ Object

Raises:


15
16
17
18
19
20
# File 'lib/cinch/plugins/qdb/mit.rb', line 15

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-body").nil?
  quotes = CGI.unescape_html o.at(".quote-body").inner_text.gsub("\t","").strip
  Quote.new(id, quotes.split("\n"))
end

#latestObject


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

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

#randomObject


22
23
24
# File 'lib/cinch/plugins/qdb/mit.rb', line 22

def random
  self.by_id(get_first_id(@url + @random_path))
end