Xporter
ruby gem for DSL creating streaming CSV exports.
Installation
Add this line to your application's Gemfile:
gem 'xporter'
And then execute:
$ bundle
Or install it yourself as:
$ gem install xporter
Usage
In most cases, you'll use this library in a rails app, to organize different exporters and optionally use Live Streaming from the controller to browser to begin streaming results straight from the database to the client.
Define Exporter
To define an exporter, simply place a file in the app/exporters
directory in your rails app (restarting app server if creating directory for the first time).
# app/exporters/admin_exporter.rb
class AdminExporter < Xporter::Exporter
model User
decorates AdminDecorator
column(:name)
column(:email)
column(:dynamic) { |record| record.object_id }
end
Generate CSV String
With an exporter defined, you'll want to convert a collection of objects into a CSV string.
exporter = AdminExporter.new
exporter.generate(User.all)
# => "Name,Email,Dynamic\nJustin,justin@madebylotus.com,12345511\n"
If provided an ActiveRecord::Relation
, we'll fetch records lazily in batches from ActiveRecord.
Stream CSV to Browser
With an exporter defined, you'll want to convert a collection of objects and stream to the browser immediately.
class AdministratorsController < ApplicationController
include Xporter::FileStreamer
def index
@users = User.all
respond_to do |format|
format.html
format.csv do
stream_file "administrators-export-#{ SecureRandom.uuid }".parameterize, 'csv' do |stream|
AdminExporter.stream(@users, stream)
end
end
end
end
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/xporter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Xporter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.