Class: Muzang::Plugins::Reddit

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/muzang-plugins/muzang-reddit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#create_database, #match, #on_channel, #on_join

Constructor Details

#initialize(bot) ⇒ Reddit

Returns a new instance of Reddit.



11
12
13
14
# File 'lib/muzang-plugins/muzang-reddit.rb', line 11

def initialize(bot)
  @bot = bot
  create_database("last_update.yml", Time.now, :last_update)
end

Instance Attribute Details

#last_updateObject

Returns the value of attribute last_update.



9
10
11
# File 'lib/muzang-plugins/muzang-reddit.rb', line 9

def last_update
  @last_update
end

Instance Method Details

#call(connection, message) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/muzang-plugins/muzang-reddit.rb', line 16

def call(connection, message)
  on_join(connection, message) do
    EventMachine::add_periodic_timer(period) do
      http = EventMachine::HttpRequest.new('http://www.reddit.com/r/ruby/.rss').get

      http.callback {
        rss = RSS::Parser.parse(http.response, false)
        rss.items.each do |item|
          connection.msg(message.channel, "#{item.title} | #{item.link}") if item.date > @last_update
        end
        save(rss)
      }
    end
  end
end

#periodObject



37
38
39
# File 'lib/muzang-plugins/muzang-reddit.rb', line 37

def period
  30
end

#save(rss) ⇒ Object



32
33
34
35
# File 'lib/muzang-plugins/muzang-reddit.rb', line 32

def save(rss)
  @last_update = rss.items.max_by{|i|i.date}.date
  file = File.open(@config + "/last_update.yml", "w"){|f| f.write YAML::dump(@last_update)}
end