Class: Birdwatcher::Modules::Reporting::Csv

Inherits:
Birdwatcher::Module show all
Defined in:
lib/birdwatcher/modules/reporting/csv.rb

Constant Summary

Constants inherited from Birdwatcher::Module

Birdwatcher::Module::MODULE_PATH

Constants included from Concerns::Concurrency

Concerns::Concurrency::DEFAULT_THREAD_POOL_SIZE

Constants included from Concerns::Core

Concerns::Core::DATA_DIRECTORY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Birdwatcher::Module

_file_path, _file_path=, descendants, #execute, inherited, meta, meta=, module_by_path, module_paths, modules, path

Methods included from Concerns::WordList

included, #make_word_list

Methods included from Concerns::Concurrency

included, #thread_pool

Methods included from Concerns::Persistence

included, #save_status, #save_user

Methods included from Concerns::Presentation

included, #make_status_summary_output, #make_url_summary_output, #make_user_details_output, #make_user_summary_output, #output_status_summary, #output_user_details, #output_user_summary, #page_text

Methods included from Concerns::Outputting

#confirm, #error, #fatal, included, #info, #line_separator, #newline, #output, #output_formatted, #task, #warn

Methods included from Concerns::Util

#escape_html, #excerpt, included, #number_to_human_size, #parse_time, #pluralize, #strip_control_characters, #strip_html, #suppress_output, #suppress_warnings, #time_ago_in_words, #unescape_html

Methods included from Concerns::Core

#console, #current_workspace, #current_workspace=, #database, included, #klout_client, #read_data_file, #twitter_client

Class Method Details

.infoObject



29
30
31
32
33
34
35
36
37
# File 'lib/birdwatcher/modules/reporting/csv.rb', line 29

def self.info
<<-INFO
The CSV exporter can write the results of an SQL query to a file in CSV format.

#{'IMPORTANT:'.bold} The module does not limit the data returned from the query
to the currently active workspace, the query will need to take that in to
consideration if necessary.
INFO
end

Instance Method Details

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/birdwatcher/modules/reporting/csv.rb', line 39

def run
  result      = nil
  rows        = nil
  headers     = nil
  csv         = nil
  destination = option_setting("DEST")
  task("Executing SQL query...") do
    begin
      result  = database[option_setting("QUERY")]
      rows    = result.map { |r| r.to_hash.values }
      headers = result.columns.map { |c| c.to_s }
    rescue Sequel::DatabaseError => e
      error("Syntax error: #{e.message}")
      return false
    end
  end
  task("Generating CSV...") do
    csv = CSV.generate(:write_headers => option_setting("HEADERS"), :headers => headers) do |doc|
      rows.each { |r| doc << r }
    end
  end
  task("Writing #{pluralize(rows.count, 'row', 'rows')} to file...") do
    File.open(destination, "w") do |f|
      f.write(csv)
    end
  end
  file_size = number_to_human_size(File.size(destination))
  info("Wrote #{file_size.bold} to #{destination.bold}")
end