Skip to content

Commit

Permalink
Change behavior of the output argument in Converter.convert
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
postmodern committed Jan 29, 2024
1 parent 185efbd commit 60e13a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
10 changes: 8 additions & 2 deletions lib/ronin/nmap/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

#
Expand Down
24 changes: 8 additions & 16 deletions lib/ronin/nmap/converters/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
22 changes: 8 additions & 14 deletions lib/ronin/nmap/converters/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@ module Converters
#
# Handles converting nmap XML into JSON.
#
# @api private
#
module JSON
#
# Converts parsed nmap XML to 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

Expand All @@ -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

Expand Down

0 comments on commit 60e13a5

Please sign in to comment.