
Security News
Follow-up and Clarification on Recent Malicious Ruby Gems Campaign
A clarification on our recent research investigating 60 malicious Ruby gems.
= ToCSV
ToCSV is a gem for converting arrays or scopes (Rails) to CSV by calling +to_csv+. These arrays can contain different data structures, as long as they are homogeneous, like the ones described below:
=== Requirements
ToCSV has been tested with Ruby 1.8.7-p299/1.9.1-p378/1.9.2-rc1.
If you are using Ruby 1.9 the only dependency to use the basic features is +ActiveSupport+. Otherwise you will need +fastercsv+. You will receive a warning if you don't meet the requirements.
$ sudo gem install activesupport
$ sudo gem install fastercsv
Full compatibility with Rails 3 is on the way...as well as a new API, with new features.
=== Configuration
If you want to use this gem with Rails, put the following requirement in your environment.rb:
config.gem 'to-csv', :lib => 'to_csv', :source => 'http://rubygems.org'
After that, if you need to globally configure the gem, just create a to_csv.rb file in initializers.
ToCSV.byte_order_marker = true ToCSV.timestamps = true ToCSV.locale = 'en-US' ToCSV.primary_key = false ToCSV.csv_options[:col_sep] = ',' ToCSV.csv_options[:row_sep] = "\r\n"
== Examples
Let's start with the most simple example.
['Alfred Hitchcock', 'Robert Mitchum', 'Lucille Ball'].to_csv #=> "Alfred Hitchcock;Robert Mitchum;Lucille Ball\n"
Or, if we have an array of arrays (i.e. a matrix) we can create tabular data. [ ['Name', 'Gender'], ['Alfred', 'M'], ['Robert', 'M'], ['Lucille', 'F'] ].to_csv #=> "Name;Gender\nAlfred;M\nRobert;M\nLucille;F\n"
Almost always, when we generate CSV files, we want it to have appropriate headers, so a better approach might be to use an array of hashes.
[ { 'Name' => 'Alfred', 'Gender' => 'M' }, { 'Name' => 'Robert', 'Gender' => 'M' }, { 'Name' => 'Lucille', 'Gender' => 'F' } ].to_csv #=> "Gender;Name\nM;Alfred\nM;Robert\nF;Lucille\n"
Look carefully to the above output. You can see that when we use hashes we can no longer be sure of the headers' order (true for Ruby versions older than 1.9). When we are working with tabular data the headers' order can be very important, thus we can use a somewhat similar data structure:
[ [ ['Name', 'Alfred'], ['Gender', 'M'] ], [ ['Name', 'Robert'], ['Gender', 'M'] ], [ ['Name', 'Lucille'], ['Gender', 'F'] ] ].to_csv #=> "Name;Gender\nAlfred;M\nRobert;M\nLucille;F\n"
That's a lot to type... The first example was much simpler...
There is the headers option. You can use it in all the examples above to enable/disable headers from the output. Default is to show (true).
users = [{ 'Name' => 'Alfred', 'Gender' => 'M' }] users.to_csv(:headers => false)
==== Active Record Objects
When we're building our data like the previous examples we have very few options compared to what can be passed when converting an array of AR objects. Again, the easiest way:
@users = User.all File.open('path/to/file.csv', 'w') { |io| io.puts @users.to_csv }
==== Headers
You can control the order and the text of any header. You can accomplish that in various ways.
By default all attribute/method names will be sorted in alphabetical order. So imagine a person model have +name+, +age+ and +email+ as attributes, and you want to get the following output:
Name | E-mail | Age ... | ... | .. ... | ... | ..
You can tell to-csv to use a specific locale. If you don't, it uses your app current locale. It will try to translate attributes to a more friendly text by using the scope activerecord.attributes.. If the translation doesn't exist the header's text is going to be humanized.
The order of columns can be changed with the option +headers+. The way this option works is very similar to the plugins method in your Rails environment.rb file.
So, in our example above, we can say:
@users.to_csv(:headers => [:name, :email, :age])
Or, using the placeholder +all+, which is not very useful here:
@users.to_csv(:headers => [:name, :email, :all])
If you want a completely different result you could, for instance, map all users to a hash. Example:
@users.map do |user| { 'Name' => user.name, 'Age' => user.age, 'Total Comments' => user.comments.count } end.to_csv
==== Passing a Block
Sometimes you may want to change just one value out of six for example. The best way to go is to pass a block so that you don't have to repeat yourself writing five headers and it's obvious values and also loosing I18n header translations.
@users.to_csv do |row, user| row.date_of_birth = user.date_of_birth.to_s(:long) end
==== Include Relationships
If you have an AR object with many relationships and you want to include these to CSV results, you can use :include option.
Examples:
User.all.to_csv(:include => :posts)
User.all.to_csv(:include => :organization)
User.all.to_csv(:include => [:organization, :posts])
==== A More Complete Example
= link_to 'export (CSV)', users_url(:csv)
class UsersController < ApplicationController def index @users = User.most_active respond_to do |format| format.html format.csv do send_data User.csv(@users), :filename => 'users_report.csv' end end end end
class User < ActiveRecord::Base def self.csv(users) users.csv(:headers => [:id, :all], :primary_key => true, :except => :password) do |row, user| row.id = "%04d" % user.id row.created_at = I18n.l(user.created_at, :format => :default) end end end
activerecord: attributes: user: id: Code
==== Full Customization
You can always customize the output if you wish by building arrays of hashes, arrays of arrays of bidimensional arrays etc :). Or you can obviously mix anything you want and even use FasterCSV directly.
@user.to_csv { :only => [:name, :email] }, :col_sep => ','
There are other options for you to customize the output. Take a look at the to_csv method documentation.
==== Credits
Special thanks to these people for their contributions and/or ideas:
Copyright (c) 2010 Ícaro Leopoldino da Motta, released under the MIT license.
FAQs
Unknown package
We found that to-csv demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.
Research
/Security News
A malicious Go module posing as an SSH brute forcer exfiltrates stolen credentials to a Telegram bot controlled by a Russian-speaking threat actor.