Sha256: 0b1b1b73935c5f43c04b9de905d78c593619f3c7fd743e0f78fb27513ef580c1

Contents?: true

Size: 1.77 KB

Versions: 13

Compression:

Stored size: 1.77 KB

Contents

# encoding: utf-8
require 'twitter'

module Backup
  module Notifier
    class Twitter < Base

      ##
      # Twitter consumer key credentials
      attr_accessor :consumer_key, :consumer_secret

      ##
      # OAuth credentials
      attr_accessor :oauth_token, :oauth_token_secret

      def initialize(model, &block)
        super
        instance_eval(&block) if block_given?
      end

      private

      ##
      # Notify the user of the backup operation results.
      #
      # `status` indicates one of the following:
      #
      # `:success`
      # : The backup completed successfully.
      # : Notification will be sent if `on_success` is `true`.
      #
      # `:warning`
      # : The backup completed successfully, but warnings were logged.
      # : Notification will be sent if `on_warning` or `on_success` is `true`.
      #
      # `:failure`
      # : The backup operation failed.
      # : Notification will be sent if `on_warning` or `on_success` is `true`.
      #
      def notify!(status)
        tag = case status
              when :success then '[Backup::Success]'
              when :warning then '[Backup::Warning]'
              when :failure then '[Backup::Failure]'
              end
        message = "#{ tag } #{ model.label } (#{ model.trigger }) (@ #{ model.time })"
        send_message(message)
      end

      # Twitter::Client will raise an error if unsuccessful.
      def send_message(message)
        ::Twitter.configure do |config|
          config.consumer_key       = @consumer_key
          config.consumer_secret    = @consumer_secret
          config.oauth_token        = @oauth_token
          config.oauth_token_secret = @oauth_token_secret
        end

        client = ::Twitter::Client.new
        client.update(message)
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
backup-3.11.0 lib/backup/notifier/twitter.rb
backup-4.0.2 lib/backup/notifier/twitter.rb
nfm-backup-4.0.1a lib/backup/notifier/twitter.rb
backup-4.0.1 lib/backup/notifier/twitter.rb
backup-4.0.0 lib/backup/notifier/twitter.rb
backup-4.0.0rc1 lib/backup/notifier/twitter.rb
backup-3.10.0 lib/backup/notifier/twitter.rb
backup-3.9.0 lib/backup/notifier/twitter.rb
backup-3.8.0 lib/backup/notifier/twitter.rb
backup-3.7.2 lib/backup/notifier/twitter.rb
backup-3.7.1 lib/backup/notifier/twitter.rb
backup-3.7.0 lib/backup/notifier/twitter.rb
backup-3.6.0 lib/backup/notifier/twitter.rb