Sha256: 354a10e39823baefc590c355bc7769dd1342d3608ac59ad2a0e62956706ecdd5

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# -*- encoding : utf-8 -*-
# Class to handle user subscriptions
class RabbitSubscription < Cinch::Plugins::Rabbit
  attr_accessor :nick, :all_links
  FILENAME = ENV['RABBIT_SUBSCRIPTIONS_FILE'] || 'yaml/rabbit_subs.yml'

  def initialize(nick, file)
    @nick = nick
    @all_links = false
    save
  end

  def save
    subs = Subscription.storage
    subs.data[nick] = self
    subs.save
  end

  def delete
    subs = Subscription.storage
    subs.data.delete(nick)
    subs.save
  end

  def self.for_user(nick)
    return nil unless list.key?(nick)
    list[nick]
  end

  def self.list
    storage.data
  end

  def self.notify(rabbit_id, bot, type)
    nick = RabbitLink.find_by_id(rabbit_id).nick
    list.each_value do |s|
      # Don't link the person who linked it.
      unless nick == s.nick
        user = Cinch::User.new(s.nick, bot)
        message = "#{nick} just linked a new rabbit at: " +
                  RabbitLink.url(rabbit_id)
        respond(user, message, type)
      end
    end
  end

  def self.respond(user, message, type)
    case type
    when :notice
      user.notice message
    when :pm
      user.send message
    end
  end

  private

  def self.storage
    Cinch::Storage.new(FILENAME)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cinch-rabbit-0.0.2 lib/cinch/plugins/rabbit/subscription.rb