Search code examples
rubyrubygemssavon

undefined method `configure' for Savon:Module


I'm getting the above error in a gem with this code snippet

Savon.configure do |config|
  config.log = false
  config.log_level = :error
  HTTPI.log = false
end

This code used to pass in past runs on Travis, so I'm not sure why this changed when I altered the Readme.


Solution

  • Part of this confusion comes from my situation--inheriting a gem to maintain--along with this line in the gemspec:

    gem.add_dependency 'savon'
    

    There's no version number specified, so the newest run switched over to using Savon 2, which ditched the Savon.configure global behavior. If you're in the same boat as me, changing this line to the last pre-2.0 version of Savon will resolve the issue:

    gem.add_dependency 'savon', '~>1.2.0'
    

    Then bundle install and you should be good.


    Or you want to upgrade your code. I know I do.

    Savon.configure was removed from Savon 2.0 because the "problem was global state". The quickest way to keep the behavior the same in your app would be to define a app-level global hash in the same place. You'd then pass this hash into every Savon.client call you make. For instance:

    # Where Savon.configure was called
    APP_OPTS = {
      # disable request logging, silences HTTPI as well
      log:       false,
      # Don't log Laundry xmls to STDOUT
      log_level: :error,
      #... etc
    }
    
    # Elsewhere
    @client = Savon::Client.new(APP_OPTS)
    

    I'd consider this a starting point to migrating to the 2.0 configuration style. Ideally, you should always consider the client-specific 2.0 options available when initializing each Savon client.