Class: Cinch::Plugins::Quotes

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Quotes

Returns a new instance of Quotes.



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

def initialize(*args)
  super

  @quotes_file = config[:quotes_file]
end

Instance Method Details

#addquote(m, quote) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cinch/plugins/quotes.rb', line 19

def addquote(m, quote)
  # make the quote
  new_quote = { "quote" => quote, "added_by" => m.user.nick, "created_at" => Time.now, "deleted" => false }

  # add it to the list
  existing_quotes = get_quotes || []
  existing_quotes << new_quote

  # find the id of the new quote and set it based on where it was placed in the quote list
  new_quote_index = existing_quotes.index(new_quote)
  existing_quotes[new_quote_index]["id"] = new_quote_index + 1

  # write it to the file
  output = File.new(@quotes_file, 'w')
  output.puts YAML.dump(existing_quotes)
  output.close

  # send reply that quote was added
  m.reply "#{m.user.nick}: Quote successfully added as ##{new_quote_index + 1}."
end

#quote(m, search = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cinch/plugins/quotes.rb', line 40

def quote(m, search = nil)
  quotes = get_quotes.delete_if{ |q| q["deleted"] == true }
  if search.nil? # we are pulling random
    quote = quotes.sample
    m.reply "#{m.user.nick}: ##{quote["id"]} - #{quote["quote"]}"
  elsif search.to_i != 0 # then we are searching by id
    quote = quotes.find{|q| q["id"] == search.to_i }
    if quote.nil?
      m.reply "#{m.user.nick}: No quotes found."
    else 
      m.reply "#{m.user.nick}: ##{quote["id"]} - #{quote["quote"]}"
    end
  else 
    quotes.keep_if{ |q| q["quote"].downcase.include?(search.downcase) }
    if quotes.empty?
      m.reply "#{m.user.nick}: No quotes found."
    else
      quote = quotes.first
      m.reply "#{m.user.nick}: ##{quote["id"]} - #{quote["quote"]}"
      m.reply "The search term also matched on quote IDs: #{quotes.map{|q| q["id"]}.join(", ")}" if quotes.size > 1
    end
  end
end