csv_builder 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +11 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +80 -0
- data/Rakefile +12 -0
- data/lib/csv_builder.rb +64 -0
- data/rails/init.rb +3 -0
- metadata +72 -0
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
== 1.1.4 release 2009-07-27
|
2
|
+
|
3
|
+
* Merged some patches:
|
4
|
+
* Added gem spec (Jan De Poorter)
|
5
|
+
* Set the correct response headers to work with IE, made the existing ones a little more robust (Jose Fernandez)
|
6
|
+
* Created changelog, updated docs (Michael Reinsch)
|
7
|
+
|
8
|
+
== 1.0 released 2008
|
9
|
+
|
10
|
+
* The original csv_builder plugin, released by Econsultancy.com
|
11
|
+
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Econsultancy.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
= CSV Builder
|
2
|
+
|
3
|
+
The CSV Builder Rails plugin provides a simple templating system for serving
|
4
|
+
dynamically generated CSV files from your application.
|
5
|
+
|
6
|
+
|
7
|
+
== Requirements
|
8
|
+
|
9
|
+
CSV Builder requires Rails v2.1.
|
10
|
+
|
11
|
+
It also depends upon the FasterCSV gem http://fastercsv.rubyforge.org,
|
12
|
+
which you can install with
|
13
|
+
|
14
|
+
$ sudo gem install fastercsv
|
15
|
+
|
16
|
+
Encoding conversions are done with Iconv, so make sure you have it on your
|
17
|
+
development/production machine.
|
18
|
+
|
19
|
+
== Install
|
20
|
+
|
21
|
+
=== Install as gem (recommended)
|
22
|
+
|
23
|
+
Install as a gem:
|
24
|
+
|
25
|
+
$ sudo gem install mreinsch-csv_builder
|
26
|
+
|
27
|
+
Then add the gem dependency in your config:
|
28
|
+
|
29
|
+
# config/environment.rb
|
30
|
+
config.gem "mreinsch-csv_builder", :source => "http://gems.github.com", :lib => "csv_builder"
|
31
|
+
|
32
|
+
=== Install as plugin
|
33
|
+
|
34
|
+
To install as a plugin, use:
|
35
|
+
|
36
|
+
$ ./script/plugin install git://github.com/mreinsch/csv_builder.git
|
37
|
+
|
38
|
+
|
39
|
+
== Example
|
40
|
+
|
41
|
+
CSV template files are suffixed with '.csv.csvbuilder', for example
|
42
|
+
'index.csv.csvbuilder'
|
43
|
+
|
44
|
+
Add rows to your CSV file in the template by pushing arrays of columns into the
|
45
|
+
csv object.
|
46
|
+
|
47
|
+
# First row
|
48
|
+
csv << [ 'cell 1', 'cell 2' ]
|
49
|
+
# Second row
|
50
|
+
csv << [ 'another cell value', 'and another' ]
|
51
|
+
# etc...
|
52
|
+
|
53
|
+
You can set the default filename for that a browser will use for 'save as' by
|
54
|
+
setting <tt>@filename</tt> instance variable in your controller's action method
|
55
|
+
e.g.
|
56
|
+
|
57
|
+
@filename = 'report.csv'
|
58
|
+
|
59
|
+
You can set the input encoding and output encoding by setting
|
60
|
+
<tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
|
61
|
+
These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
62
|
+
|
63
|
+
@output_encoding = 'UTF-8'
|
64
|
+
|
65
|
+
You can set <tt>@csv_options</tt> instance variable to define options for
|
66
|
+
FasterCSV generator. For example:
|
67
|
+
|
68
|
+
@csv_options = { :force_quotes => true, :col_sep => ';' }
|
69
|
+
|
70
|
+
You can also attach a csv file to mail sent out by your application by
|
71
|
+
including a snippet like the following in your mailer method
|
72
|
+
|
73
|
+
attachment "text/csv" do |attachment|
|
74
|
+
attachment.body = render(:file => 'example/index.csv.csvbuilder')
|
75
|
+
attachment.filename = 'report.csv'
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
Copyright (c) 2008 Econsultancy.com and 2009 Vidmantas Kabošis, released under
|
80
|
+
the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
|
4
|
+
desc 'Generate documentation for the csv_builder plugin.'
|
5
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
6
|
+
rdoc.rdoc_dir = 'rdoc'
|
7
|
+
rdoc.title = 'CSV Builder'
|
8
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
9
|
+
rdoc.rdoc_files.include('README.rdoc')
|
10
|
+
rdoc.rdoc_files.include('CHANGELOG.rdoc')
|
11
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
12
|
+
end
|
data/lib/csv_builder.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'fastercsv'
|
2
|
+
require 'iconv'
|
3
|
+
|
4
|
+
module ActionView # :nodoc:
|
5
|
+
module TemplateHandlers
|
6
|
+
# Template handler for csv templates
|
7
|
+
#
|
8
|
+
# Add rows to your CSV file in the template by pushing arrays of columns into csv
|
9
|
+
#
|
10
|
+
# # First row
|
11
|
+
# csv << [ 'cell 1', 'cell 2' ]
|
12
|
+
# # Second row
|
13
|
+
# csv << [ 'another cell value', 'and another' ]
|
14
|
+
# # etc...
|
15
|
+
#
|
16
|
+
# You can set the default filename for that a browser will use for 'save as' by
|
17
|
+
# setting <tt>@filename</tt> instance variable in your controller's action method
|
18
|
+
# e.g.
|
19
|
+
#
|
20
|
+
# @filename = 'report.csv'
|
21
|
+
#
|
22
|
+
# You can also set the input encoding and output encoding by setting
|
23
|
+
# <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
|
24
|
+
# These default to 'UTF-8' and 'LATIN1' respectively. e.g.
|
25
|
+
#
|
26
|
+
# @output_encoding = 'UTF-8'
|
27
|
+
class CsvBuilder < TemplateHandler
|
28
|
+
include Compilable
|
29
|
+
|
30
|
+
def self.line_offset
|
31
|
+
9
|
32
|
+
end
|
33
|
+
|
34
|
+
def compile(template)
|
35
|
+
<<-EOV
|
36
|
+
begin
|
37
|
+
unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
|
38
|
+
@filename ||= "\#{controller.action_name}.csv"
|
39
|
+
if controller.request.env['HTTP_USER_AGENT'] =~ /msie/i
|
40
|
+
controller.response.headers['Pragma'] = 'public'
|
41
|
+
controller.response.headers["Content-type"] = "text/plain"
|
42
|
+
controller.response.headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
|
43
|
+
controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}"
|
44
|
+
controller.response.headers['Expires'] = "0"
|
45
|
+
else
|
46
|
+
controller.response.headers["Content-Type"] ||= 'text/csv'
|
47
|
+
controller.response.headers["Content-Disposition"] = "attachment; filename=\#{@filename}"
|
48
|
+
controller.response.headers["Content-Transfer-Encoding"] = "binary"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
FasterCSV.generate do |faster_csv|
|
53
|
+
csv = TransliteratingFilter.new(faster_csv)
|
54
|
+
#{template.source}
|
55
|
+
end
|
56
|
+
rescue Exception => e
|
57
|
+
RAILS_DEFAULT_LOGGER.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
|
58
|
+
raise e
|
59
|
+
end
|
60
|
+
EOV
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/rails/init.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Econsultancy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-07-27 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: fastercsv
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: CSV template Rails plugin
|
26
|
+
email: code@econsultancy.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- CHANGELOG.rdoc
|
34
|
+
- MIT-LICENSE
|
35
|
+
files:
|
36
|
+
- MIT-LICENSE
|
37
|
+
- README.rdoc
|
38
|
+
- CHANGELOG.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- rails/init.rb
|
41
|
+
- lib/csv_builder.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://github.com/mreinsch/csv_builder
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --main
|
49
|
+
- README.rdoc
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.5
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: CSV template Rails plugin
|
71
|
+
test_files: []
|
72
|
+
|