10

When I try to start the server I get the following warning:

/Users/sumitkalra1984/MVP/config/initializers/devise.rb:5: warning: already initialized constant VERIFY_PEER   

My devise file:

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE if Rails.env.development?  

How do I find where the constant is already defined, and how do I overwrite that definition?

toro2k
  • 19,020
  • 7
  • 64
  • 71
Ishan Sharma
  • 117
  • 2
  • 10

2 Answers2

10

While I cannot find where else the constant is initialized, you can silence the warning by wrapping that line in a silence_warnings block.

silence_warnings do
  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE unless Rails.env.production?
end

See: http://api.rubyonrails.org/classes/Kernel.html#method-i-silence_warnings

ryanttb
  • 1,278
  • 13
  • 20
  • Note: I also changed the end of the line to 'unless Rails.env.production?' because I was getting the warning while running tests as well. – ryanttb Jan 23 '14 at 17:22
2

You can invoke OpenSSL::SSL.send(:remove_const, :VERIFY_PEER) before to unset the constant, set it as you need it, and restore it to its original value afterwards. Here is example code from a gist:

prev_setting = OpenSSL::SSL.send(:remove_const, :VERIFY_PEER)
OpenSSL::SSL.const_set(:VERIFY_PEER, OpenSSL::SSL::VERIFY_NONE)

# HTTP requests with DISABLED certificate verification go here.

OpenSSL::SSL.send(:remove_const, :VERIFY_PEER)
OpenSSL::SSL.const_set(:VERIFY_PEER, prev_setting)

 

Source and attribution: The solution comes from a comment by @sameers on Stack Overflow. Licenced under CC-BY-SA 4.0 as per the Stack Overflow user contribution licensing policy. The gist is assumed to be part of that as the author indicated their original intent of publishing it in a Stack Overflow comment.

tanius
  • 14,003
  • 3
  • 51
  • 63