class Noticed::DeliveryMethods::Base

Attributes

notification[R]
options[R]
params[R]
recipient[R]
record[R]

Public Class Methods

option(*names)
Alias for: options
options(*names) click to toggle source
# File lib/noticed/delivery_methods/base.rb, line 18
def options(*names)
  option_names.concat Array.wrap(names)
end
Also aliased as: option
validate!(delivery_method_options) click to toggle source
# File lib/noticed/delivery_methods/base.rb, line 23
def validate!(delivery_method_options)
  option_names.each do |option_name|
    unless delivery_method_options.key? option_name
      raise ValidationError, "option `#{option_name}` must be set for #{name}"
    end
  end
end

Public Instance Methods

deliver() click to toggle source
# File lib/noticed/delivery_methods/base.rb, line 51
def deliver
  raise NotImplementedError, "Delivery methods must implement a deliver method"
end
perform(args) click to toggle source
# File lib/noticed/delivery_methods/base.rb, line 32
def perform(args)
  @notification = args[:notification_class].constantize.new(args[:params])
  @options = args[:options]
  @params = args[:params]
  @recipient = args[:recipient]
  @record = args[:record]

  # Make notification aware of database record and recipient during delivery
  @notification.record = args[:record]
  @notification.recipient = args[:recipient]

  return if (condition = @options[:if]) && !@notification.send(condition)
  return if (condition = @options[:unless]) && @notification.send(condition)

  run_callbacks :deliver do
    deliver
  end
end

Private Instance Methods

post(url, args = {}) click to toggle source

Helper method for making POST requests from delivery methods

Usage:

post("http://example.com", basic_auth: {user:, pass:}, json: {}, form: {})
# File lib/noticed/delivery_methods/base.rb, line 62
def post(url, args = {})
  basic_auth = args.delete(:basic_auth)

  request = if basic_auth
    HTTP.basic_auth(user: basic_auth[:user], pass: basic_auth[:pass])
  else
    HTTP
  end

  response = request.post(url, args)

  if options[:debug]
    Rails.logger.debug("POST #{url}")
    Rails.logger.debug("Response: #{response.code}: #{response}")
  end

  if !options[:ignore_failure] && !response.status.success?
    raise ResponseUnsuccessful.new(response)
  end

  response
end