Module: GifBot

Defined in:
lib/gifbot.rb,
lib/gifbot/cli.rb,
lib/gifbot/version.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

GIFBIN_URL =
'http://www.gifbin.com'
VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.connect(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
# File 'lib/gifbot.rb', line 12

def self.connect(options={})
  bot = Cinch::Bot.new do
    configure do |c|
      c.server   = options[:server]
      c.nick     = options[:nick]
      c.channels = options[:channels]
    end
    
    helpers do
      def search(query)
        query   = CGI.escape(query)
        gifs    = []
        results = Nokogiri::HTML( open("#{GIFBIN_URL}/search/#{query}/") )
        
        results.xpath('//div[@class="thumb-cell"]//a').each do |a|
          gifs << a['href'].sub(/^\//, '')
        end
        
        if gifs.empty?
          "damn, no gif for \"#{query}\""
        else
          id = gifs[rand(gifs.length)]
          page = Nokogiri::HTML( open("#{GIFBIN_URL}/#{id}") )

          image_url_from_page(page)
        end
      end
      
      def random
        page = Nokogiri::HTML(open("#{GIFBIN_URL}/random"))
        
        image_url_from_page(page)
      end
      
      def image_url_from_page(page)
        URI.escape(GIFBIN_URL + page.xpath('//div[@class="box"]//img[@class="gif"]').first['src'])
      end
    end
    
    on :message, /^?randomgif/ do |m|
      m.reply random
    end
    
    on :message, /^?gifme (.+)/ do  |m, query|
      m.reply search(query)
    end
    
  end
  
  bot.start
end