csvkit 0.0.1
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/csvkit.gemspec +21 -0
- data/lib/csvkit.rb +3 -0
- data/lib/csvkit/csvkit.rb +31 -0
- data/lib/csvkit/middleware.rb +88 -0
- data/lib/csvkit/version.rb +3 -0
- data/test/lib/csvkit/version_test.rb +6 -0
- data/test/test_helper.rb +3 -0
- metadata +76 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Mathew Hartley
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# CSVKit
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'csvkit'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install csvkit
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/csvkit.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csvkit/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "csvkit"
|
8
|
+
gem.version = CSVKit::VERSION
|
9
|
+
gem.authors = ["Mathew Hartley"]
|
10
|
+
gem.email = ["matt@route66.sytes.net"]
|
11
|
+
gem.description = %q{Rack middleware to convert a <table> into a csv}
|
12
|
+
gem.summary = %q{Inspired by PDFKit, allows a html page (with a <table>) to be converted to a csv file.}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.add_dependency "nokogiri"
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
data/lib/csvkit.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
class CSVKit
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
attr_accessor :content
|
5
|
+
|
6
|
+
attr_reader :options
|
7
|
+
|
8
|
+
def initialize(body_content, options = {})
|
9
|
+
@content = body_content
|
10
|
+
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_csv
|
15
|
+
|
16
|
+
doc = Nokogiri::HTML(@content)
|
17
|
+
|
18
|
+
result = ""
|
19
|
+
|
20
|
+
doc.xpath('//table//tr').each do |row|
|
21
|
+
row.xpath('td').each do |cell|
|
22
|
+
result += '"' + cell.text.gsub("\n", ' ').gsub('"', '\"').gsub(/(\s){2,}/m, '\1').gsub(/\,\$/, '') + "\", "
|
23
|
+
end
|
24
|
+
result += "\n"
|
25
|
+
end
|
26
|
+
|
27
|
+
raise "command failed: #{invoke}" if result.to_s.strip.empty?
|
28
|
+
return result
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
class CSVKit
|
2
|
+
class Middleware
|
3
|
+
def initialize(app, options = {}, conditions = {})
|
4
|
+
@app = app
|
5
|
+
@options = options
|
6
|
+
@conditions = conditions
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
@request = Rack::Request.new(env)
|
11
|
+
@render_csv = false
|
12
|
+
|
13
|
+
set_request_to_render_as_csv(env) if render_as_csv?
|
14
|
+
status, headers, response = @app.call(env)
|
15
|
+
|
16
|
+
if rendering_csv? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
|
17
|
+
body = response.respond_to?(:body) ? response.body : response.join
|
18
|
+
body = body.join if body.is_a?(Array)
|
19
|
+
body = CSVKit.new(body, @options).to_csv
|
20
|
+
response = [body]
|
21
|
+
|
22
|
+
# Do not cache CSVs
|
23
|
+
headers.delete('ETag')
|
24
|
+
headers.delete('Cache-Control')
|
25
|
+
|
26
|
+
headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
|
27
|
+
headers["Content-Type"] = "text/csv"
|
28
|
+
end
|
29
|
+
|
30
|
+
[status, headers, response]
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Change relative paths to absolute
|
36
|
+
def translate_paths(body, env)
|
37
|
+
# Host with protocol
|
38
|
+
root = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
|
39
|
+
|
40
|
+
body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
|
41
|
+
end
|
42
|
+
|
43
|
+
def rendering_csv?
|
44
|
+
@render_csv
|
45
|
+
end
|
46
|
+
|
47
|
+
def render_as_csv?
|
48
|
+
request_path_is_csv = @request.path.match(%r{\.csv$})
|
49
|
+
|
50
|
+
if request_path_is_csv && @conditions[:only]
|
51
|
+
rules = [@conditions[:only]].flatten
|
52
|
+
rules.any? do |pattern|
|
53
|
+
if pattern.is_a?(Regexp)
|
54
|
+
@request.path =~ pattern
|
55
|
+
else
|
56
|
+
@request.path[0, pattern.length] == pattern
|
57
|
+
end
|
58
|
+
end
|
59
|
+
elsif request_path_is_csv && @conditions[:except]
|
60
|
+
rules = [@conditions[:except]].flatten
|
61
|
+
rules.map do |pattern|
|
62
|
+
if pattern.is_a?(Regexp)
|
63
|
+
return false if @request.path =~ pattern
|
64
|
+
else
|
65
|
+
return false if @request.path[0, pattern.length] == pattern
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
return true
|
70
|
+
else
|
71
|
+
request_path_is_csv
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_request_to_render_as_csv(env)
|
76
|
+
@render_csv = true
|
77
|
+
path = @request.path.sub(%r{\.csv$}, '')
|
78
|
+
%w[PATH_INFO REQUEST_URI].each { |e| env[e] = path }
|
79
|
+
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
|
80
|
+
env["Rack-Middleware-CSVKit"] = "true"
|
81
|
+
end
|
82
|
+
|
83
|
+
def concat(accepts, type)
|
84
|
+
(accepts || '').split(',').unshift(type).compact.join(',')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csvkit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mathew Hartley
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Rack middleware to convert a <table> into a csv
|
31
|
+
email:
|
32
|
+
- matt@route66.sytes.net
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- csvkit.gemspec
|
43
|
+
- lib/csvkit.rb
|
44
|
+
- lib/csvkit/csvkit.rb
|
45
|
+
- lib/csvkit/middleware.rb
|
46
|
+
- lib/csvkit/version.rb
|
47
|
+
- test/lib/csvkit/version_test.rb
|
48
|
+
- test/test_helper.rb
|
49
|
+
homepage: ''
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.24
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Inspired by PDFKit, allows a html page (with a <table>) to be converted to
|
73
|
+
a csv file.
|
74
|
+
test_files:
|
75
|
+
- test/lib/csvkit/version_test.rb
|
76
|
+
- test/test_helper.rb
|