Sha256: 8e9c2dea466dad3bb389b2dfc0585c3713bc22af469edb419bfa00174c915494

Contents?: true

Size: 1.92 KB

Versions: 14

Compression:

Stored size: 1.92 KB

Contents

require 'twitter'

module Junkie
  module Notification

    class Twitter
      include Log, Config

      DEFAULT_CONFIG = {
        twitter_enabled:    false,
        append_to_message:  "#junkie",
        consumer_key:       "",
        consumer_secret:    "",
        oauth_token:        "",
        oauth_token_secret: "",
      }

      def initialize(channels)
        @channels = channels
        @config = Config.get_config(self)

        if @config[:twitter_enabled]

          # check the configuration
          [:consumer_key, :consumer_secret, :oauth_token,
           :oauth_token_secret].each do |key|

            unless @config[key] && @config[key].match(/\w+/)
              raise InvalidConfigError, "#{key} option is missing"
            end
          end

          # configure
          ::Twitter.configure do |config|
            config.consumer_key = @config[:consumer_key]
            config.consumer_secret = @config[:consumer_secret]
            config.oauth_token = @config[:oauth_token]
            config.oauth_token_secret = @config[:oauth_token_secret]
          end

          # bind to channel
          @channels[:notifications].subscribe do |episode|
            next unless episode.status == :extracted

            log.info("Received an episode from channel. I will tweet about this")
            send_notification(episode)
          end

        else
          log.info("Twitter support is disabled")
        end
      end

      private

      # Sends a formatted message out through Twitter
      #
      # @param [Junkie::Episode] episode that has been downloaded
      def send_notification(episode)
        begin
          mess = "A new episode of %s has been downloaded %s" % [
            episode.to_s, @config[:append_to_message]
          ]
          log.debug("I will tweet: " + mess)

          ::Twitter.update(mess)
        rescue Exception => e
          log.error(e)
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
junkie-0.1.3 lib/junkie/notification/twitter.rb
junkie-0.1.2 lib/junkie/notification/twitter.rb
junkie-0.1.1 lib/junkie/notification/twitter.rb
junkie-0.1.0 lib/junkie/notification/twitter.rb
junkie-0.0.15 lib/junkie/notification/twitter.rb
junkie-0.0.14 lib/junkie/notification/twitter.rb
junkie-0.0.13 lib/junkie/notification/twitter.rb
junkie-0.0.12 lib/junkie/notification/twitter.rb
junkie-0.0.11 lib/junkie/notification/twitter.rb
junkie-0.0.10 lib/junkie/notification/twitter.rb
junkie-0.0.9 lib/junkie/notification/twitter.rb
junkie-0.0.8 lib/junkie/notification/twitter.rb
junkie-0.0.7 lib/junkie/notification/twitter.rb
junkie-0.0.6 lib/junkie/notification/twitter.rb