Class: Junkie::Notification::Twitter

Inherits:
Object
  • Object
show all
Includes:
Config, Log
Defined in:
lib/junkie/notification/twitter.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Log

#log

Methods included from Config

collect_default_configs, get_config, included

Constructor Details

#initialize(channels) ⇒ Twitter

Returns a new instance of Twitter.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/junkie/notification/twitter.rb', line 18

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