Class: Cinch::Plugins::MultiQDB

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/multiqdb.rb

Constant Summary collapse

QDB_LINE_MARKS =
{normal: '-', contd: '+', end: '$'}

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MultiQDB


22
23
24
25
26
27
# File 'lib/cinch/plugins/multiqdb.rb', line 22

def initialize(*args)
  super
  # Creating a hash of all QDB objects available.
  names = [QDB::Bash, QDB::QdbUS, QDB::Shakesoda, QDB::MIT].map {|qdb| [qdb.new.shortname, qdb] }
  @@qdbs = Hash[*names.flatten]
end

Instance Method Details

#execute_list(m) ⇒ Object


67
68
69
70
71
72
73
74
# File 'lib/cinch/plugins/multiqdb.rb', line 67

def execute_list(m)
  list = []
  @@qdbs.each_value {|q|
    qdb = q.new
    list << "#{q.name} (#{q.shortname}): #{q.url}"
  }
  m.notice "To use a QDB, type " + Format(:bold, "!qdb [selector] [#|latest|random]") + ".\n" + list.join("\n")
end

#execute_qdb(m, selector = nil, id = nil) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cinch/plugins/multiqdb.rb', line 30

def execute_qdb(m, selector=nil, id=nil)
  m.reply "You have not supplied a selector. Selectors: #{@@qdbs.keys * ", "}" and return if !selector
  m.reply "#{selector} does not exist. Valid selectors: #{@@qdbs.keys * ", "}" and return if !@@qdbs.include?(selector)

  qdb = @@qdbs[selector].new # Instantize the QDB object

  result = fetch_qdb_obj(id, qdb)

  #rcolors = [:green, :red, :purple, :yellow, :lime, :teal, :aqua, :royal, :pink]
  #colorize_text = ->(text) { (Zlib::crc32(text) % rcolors.size)-1 }
  qdb_banner = "#{qdb.name} ##{result.id}"
  m.reply qdb_banner
  result.quote.each_with_index {|q,i|
    marker = if i.succ == result.quote.length then QDB_LINE_MARKS[:end]
    elsif i.succ == config[:limit] then QDB_LINE_MARKS[:contd]
    else QDB_LINE_MARKS[:normal]
    end

    if i.succ == config[:limit]
      # qdb public/private boundary
      m.reply "%s %s\nThe full quote can be viewed online at %s" % [marker, q, qdb.url + qdb.id_path_template % {id: result.id}]
      m.user.notice "%s continued:" % qdb_banner
    elsif i.succ > config[:limit]
      # qdb private
      m.user.notice "%s %s" % [marker, q]
    else
      # qdb public
      m.reply "%s %s" % [marker, q]
    end
  }
rescue QDB::Error::QuoteNotFound => e
  m.reply e.message, true
rescue OpenURI::HTTPError => e
  m.reply "An error has occured. #{$!}", true
end