0

I am currently working on a monkey patch in the JRuby Mail-2.6.0 module. Specifically, I want to patch the ssl_context class method. If it helps, this is the full path to the module.

An issue I am having with the monkey patch is that since I am looking to replace a class method, I am not quite sure what method to use to replace ssl_context.

This is what I have for the monkey patch:

require 'mail/check_delivery_params'

class << Mail::SMTP
  remove_method :ssl_context
end

module Mail
  class SMTP
        # Allow SSL context to be configured via settings, for Ruby >= 1.9
    # Just returns openssl verify mode for Ruby 1.8.x
    def ssl_context
      openssl_verify_mode = settings[:openssl_verify_mode]

      if openssl_verify_mode.kind_of?(String)
        openssl_verify_mode = "OpenSSL::SSL::VERIFY_#{openssl_verify_mode.upcase}".constantize
      end

      # context = Net::SMTP.default_ssl_context
      context = OpenSSL::SSL::SSLContext.new(settings[:ssl_version])
      context.verify_mode = openssl_verify_mode
      context.ca_path = settings[:ca_path] if settings[:ca_path]
      context.ca_file = settings[:ca_file] if settings[:ca_file]
      context
    end
  end
end

I am basically just trying to change:

context = Net::SMTP.default_ssl_context

To:

context = OpenSSL::SSL::SSLContext.new(settings[:ssl_version])

I've tried undef_method, remove_class_variable, and remove_method, but kept getting the following type of error:

2017-05-17 11:44:03.423 -0700 [ERROR|007d0|main] :: Error starting Application: Undefined method ssl_context for 'Mail::SMTP'
  org/jruby/RubyModule.java:2826:in `undef_method'
  /Users/App/Code/yourtool/lib/helltool/mailer.rb:328:in `singleton class'
  /Users/App/Code/yourtool//lib/helltool/mailer.rb:327:in `<main>'
  org/jruby/RubyKernel.java:961:in `require'

Curious what I can do to make this work?

theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
  • The error is telling you that `Mail::SMTP` does not have a method called `ssl_context`. This method is an instance method not a class method. – engineersmnky May 17 '17 at 18:58
  • It looks like instead of using `remove_method`, I simply redefine the method (and adding a `private` on top of the class method), like this: http://stackoverflow.com/questions/33592221/override-a-private-method-in-the-lib-folder-of-a-gem – theGreenCabbage May 17 '17 at 18:59
  • This is not a class method that being said your patch should be fine without the first block `class << Mail::SMTP;...` and yes if you want it to be private make it private. Also might be easier to alias the method and make a call to the alias when you want the original functionality. – engineersmnky May 17 '17 at 19:02
  • @engineersmnky Would you mind writing this as an answer? "Also might be easier to alias the method and make a call to the alias when you want the original functionality." – theGreenCabbage May 17 '17 at 19:03

0 Answers0