Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAVON 3 Use of Nori should include configurable nori options #543

Closed
ashrocket opened this issue Jan 25, 2014 · 5 comments
Closed

SAVON 3 Use of Nori should include configurable nori options #543

ashrocket opened this issue Jan 25, 2014 · 5 comments

Comments

@ashrocket
Copy link
Contributor

Though the change log current says that your are not using Nori, you are still using it
in the response. Unfortunately for me one of the cool elements of using the response header, in the request headers (for soap services that return a changing token in the Securtiy header) is not working since you default to snake_case on the symobls returned.

Can we get :

  def self.configure
      yield self
    end


in the savon.rb file  so we can set some options:
mattr_accessor :nori_options
@@nori_options =     nori_options = {
    strip_namespaces: true,
    convert_tags_to: lambda { |tag| tag.snakecase.to_sym }
  }

I'll be happy to fork it, but I'm terrible at writing rspec.

Or if this is undesirable (because you are referencing :envelope, and :body direectly in the response Object)
is there some otherway you recommend getting header elements into camelcase?

For now, I've monkeypatched Nori to camelcase anything inside [:envelope][:header] in my app using :symbol_name.to_s.camelcase.to_sym

@ashrocket
Copy link
Contributor Author

Ditched the monkey patch in favor of a direct solution:
In the code where I'm storing the security header for re-use I'm doing this:

    def process_header response
      @security_header = camelcase_keys(response.hash[:envelope][:header][:security])
      next_message
    end

where next_message is a function I have to increment my message numebr
and camelcase_keys is defined below to recursively camelcase any symbol keys to match the required input hash
for the service I'm using.

   def camelcase_keys element
      if element.is_a? Hash
        new_element = {}
        element.each do |name,el|
          new_element[name.to_s.camelcase.to_sym] = camelcase_keys(el)
        end
        new_element
      elsif element.is_a? Array
        element.map{|el| camelcase_keys(el)}
      else
        element
      end
    end

@proby
Copy link

proby commented Feb 3, 2014

👍 on this. I don't want all my keys to be symbols. I'd like a way to configure Nori such that it only snake case's the keys, leaving them as strings.

@ashrocket
Copy link
Contributor Author

change the above code:

element.each do |name,el|
          new_element[name.to_s.camelcase.to_sym] = camelcase_keys(el)
 end

to

element.each do |name,el|
          new_element[name.to_s] = camelcase_keys(el)
end

And you shall have that which you desired

@tjarratt
Copy link
Contributor

tjarratt commented Feb 5, 2014

@ashrocket I think allowing users to set the Nori options for parsing the response is a reasonable request for Savon3. The method you laid out seems good, but I'd definitely want to get some tests for this behavior before merging anything in.

Seems like a simple test where you pass in a convert_tags_to lambda and the response parsed matches some known good response would be a reasonable first pass at a test for this.

Re: symbols, @proby, there's an outstanding issue in #473 to switch from symbols to strings to avoid a memory leak. Hoping to address that soon.

@ashrocket
Copy link
Contributor Author

Sure, I think we can add something like ActiveModel::Validator
http://api.rubyonrails.org/classes/ActiveModel/Validator.html

class Person
  include ActiveModel::Validations
  validates_with MyValidator
end

class MyValidator < ActiveModel::Validator
  def validate(record)
    if some_complex_logic
      record.errors[:base] = "This record is invalid"
    end
  end

  private
    def some_complex_logic
      # ...
    end
end

Only savon's api would require a method for convert convert_tags_to

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants