mreinsch-csv_builder 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,10 @@
1
+ == 1.1.3 release 2009-06-27
2
+
3
+ * Merged some patches, created this changelog (Michael Reinsch)
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
+
7
+ == 1.0 released 2008
8
+
9
+ * The original csv_builder plugin, released by Econsultancy.com
10
+
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,60 @@
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
+ == Example
20
+
21
+ CSV template files are suffixed with '.csv.csvbuilder', for example
22
+ 'index.csv.csvbuilder'
23
+
24
+ Add rows to your CSV file in the template by pushing arrays of columns into the
25
+ csv object.
26
+
27
+ # First row
28
+ csv << [ 'cell 1', 'cell 2' ]
29
+ # Second row
30
+ csv << [ 'another cell value', 'and another' ]
31
+ # etc...
32
+
33
+ You can set the default filename for that a browser will use for 'save as' by
34
+ setting <tt>@filename</tt> instance variable in your controller's action method
35
+ e.g.
36
+
37
+ @filename = 'report.csv'
38
+
39
+ You can set the input encoding and output encoding by setting
40
+ <tt>@input_encoding</tt> and <tt>@output_encoding</tt> instance variables.
41
+ These default to 'UTF-8' and 'LATIN1' respectively. e.g.
42
+
43
+ @output_encoding = 'UTF-8'
44
+
45
+ You can set <tt>@csv_options</tt> instance variable to define options for
46
+ FasterCSV generator. For example:
47
+
48
+ @csv_options = { :force_quotes => true, :col_sep => ';' }
49
+
50
+ You can also attach a csv file to mail sent out by your application by
51
+ including a snippet like the following in your mailer method
52
+
53
+ attachment "text/csv" do |attachment|
54
+ attachment.body = render(:file => 'example/index.csv.csvbuilder')
55
+ attachment.filename = 'report.csv'
56
+ end
57
+
58
+
59
+ Copyright (c) 2008 Econsultancy.com and 2009 Vidmantas Kabošis, released under
60
+ 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
@@ -0,0 +1,86 @@
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
+
28
+ class CsvBuilder < TemplateHandler
29
+
30
+ include Compilable
31
+
32
+ def self.line_offset
33
+ 9
34
+ end
35
+
36
+ def compile(template)
37
+ <<-EOV
38
+ begin
39
+
40
+ unless defined?(ActionMailer) && defined?(ActionMailer::Base) && controller.is_a?(ActionMailer::Base)
41
+ @filename ||= "\#{controller.action_name}.csv"
42
+ if controller.request.env['HTTP_USER_AGENT'] =~ /msie/i
43
+ controller.response.headers['Pragma'] = 'public'
44
+ controller.response.headers["Content-type"] = "text/plain"
45
+ controller.response.headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'
46
+ controller.response.headers['Content-Disposition'] = "attachment; filename=\#{@filename}"
47
+ controller.response.headers['Expires'] = "0"
48
+ else
49
+ controller.response.headers["Content-Type"] ||= 'text/csv'
50
+ controller.response.headers["Content-Disposition"] = "attachment; filename=\#{@filename}"
51
+ controller.response.headers["Content-Transfer-Encoding"] = "binary"
52
+ end
53
+ end
54
+
55
+ result = FasterCSV.generate(@csv_options || {}) do |csv|
56
+ #{template.source}
57
+ end
58
+
59
+ # Transliterate into the required encoding if necessary
60
+ # TODO: make defaults configurable
61
+ @input_encoding ||= 'UTF-8'
62
+ @output_encoding ||= 'LATIN1'
63
+
64
+ if @input_encoding == @output_encoding
65
+ result
66
+ else
67
+ # TODO: do some checking to make sure iconv works correctly in
68
+ # current environment. See ActiveSupport::Inflector#transliterate
69
+ # definition for details
70
+ #
71
+ # Not using the more standard //IGNORE//TRANLIST because it raises
72
+ # Iconv::IllegalSequence for some inputs
73
+ c = Iconv.new("\#{@output_encoding}//TRANSLIT//IGNORE", @input_encoding)
74
+ c.iconv(result)
75
+ end
76
+
77
+ rescue Exception => e
78
+ RAILS_DEFAULT_LOGGER.warn("Exception \#{e} \#{e.message} with class \#{e.class.name} thrown when rendering CSV")
79
+ raise e
80
+ end
81
+ EOV
82
+ end
83
+
84
+ end
85
+ end
86
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'csv_builder'
2
+
3
+ ActionView::Template.register_template_handler 'csvbuilder', ActionView::TemplateHandlers::CsvBuilder
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mreinsch-csv_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Econsultancy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-27 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: CSV template Rails plugin
17
+ email: code@econsultancy.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - CHANGELOG.rdoc
25
+ - MIT-LICENSE
26
+ files:
27
+ - MIT-LICENSE
28
+ - README.rdoc
29
+ - CHANGELOG.rdoc
30
+ - Rakefile
31
+ - rails/init.rb
32
+ - lib/csv_builder.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/mreinsch/csv_builder
35
+ licenses:
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --main
39
+ - README.rdoc
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: CSV template Rails plugin
61
+ test_files: []
62
+