Sha256: 11aa6201111ef6eefc767067b0e1be22c91c5b843e7ad5a2b7bba14fcfc0f210

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

require 'twitter'

module Junkie
  module Notification

    class Twitter
      include Log, Config

      DEFAULT_CONFIG = {
        twitter_enabled:    false,
        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 #{episode} has been downloaded #junkie"
          ::Twitter.update(mess)
        rescue Exception => e
          log.error(e)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
junkie-0.0.5 lib/junkie/notification/twitter.rb
junkie-0.0.4 lib/junkie/notification/twitter.rb