class Feedbook::Notifiers::TwitterNotifier

Attributes

client[R]

Public Instance Methods

load_configuration(configuration = {}) click to toggle source

Load configuration for TwitterNotifier @param configuration = {} [Hash] Configuration hash (required: consumer_key, consumer_secret, access_token, access_token_secret)

@return [NilClass] nil @raise [Feedbook::Errors::NotifierConfigurationError] if notifier configuration fails

# File lib/feedbook/notifiers/twitter_notifier.rb, line 31
def load_configuration(configuration = {})
  @client = Twitter::REST::Client.new do |config|
    config.consumer_key        = configuration.fetch('api_key')
    config.consumer_secret     = configuration.fetch('api_secret')
    config.access_token        = configuration.fetch('access_token')
    config.access_token_secret = configuration.fetch('access_token_secret')
  end

  puts 'Configuration loaded for TwitterNotifier'
rescue Twitter::Error => e
  raise Errors::NotifierConfigurationError.new(:twitter, e.message)
end
notify(message) click to toggle source

Sends notification to Twitter @param message [String] message to be send to Twitter

@return [NilClass] nil @raise [Feedbook::Errors::NotifierNotifyError] if notify method fails

# File lib/feedbook/notifiers/twitter_notifier.rb, line 15
def notify(message)
  if client.nil?
    puts "Message has not been notified on Twitter: #{message} because of invalid client configuration"
  else
    client.update(message)
    puts "New message has been notified on Twitter: #{message}"
  end
rescue Twitter::Error => e
  p "TwitterNotifier did not notify because of client error (#{e.message})."
end