Skip to content

Commit

Permalink
changed Nori#find to ignore namespaces /cc #474
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiii committed Jun 30, 2013
1 parent 27d13fe commit 41b7f1d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# UPCOMING

* Change: `Nori#find` now ignores namespace prefixes in Hash keys it is searching through.

# 2.2.0 (2013-04-25)

* Feature: [#42](https://github.com/savonrb/nori/pull/42) adds the `:delete_namespace_attributes`
Expand Down
13 changes: 11 additions & 2 deletions lib/nori.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def find(hash, *path)
key = path.shift
key = self.class.hash_key(key, @options)

return nil unless hash.include? key
find(hash[key], *path)
value = find_value(hash, key)
find(value, *path) if value
end

def parse(xml)
Expand All @@ -60,4 +60,13 @@ def validate_options!(available_options, options)
end
end

def find_value(hash, key)
hash.each do |k, v|
key_without_namespace = k.to_s.split(':').last
return v if key_without_namespace == key.to_s
end

nil
end

end
8 changes: 8 additions & 0 deletions spec/nori/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@
result = @nori.find(@hash, 'userResponse', 'accountStatus')
expect(result).to eq("active")
end

it 'strips the namespaces from Hash keys' do
xml = '<v1:userResponse xmlns:v1="http://example.com"><v1:accountStatus>active</v1:accountStatus></v1:userResponse>'
hash = @nori.parse(xml)

result = @nori.find(hash, 'userResponse', 'accountStatus')
expect(result).to eq("active")
end
end

context "#parse" do
Expand Down

1 comment on commit 41b7f1d

@rubiii
Copy link
Contributor Author

@rubiii rubiii commented on 41b7f1d Jun 30, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is related to savonrb/savon#474

Please sign in to comment.