Skip to content

Commit

Permalink
Updated data_set.rb to_csv to use lib CSV too
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Peters committed May 15, 2009
1 parent 1d1bcf1 commit 5b3c8c4
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions lib/gattica/data_set.rb
Expand Up @@ -25,24 +25,21 @@ def initialize(xml)
def to_csv(format = :long)
# build the headers
output = ''

columns = []

# only show the nitty gritty details of id, updated_at and title if requested
case format
when :long
output = '"id","updated","title",'
columns << ["id", "updated", "title"]
end

unless @points.empty? # if there was at least one result
output += @points.first.dimensions.collect do |dimension|
"\"#{dimension.key.to_s}\""
end.join(',')
output += ','
output += @points.first.metrics.collect do |metric|
"\"#{metric.key.to_s}\""
end.join(',')
output += "\n"
columns << @points.first.dimensions.map {|d| d.key}
columns << @points.first.metrics.map {|m| m.key}
end

output = CSV.generate_line(columns) + "\n"

# get the data from each point
@points.each do |point|
output += point.to_csv(format) + "\n"
Expand Down

0 comments on commit 5b3c8c4

Please sign in to comment.