From 60e13a588b1a86ed9785929807d956086c38a735 Mon Sep 17 00:00:00 2001 From: Postmodern Date: Mon, 29 Jan 2024 07:50:52 -0800 Subject: [PATCH] Change behavior of the `output` argument in `Converter.convert` * Changed `Converter::JSON.convert` and `Converter::CSV.convert` to not return a String, but simply write the CSV or JSON to an IO or StringIO output argument. --- lib/ronin/nmap/converter.rb | 10 ++++++++-- lib/ronin/nmap/converters/csv.rb | 24 ++++++++---------------- lib/ronin/nmap/converters/json.rb | 22 ++++++++-------------- 3 files changed, 24 insertions(+), 32 deletions(-) diff --git a/lib/ronin/nmap/converter.rb b/lib/ronin/nmap/converter.rb index d847aa4..3dd5e55 100644 --- a/lib/ronin/nmap/converter.rb +++ b/lib/ronin/nmap/converter.rb @@ -69,7 +69,7 @@ def self.convert_file(src,dest, format: infer_format_for(dest)) # @param [::Nmap::XML] xml # The nmap XML to convert. # - # @param [IO, String, nil] output + # @param [IO, nil] output # Optional output to write the converted output to. # # @param [:json, :csv] format @@ -81,7 +81,13 @@ def self.convert_file(src,dest, format: infer_format_for(dest)) # @api public # def self.convert(xml,output=nil, format: ) - Converters[format].convert(xml,output) + if output + Converters[format].convert(xml,output) + else + output = StringIO.new + convert(xml,output, format: format) + output.string + end end # diff --git a/lib/ronin/nmap/converters/csv.rb b/lib/ronin/nmap/converters/csv.rb index 2370670..ec2edd6 100644 --- a/lib/ronin/nmap/converters/csv.rb +++ b/lib/ronin/nmap/converters/csv.rb @@ -26,21 +26,18 @@ module Converters # # Handles converting nmap XML into CSV. # + # @api private + # module CSV # # Converts parsed nmap XML into CSV. # # @param [::Nmap::XML] xml # - # @param [String, IO] output - # The optional output to write the CSV to. - # - # @return [String, IO] - # The CSV output. - # - # @api public + # @param [IO, StringIO] output + # The output to write the CSV to. # - def self.convert(xml,output=String.new) + def self.convert(xml,output) xml_to_csv(xml,output) end @@ -50,18 +47,13 @@ def self.convert(xml,output=String.new) # @param [::Nmap::XML] xml # The parsed nmap XML to convert to CSV. # - # @param [String, IO] output - # The optional output to write the CSV to. + # @param [IO, StringIO] output + # The output to write the CSV to. # - # @return [String, IO] - # The CSV output. - # - def self.xml_to_csv(xml,output=String.new) + def self.xml_to_csv(xml,output) xml_to_rows(xml) do |row| output << ::CSV.generate_line(row) end - - return output end # CSV rows header. diff --git a/lib/ronin/nmap/converters/json.rb b/lib/ronin/nmap/converters/json.rb index 8d5e6d3..e7bd68a 100644 --- a/lib/ronin/nmap/converters/json.rb +++ b/lib/ronin/nmap/converters/json.rb @@ -26,6 +26,8 @@ module Converters # # Handles converting nmap XML into JSON. # + # @api private + # module JSON # # Converts parsed nmap XML to JSON. @@ -33,15 +35,10 @@ module JSON # @param [::Nmap::XML] xml # The parsed nmap XML. # - # @param [IO, nil] output - # Optional output stream to write the JSON to. - # - # @return [String] - # The raw JSON. + # @param [IO, StringIO] output + # The output stream to write the JSON to. # - # @api public - # - def self.convert(xml,output=nil) + def self.convert(xml,output) xml_to_json(xml,output) end @@ -51,13 +48,10 @@ def self.convert(xml,output=nil) # @param [::Nmap::XML] xml # The parsed nmap XML. # - # @param [IO, nil] output - # Optional output stream to write the JSON to. - # - # @return [String] - # The raw JSON. + # @param [IO, StringIO] output + # The output stream to write the JSON to. # - def self.xml_to_json(xml,output=nil) + def self.xml_to_json(xml,output) ::JSON.dump(xml_as_json(xml),output) end