Class: QDB::Bash

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

#initializeBash

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


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

def initialize
  @name, @shortname, @url = "Bash.org", "bash", "http://bash.org/"
  @id_path_template = "?%<id>s"
  @random_path = "?random"
  @latest_path = "?latest"
end

Instance Method Details

#by_id(id) ⇒ Object

Raises:


15
16
17
18
19
20
# File 'lib/cinch/plugins/qdb/bash.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(".qt").nil?
  quotes = CGI.unescape_html o.at(".qt").children.to_s.gsub(/[\r\n]/,"")
  Quote.new(id, quotes.split(/<br *\/?>/i))
end

#latestObject


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

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

#randomObject


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

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