Class: CSVStore
- Inherits:
-
Object
- Object
- CSVStore
- Defined in:
- lib/csvget.rb
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(options = {}) ⇒ CSVStore
constructor
A new instance of CSVStore.
- #put(host, tmpfile) ⇒ Object
- #walk(data, prefix = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CSVStore
Returns a new instance of CSVStore.
9 10 11 12 13 14 15 16 |
# File 'lib/csvget.rb', line 9 def initialize( = {}) @output_folder = [:prefix] || "." @filters = [:filter] || [] FileUtils.mkdir_p(@output_folder) @parselets = ([:parselets] || []).map{|path| Parsley.new(File.read(path)) } @files = {} @headers = {} end |
Instance Method Details
#close ⇒ Object
49 50 51 52 53 |
# File 'lib/csvget.rb', line 49 def close @files.each do |k, v| v.close end end |
#put(host, tmpfile) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/csvget.rb', line 18 def put(host, tmpfile) @parselets.each do |parselet| begin type = (`file "#{tmpfile.path}"` =~ /xml/i) ? :xml : :html output = parselet.parse(:file => tmpfile.path, :input => type) walk(output) rescue ParsleyError => e STDERR.puts "warning: #{e.}" end end end |
#walk(data, prefix = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/csvget.rb', line 30 def walk(data, prefix = nil) data.each do |prefix, values| values = [values] unless values.is_a?(Array) file_name = File.join(@output_folder, "#{prefix}.csv") h = @headers[prefix] ||= values.first.keys should_write_headers = !File.exists?(file_name) f = @files[prefix] ||= FasterCSV.open(file_name, "a", :headers => h, :write_headers => should_write_headers) values.each do |hash| arr = h.inject([]) do |memo, key| memo << hash[key] end @row = FasterCSV::Row.new(h, arr) @filters.each {|filter| eval(filter) } f << @row end end end |