<commit_before>module Mailgun
  class Deliverer

    attr_accessor :settings

    def initialize(settings)
      self.settings = settings
    end

    def domain
      self.settings[:domain]
    end

    def api_key
      self.settings[:api_key]
    end

    def deliver!(rails_message)
      mailgun_message = prepare_mailgun_message_for(rails_message)
      remove_empty_values mailgun_message
      mailgun_client.send_message mailgun_message
    end

    private

    def prepare_mailgun_message_for(rails_message)
      mailgun_message = build_basic_mailgun_message_from_rails_message rails_message

      prepare_reply_to rails_message, mailgun_message if rails_message.reply_to
      prepare_mailgun_variables rails_message, mailgun_message
      prepare_mailgun_recipient_variables rails_message, mailgun_message

      mailgun_message
    end

    def build_basic_mailgun_message_from_rails_message(rails_message)
      {:from => rails_message.from, :to => rails_message.to, :subject => rails_message.subject,
       :html => extract_html(rails_message), :text => extract_text(rails_message)}
    end

    def prepare_reply_to(rails_message, mailgun_message)
      mailgun_message['h:Reply-To'] = rails_message.reply_to
    end

    # @see http://stackoverflow.com/questions/4868205/rails-mail-getting-the-body-as-plain-text
    def extract_html(rails_message)
      if rails_message.html_part
        rails_message.html_part.body.decoded
      else
        rails_message.content_type =~ /text\/html/ ? rails_message.body.decoded : nil
      end
    end

    def extract_text(rails_message)
      if rails_message.multipart?
        rails_message.text_part ? rails_message.text_part.body.decoded : nil
      else
        rails_message.content_type =~ /text\/plain/ ? rails_message.body.decoded : nil
      end
    end

    def prepare_mailgun_variables(rails_message, mailgun_message)
      rails_message.mailgun_variables.try(:each) do |name, value|
        mailgun_message["v:#{name}"] = value
      end
    end

    def prepare_mailgun_recipient_variables(rails_message, mailgun_message)
      mailgun_message['recipient-variables'] = rails_message.mailgun_recipient_variables.to_json if rails_message.mailgun_recipient_variables
    end

    def remove_empty_values(mailgun_message)
      mailgun_message.delete_if { |key, value| value.nil? }
    end

    def mailgun_client
      @maingun_client ||= Client.new(api_key, domain)
    end
  end
end

ActionMailer::Base.add_delivery_method :mailgun, Mailgun::Deliverer<commit_msg>Rename method<commit_after>module Mailgun
  class Deliverer

    attr_accessor :settings

    def initialize(settings)
      self.settings = settings
    end

    def domain
      self.settings[:domain]
    end

    def api_key
      self.settings[:api_key]
    end

    def deliver!(rails_message)
      mailgun_message = build_mailgun_message_for(rails_message)
      remove_empty_values mailgun_message
      mailgun_client.send_message mailgun_message
    end

    private

    def build_mailgun_message_for(rails_message)
      mailgun_message = build_basic_mailgun_message_from_rails_message rails_message

      prepare_reply_to rails_message, mailgun_message if rails_message.reply_to
      prepare_mailgun_variables rails_message, mailgun_message
      prepare_mailgun_recipient_variables rails_message, mailgun_message

      mailgun_message
    end

    def build_basic_mailgun_message_from_rails_message(rails_message)
      {:from => rails_message.from, :to => rails_message.to, :subject => rails_message.subject,
       :html => extract_html(rails_message), :text => extract_text(rails_message)}
    end

    def prepare_reply_to(rails_message, mailgun_message)
      mailgun_message['h:Reply-To'] = rails_message.reply_to
    end

    # @see http://stackoverflow.com/questions/4868205/rails-mail-getting-the-body-as-plain-text
    def extract_html(rails_message)
      if rails_message.html_part
        rails_message.html_part.body.decoded
      else
        rails_message.content_type =~ /text\/html/ ? rails_message.body.decoded : nil
      end
    end

    def extract_text(rails_message)
      if rails_message.multipart?
        rails_message.text_part ? rails_message.text_part.body.decoded : nil
      else
        rails_message.content_type =~ /text\/plain/ ? rails_message.body.decoded : nil
      end
    end

    def prepare_mailgun_variables(rails_message, mailgun_message)
      rails_message.mailgun_variables.try(:each) do |name, value|
        mailgun_message["v:#{name}"] = value
      end
    end

    def prepare_mailgun_recipient_variables(rails_message, mailgun_message)
      mailgun_message['recipient-variables'] = rails_message.mailgun_recipient_variables.to_json if rails_message.mailgun_recipient_variables
    end

    def remove_empty_values(mailgun_message)
      mailgun_message.delete_if { |key, value| value.nil? }
    end

    def mailgun_client
      @maingun_client ||= Client.new(api_key, domain)
    end
  end
end

ActionMailer::Base.add_delivery_method :mailgun, Mailgun::Deliverer
{'fraction_non_alphanumeric': 0.07171000788022065, 'fraction_numerical': 0.0027580772261623326, 'mean_word_length': 3.7096474953617813, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6853506', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1546, 'n_words': 262}