Skip to content

Commit

Permalink
Resolve Nokogiri deprecation warning
Browse files Browse the repository at this point in the history
This fixes the following deprecation warning:

```
worldpay_cnp-0.1.0/lib/worldpay_cnp/xml/nokogiri.rb:36: warning: Passing a Node as the second parameter to Node.new is deprecated. Please pass a Document instead, or prefer an alternative constructor like Node#add_child. This will become an error in a future release of Nokogiri.
```

We are now passing the document along with the parent element. The serializer will handle the case of setting the parent to the document, when the parent is nil due to the recursive nature of the method calls.

This deprecation was included in release 1.13.0 https://github.com/sparklemotion/nokogiri/releases/tag/v1.13.0

For reference: sparklemotion/nokogiri#975 and sparklemotion/nokogiri#2401
  • Loading branch information
javierjulio committed Jan 27, 2022
1 parent f9a94d0 commit 52981bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/worldpay_cnp/xml/nokogiri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class Serializer
include XML::Serializer

def call(hash)
::Nokogiri::XML::Document.new.tap { |d| add_xml_elements!(d, hash) }.to_s
::Nokogiri::XML::Document.new.tap { |d| add_xml_elements!(d, nil, hash) }.to_s
end

private

def attributes_or_elements!(parent, key, value)
def attributes_or_elements!(document, parent, key, value)
return parent[attribute_name(key)] = text_with(value) if attribute?(key)
element = ::Nokogiri::XML::Element.new(key.to_s, parent)
element = ::Nokogiri::XML::Element.new(key.to_s, document)
parent.add_child(element)
add_xml_elements!(element, value)
add_xml_elements!(document, element, value)
end

def insert_text!(element, text)
Expand Down
9 changes: 5 additions & 4 deletions lib/worldpay_cnp/xml/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ def call(hash)

private

def attributes_or_elements!(parent, key, value)
def attributes_or_elements!(document, parent, key, value)
raise NotImplementedError
end

def insert_text!(element, text)
raise NotImplementedError
end

def add_xml_elements!(parent, obj)
def add_xml_elements!(document, parent, obj)
parent = document if parent.nil?
case obj
when Hash
obj.each { |key, value| attributes_or_elements!(parent, key, value) }
obj.each { |key, value| attributes_or_elements!(document, parent, key, value) }
when Array
obj.each { |value| add_xml_elements!(parent, value) }
obj.each { |value| add_xml_elements!(document, parent, value) }
else
insert_text!(parent, obj)
end
Expand Down

0 comments on commit 52981bb

Please sign in to comment.