class PaperPlane::Base

Attributes

flight_routes[R]
mid[R]
params[R]
skipped_flight_routes[R]

Public Class Methods

flight_routes() click to toggle source
# File lib/paper_plane/base.rb, line 29
def flight_routes
  @flight_routes ||= {}
end
fly_action(mid, **kwargs) click to toggle source
# File lib/paper_plane/base.rb, line 41
def fly_action(mid, **kwargs)
  new(**kwargs).tap do |paper_plane|
    paper_plane.run_callbacks(:flying) do
      paper_plane.send(mid)
      paper_plane.fly(mid)
    end
  end
end
inherited(subclass) click to toggle source
# File lib/paper_plane/base.rb, line 63
def self.inherited(subclass)
  subclass.class_eval do
    object_type :paper_plane
    register_flight_route :email, PaperPlane::FlightRoutes::Email
    register_flight_route :sms, PaperPlane::FlightRoutes::Sms
  end
end
method_missing(mid, *args, &block) click to toggle source
Calls superclass method
# File lib/paper_plane/base.rb, line 50
def method_missing(mid, *args, &block)
  if instance_methods(false).include? mid
    fly_action(mid, *args)
  else
    super
  end
end
new(**kwargs) click to toggle source

set the routes to fly

# File lib/paper_plane/base.rb, line 74
def initialize(**kwargs)
  @template_formats = template_formats
  @skipped_flight_routes = []
  @flight_routes = self.class.flight_routes
  @paper_plane_name = base_klass_string
  @recipient ||= kwargs.delete(:to)
  @async ||= kwargs.delete(:async) || ENV['RAILS_ENV'] == 'development'
  @params = kwargs
end
register_flight_route(route_name, flight_route) click to toggle source
# File lib/paper_plane/base.rb, line 33
def register_flight_route(route_name, flight_route)
  flight_routes[route_name] = flight_route
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/paper_plane/base.rb, line 58
def respond_to_missing?(method_name, include_private = false)
  instance_methods(false).include?(method_name) || super
end
skip(*flight_routes_types) click to toggle source
# File lib/paper_plane/base.rb, line 23
def skip(*flight_routes_types)
  define_method(:skipped_flight_routes) do
    @skipped_flight_routes = flight_routes_types
  end
end
template_formats(**map) click to toggle source
# File lib/paper_plane/base.rb, line 37
def template_formats(**map)
  @template_formats = map || {}
end

Public Instance Methods

context() click to toggle source

extracts context from the method in the paper_plane, to be injected in each flight route

# File lib/paper_plane/base.rb, line 113
def context
  instance_variables.map { |attribute| [attribute, instance_variable_get(attribute)] }.to_h
end
fly(mid) click to toggle source

set message and call private do_fly method

# File lib/paper_plane/base.rb, line 85
def fly(mid)
  @mid = mid

  if @async
    do_fly
  else
    PaperPlane::FlyJob.fly_later(self.class.to_s, mid, **params)
  end
end
fly_now!(mid) click to toggle source

flies without enqueing jobs

# File lib/paper_plane/base.rb, line 96
def fly_now!(mid)
  @async = true

  fly(mid)
end
flying_route?(route_type) click to toggle source

determines whether the piegon is going to fly this route

# File lib/paper_plane/base.rb, line 108
def flying_route?(route_type)
  skipped_flight_routes.exclude? route_type
end
skip(*flight_routes_types) click to toggle source

determines which routes the paper_plane should skip

# File lib/paper_plane/base.rb, line 103
def skip(*flight_routes_types)
  @skipped_flight_routes.concat(flight_routes_types)
end

Private Instance Methods

_logger() click to toggle source
# File lib/paper_plane/base.rb, line 147
def _logger
  @_logger ||= Logger.new(STDOUT)
end
do_fly() click to toggle source
# File lib/paper_plane/base.rb, line 121
def do_fly
  flight_routes.each do |route_name, _route_object|
    # next unless route.fly?(args)
    next unless flying_route?(route_name)

    fly_route(route_name)
  end
  self
end
fly_route(type) click to toggle source
# File lib/paper_plane/base.rb, line 131
def fly_route(type)
  raise NoMessageError, 'There are no messages to deliver' unless mid

  begin
    flight_routes[type].fly(mid, context, **params)
  rescue
    _logger.error "Error in #{self.class}##{mid}: Could not send #{type.to_s.titleize}!."
  else
    _logger.debug "#{type.to_s.titleize} processed."
  end
end
template_formats() click to toggle source
# File lib/paper_plane/base.rb, line 143
def template_formats
  self.class.template_formats
end