Sha256: 7eef6ecf00d90c3e5d81452aa4dde4b267fac1d1dc7e36103718e5d0076ecb8b

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'exception_notification/pushbullet/version'
require 'pushbullet'

module ExceptionNotifier
  class PushbulletNotifier
    attr_reader :users

    def initialize(options)
      @users = options[:users]
      raise(ArgumentError, 'Users must be specified') unless @users
      users.each do |user|
        raise(ArgumentError, 'api_token must be specified') unless user[:api_token]

        Pushbullet.api_token = user[:api_token]
        user[:device_idens] = Pushbullet::Device.all.map(&:iden) unless user[:device_idens]
      end

      Pushbullet.api_token = nil
      @title = options[:title]
    end

    def call(exception, options = {})
      @users.each do |user|
        Pushbullet.api_token = user[:api_token]
        user[:device_idens].each do |device_iden|
          Pushbullet::Push.create_note(device_iden, title, body(exception))
        end
      end

      Pushbullet.api_token = nil
    end

    def title
      if @title
        @title
      elsif app && environment
        "New exception occured in #{app}(#{environment})."
      else
        'New exception occured.'
      end
    end

    def body(exception)
      body = "'#{exception.message}'"
      body += " on '#{exception.backtrace.first}'" if exception.backtrace
      body
    end

    def app
      Rails.application.class.parent_name if defined?(Rails.application)
    end

    def environment
      Rails.env if defined?(Rails.env)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
exception_notification-pushbullet-0.1.0 lib/exception_notifier/pushbullet_notifier.rb