Sha256: 0bce6111245cefe423613756c25e35df2ca2285862d6176d3e7fcace5aab9527

Contents?: true

Size: 1.89 KB

Versions: 30

Compression:

Stored size: 1.89 KB

Contents

# encoding: utf-8
require 'uri'

module Backup
  module Notifier
    class Pushover < Base

      ##
      # The API User Token
      attr_accessor :user

      ##
      # The API Application Token
      attr_accessor :token

      ##
      # The user's device identifier to sent the message directly to,
      # rather than all of the user's devices
      attr_accessor :device

      ##
      # The message title
      attr_accessor :title

      ##
      # The priority of the notification
      attr_accessor :priority

      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)
        send_message(message.call(model, :status => status_data_for(status)))
      end

      def send_message(message)
        uri = 'https://api.pushover.net/1/messages.json'
        data = { :user => user, :token => token, :message => message }
        [:device, :title, :priority].each do |param|
          val = send(param)
          data.merge!(param => val) if val
        end
        options = {
          :headers  => { 'Content-Type' => 'application/x-www-form-urlencoded' },
          :body     => URI.encode_www_form(data)
        }
        options.merge!(:expects => 200) # raise error if unsuccessful
        Excon.post(uri, options)
      end

    end
  end
end

Version data entries

30 entries across 30 versions & 5 rubygems

Version Path
backup-ssh-4.4.5 lib/backup/notifier/pushover.rb
backup-remote-0.0.19 lib/backup/notifier/pushover.rb
cm-backup-1.0.0 lib/backup/notifier/pushover.rb
backup-ssh-4.4.4 lib/backup/notifier/pushover.rb
backup-ssh-4.4.3 lib/backup/notifier/pushover.rb
backup-4.4.1 lib/backup/notifier/pushover.rb
backup-remote-0.0.18 lib/backup/notifier/pushover.rb
backup-remote-0.0.17 lib/backup/notifier/pushover.rb
backup-remote-0.0.16 lib/backup/notifier/pushover.rb
backup-remote-0.0.15 lib/backup/notifier/pushover.rb
backup-ssh-4.4.2 lib/backup/notifier/pushover.rb
backup-ssh-4.4.1 lib/backup/notifier/pushover.rb
backup-remote-0.0.14 lib/backup/notifier/pushover.rb
backup-remote-0.0.13 lib/backup/notifier/pushover.rb
backup-remote-0.0.12 lib/backup/notifier/pushover.rb
backup-remote-0.0.11 lib/backup/notifier/pushover.rb
backup-ssh-4.4.0 lib/backup/notifier/pushover.rb
backup-remote-0.0.10 lib/backup/notifier/pushover.rb
backup-remote-0.0.9 lib/backup/notifier/pushover.rb
backup-remote-0.0.8 lib/backup/notifier/pushover.rb