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, , @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"
["Content-Length"] = @ret.length.to_s
["Content-Type"] = "text/csv"
["Content-Disposition"] = "attachment; filename=#{filename}.#{format}"
.delete('ETag')
.delete('Cache-Control')
end
end
[@status, , self]
end
|