lib/ahoy_email.rb in ahoy_email-0.5.2 vs lib/ahoy_email.rb in ahoy_email-1.0.0

- old
+ new

@@ -1,45 +1,69 @@ +# dependencies require "active_support" -require "nokogiri" require "addressable/uri" +require "nokogiri" require "openssl" require "safely/core" + +# modules require "ahoy_email/processor" +require "ahoy_email/tracker" require "ahoy_email/interceptor" require "ahoy_email/mailer" -require "ahoy_email/engine" require "ahoy_email/version" +require "ahoy_email/engine" if defined?(Rails) module AhoyEmail - mattr_accessor :secret_token, :options, :subscribers, :belongs_to, :invalid_redirect_url + mattr_accessor :secret_token, :default_options, :subscribers, :invalid_redirect_url, :track_method, :api, :preserve_callbacks + mattr_writer :message_model - self.options = { + self.api = false + + self.default_options = { message: true, - open: true, - click: true, - utm_params: true, - utm_source: ->(message, mailer) { mailer.mailer_name }, + open: false, + click: false, + utm_params: false, + utm_source: -> { mailer_name }, utm_medium: "email", utm_term: nil, utm_content: nil, - utm_campaign: ->(message, mailer) { mailer.action_name }, - user: ->(message, mailer) { (message.to.size == 1 ? User.where(email: message.to.first).first : nil) rescue nil }, - mailer: ->(message, mailer) { "#{mailer.class.name}##{mailer.action_name}" }, + utm_campaign: -> { action_name }, + user: -> { @user || (respond_to?(:params) && params && params[:user]) || (message.to.size == 1 ? (User.find_by(email: message.to.first) rescue nil) : nil) }, + mailer: -> { "#{self.class.name}##{action_name}" }, url_options: {}, - heuristic_parse: false + extra: {}, + unsubscribe_links: false } - self.subscribers = [] + self.track_method = lambda do |data| + message = data[:message] - self.belongs_to = {} + ahoy_message = AhoyEmail.message_model.new + ahoy_message.to = Array(message.to).join(", ") if ahoy_message.respond_to?(:to=) + ahoy_message.user_type = data[:user_type] + ahoy_message.user_id = data[:user_id] - def self.track(options) - self.options = self.options.merge(options) - end + ahoy_message.mailer = data[:mailer] if ahoy_message.respond_to?(:mailer=) + ahoy_message.subject = message.subject if ahoy_message.respond_to?(:subject=) + ahoy_message.content = message.encoded if ahoy_message.respond_to?(:content=) - class << self - attr_writer :message_model + AhoyEmail::Processor::UTM_PARAMETERS.each do |k| + ahoy_message.send("#{k}=", data[k.to_sym]) if ahoy_message.respond_to?("#{k}=") + end + + ahoy_message.token = data[:token] if ahoy_message.respond_to?(:token=) + + ahoy_message.assign_attributes(data[:extra] || {}) + + ahoy_message.sent_at = Time.now + ahoy_message.save! end + + self.subscribers = [] + + self.preserve_callbacks = [] def self.message_model model = (defined?(@message_model) && @message_model) || ::Ahoy::Message model = model.call if model.respond_to?(:call) model