Class: Greentable::Export
- Inherits:
-
Object
- Object
- Greentable::Export
- Defined in:
- lib/greentable/export.rb
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(app, *args, &block) ⇒ Export
constructor
A new instance of Export.
Constructor Details
#initialize(app, *args, &block) ⇒ Export
Returns a new instance of Export.
3 4 5 6 |
# File 'lib/greentable/export.rb', line 3 def initialize(app, *args, &block) @app = app @env = nil end |
Instance Method Details
#_call(env) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/greentable/export.rb', line 12 def _call(env) @status, @headers, @response = @app.call(env) rescue nil @ret = nil if env['QUERY_STRING'] =~ /greentable_export=([csv|rtf|xml]+)/i request = Rack::Request.new(env) greentable_id = request.params['greentable_id'] if greentable_id format = $1 body = @response.respond_to?(:body) ? @response.body : @response.join doc = Hpricot(body.to_s) @ret= "" (doc/"##{greentable_id}//tr").each do |tr| row = [] col = 0 (tr/"/th | /td").each do |x| colspan = [(x.attributes['colspan'] || 1).to_i, 1].max row[col] = x.to_plain_text col += colspan end CSV.generate_row(row, row.length, @ret) end filename = request.params['greentable_export_filename'] || "export" @headers["Content-Length"] = @ret.length.to_s @headers["Content-Type"] = "text/csv" @headers["Content-Disposition"] = "attachment; filename=#{filename}.#{format}" @headers.delete('ETag') @headers.delete('Cache-Control') end end [@status, @headers, self] end |
#call(env) ⇒ Object
8 9 10 |
# File 'lib/greentable/export.rb', line 8 def call(env) dup._call(env) end |
#each(&block) ⇒ Object
47 48 49 50 |
# File 'lib/greentable/export.rb', line 47 def each(&block) block.call(@ret) if @ret @response.each(&block) end |