Class: Dachsfisch::JSON2XMLConverter

Inherits:
ConverterBase show all
Defined in:
lib/dachsfisch/json2_xml_converter.rb

Instance Method Summary collapse

Methods inherited from ConverterBase

perform

Constructor Details

#initialize(json:) ⇒ JSON2XMLConverter

Returns a new instance of JSON2XMLConverter.



5
6
7
8
9
10
# File 'lib/dachsfisch/json2_xml_converter.rb', line 5

def initialize(json:)
  super()
  @json_hash = JSON.parse json
rescue TypeError, JSON::ParserError => e
  raise InvalidJSONInputError.new(e.message)
end

Instance Method Details

#executeObject



12
13
14
15
16
17
18
19
# File 'lib/dachsfisch/json2_xml_converter.rb', line 12

def execute
  fragment = Nokogiri::XML::DocumentFragment.new(Nokogiri::XML::Document.new)

  Nokogiri::XML::Builder.with fragment do |xml|
    add_element xml, @json_hash
  end
  fragment.elements.deconstruct.map(&:to_xml).join("\n")
end