Sha256: 0fb41b166e2baebc0ebf124ee2ba4005d5ca2d7ff145e9384f424797b227f26d

Contents?: true

Size: 1.84 KB

Versions: 30

Compression:

Stored size: 1.84 KB

Contents

# encoding: utf-8
require 'uri'

module Backup
  module Notifier
    class Prowl < Base

      ##
      # Application name
      # Tell something like your server name. Example: "Server1 Backup"
      attr_accessor :application

      ##
      # API-Key
      # Create a Prowl account and request an API key on prowlapp.com.
      attr_accessor :api_key

      def initialize(model, &block)
        @message = lambda do |model, data|
          "#{ model.label } (#{ model.trigger })"
        end
        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(status)
      end

      def send_message(status)
        uri = 'https://api.prowlapp.com/publicapi/add'
        status_data = status_data_for(status)
        data = {
          :application  => application,
          :apikey       => api_key,
          :event        => status_data[:message],
          :description  => message.call(model, :status => status_data)
        }
        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-remote-0.0.7 lib/backup/notifier/prowl.rb
backup-remote-0.0.5 lib/backup/notifier/prowl.rb
backup-remote-0.0.3 lib/backup/notifier/prowl.rb
backup-remote-0.0.2 lib/backup/notifier/prowl.rb
backup-4.4.0 lib/backup/notifier/prowl.rb
backup-4.3.0 lib/backup/notifier/prowl.rb
backup-4.2.3 lib/backup/notifier/prowl.rb
backup-4.2.2 lib/backup/notifier/prowl.rb
backup_zh-4.2.0 lib/backup/notifier/prowl.rb
backup-4.2.0 lib/backup/notifier/prowl.rb