class Ralphttp::CsvExport
Attributes
data[RW]
header[RW]
Public Class Methods
new(header = nil)
click to toggle source
Public - Start the class and pass the header fields along with the data
header - Array holding the header field values
Example:
csv = Ralphttp::CsvExport( %w(Time Latency Response) )
Returns nil
# File lib/ralphttp/csvexport.rb, line 13 def initialize(header = nil) @data = [] add_header(header) end
Public Instance Methods
add_header(header)
click to toggle source
Public - Add header description for the CSV fields
header - Array of field data
Retuns nil
# File lib/ralphttp/csvexport.rb, line 60 def add_header(header) @header = header.join(',') if header.kind_of?(Array) end
add_row(row)
click to toggle source
Public - Add a row to the data Array
row - Array holding field values
Returns nil
# File lib/ralphttp/csvexport.rb, line 69 def add_row(row) @data << row.join(',') if row.kind_of?(Array) end
print()
click to toggle source
Public - Print out the CSV data
Example:
csv = Ralphttp::CsvExport.new csv.add_header(%w(Title Name Location) csv.add_row(%(CEO Khan Nebula)) csv.print # => Title,Name,Location # => CEO,Khan,Nebula
Returns String list in CSV format
# File lib/ralphttp/csvexport.rb, line 29 def print @data.unshift(@header) @data.each do |d| puts d end end
write(file)
click to toggle source
Public - Write the parsed data to a specified file
file - String File location
Example:
csv.write('/tmp/file.csv')
Returns nil
# File lib/ralphttp/csvexport.rb, line 45 def write(file) @data.unshift(@header) storage = File.open(file, 'w') @data.each do |d| storage.write("#{d}\n") end storage.close end