Sha256: 9c1e381171dd4125888e23f94e7da6dd0fec0598bfce5e1a58e33af42c791c76

Contents?: true

Size: 940 Bytes

Versions: 5

Compression:

Stored size: 940 Bytes

Contents

require "em-http-request"
require "rss/1.0"
require "rss/2.0"

class Reddit
  include Muzang::Plugins::Helpers
  attr_accessor :last_update

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

  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

  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

  def period
    30
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
muzang-plugins-1.1.4 lib/muzang-plugins/muzang-reddit.rb
muzang-plugins-1.1.3 lib/muzang-plugins/muzang-reddit.rb
muzang-plugins-1.1.2 lib/muzang-plugins/muzang-reddit.rb
muzang-plugins-1.1.1 lib/muzang-plugins/muzang-reddit.rb
muzang-plugins-1.1.0 lib/muzang-plugins/muzang-reddit.rb