csv2rest 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +5 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/csv2rest.gemspec +30 -0
- data/exe/csv2rest +4 -0
- data/lib/csv2rest.rb +66 -0
- data/lib/csv2rest/cli.rb +21 -0
- data/lib/csv2rest/version.rb +3 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cb3f250bd3456d0ffab899fe329a39c894747b03
|
4
|
+
data.tar.gz: 2c0a09c4d17760bee056c513b2441d1f5032756e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d364b250d0b056c8e16336e3032ff909047b16c8f40ce26d3d34fde8138937817991887560bdfb23a7029e4f979b30e740bef31c2193930a8c26780bf5db5bbc
|
7
|
+
data.tar.gz: 05da217941011015ec5dd33b080e3d69dc7734407505a8121cccc131af5e69ff10d2a7fb0f54058cdd217b243abbfd4ea929b9163ba29d2269249aae0ed6183c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 pikesley
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
[![Build Status](http://img.shields.io/travis/theodi/csv2rest.svg?style=flat-square)](https://travis-ci.org/theodi/csv2rest)
|
2
|
+
[![Dependency Status](http://img.shields.io/gemnasium/theodi/csv2rest.svg?style=flat-square)](https://gemnasium.com/theodi/csv2rest)
|
3
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/theodi/csv2rest.svg?style=flat-square)](https://codeclimate.com/github/theodi/csv2rest)
|
4
|
+
[![Gem Version](http://img.shields.io/gem/v/csv2rest.svg?style=flat-square)](https://rubygems.org/gems/csv2rest)
|
5
|
+
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://theodi.mit-license.org)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "csv2rest"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/csv2rest.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csv2rest/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csv2rest"
|
8
|
+
spec.version = Csv2rest::VERSION
|
9
|
+
spec.authors = ["pikesley", "floppy"]
|
10
|
+
spec.email = ["ops@theodi.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Turn CSVs into JSON}
|
13
|
+
spec.description = %q{Turn CSVs into JSON}
|
14
|
+
spec.homepage = "http://github.com/theodi/csv2rest"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "csv2json"
|
23
|
+
spec.add_dependency "thor", "~> 0.19"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
|
30
|
+
end
|
data/exe/csv2rest
ADDED
data/lib/csv2rest.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "csv2rest/version"
|
2
|
+
require 'csvlint/csvw/csv2json/csv2json'
|
3
|
+
require 'thor'
|
4
|
+
require 'json'
|
5
|
+
require 'active_support/inflector'
|
6
|
+
require 'uri'
|
7
|
+
|
8
|
+
module Csv2rest
|
9
|
+
def self.generate csv, schema
|
10
|
+
base_path = File.dirname(csv)
|
11
|
+
t = Csvlint::Csvw::Csv2Json::Csv2Json.new( csv, {}, schema, { :validate => true } )
|
12
|
+
json = JSON.parse(t.result)
|
13
|
+
|
14
|
+
h = {}
|
15
|
+
|
16
|
+
# Create individual resources
|
17
|
+
json["tables"].each do |table|
|
18
|
+
table["row"].each do |object|
|
19
|
+
obj = object["describes"][0]
|
20
|
+
path = obj["@id"].gsub("#{base_path}/","") # NASTINESS - replace with base URL somehow
|
21
|
+
resource_name = obj["@type"].gsub("#{base_path}/","") # NASTINESS - replace with base URL somehow
|
22
|
+
# parameterize path
|
23
|
+
path = path.split('/').map{|x| URI.decode(x).parameterize}.join('/')
|
24
|
+
# Dump metadata we don't want in the JSON representation of the object
|
25
|
+
obj.delete("@id")
|
26
|
+
obj.delete("@type")
|
27
|
+
# Store object
|
28
|
+
h[path] = obj
|
29
|
+
# Add to object list
|
30
|
+
h["#{resource_name}"] ||= []
|
31
|
+
h["#{resource_name}"] << {
|
32
|
+
"url" => path
|
33
|
+
}
|
34
|
+
# Add resource to root
|
35
|
+
h[""] ||= []
|
36
|
+
h[""] << {
|
37
|
+
"resource" => resource_name,
|
38
|
+
"url" => "#{resource_name}"
|
39
|
+
}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Easier than checking for duplication as we go
|
44
|
+
h[""].uniq!
|
45
|
+
|
46
|
+
# Done
|
47
|
+
h
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.write_json files, options
|
51
|
+
files.each do |name, content|
|
52
|
+
# Index
|
53
|
+
name = "index" if name == ""
|
54
|
+
# Filename
|
55
|
+
filename = name + ".json"
|
56
|
+
FileUtils.mkdir_p options[:output_dir]
|
57
|
+
Dir.chdir(options[:output_dir]) do
|
58
|
+
# Create directories
|
59
|
+
FileUtils.mkdir_p File.dirname(filename)
|
60
|
+
# Write
|
61
|
+
File.write(filename, JSON.pretty_generate(content))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/lib/csv2rest/cli.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'csv2rest'
|
2
|
+
require 'pp'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
module Csv2rest
|
6
|
+
class CLI < Thor
|
7
|
+
desc 'version', 'Print csv2rest version'
|
8
|
+
def version
|
9
|
+
puts "csv2rest version #{VERSION}"
|
10
|
+
end
|
11
|
+
|
12
|
+
desc 'generate', 'generate JSON from CSVs'
|
13
|
+
method_option :output_dir,
|
14
|
+
aliases: '-o',
|
15
|
+
description: 'Where to output files'
|
16
|
+
def generate csv, json
|
17
|
+
files = Csv2rest.generate "file:"+csv, Csvlint::Schema.load_from_json(json)
|
18
|
+
Csv2rest.write_json files, options
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv2rest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pikesley
|
8
|
+
- floppy
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-06-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: csv2json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: thor
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0.19'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0.19'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: bundler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.7'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.7'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rake
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '10.0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '10.0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rspec
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '3.0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '3.0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: pry
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
description: Turn CSVs into JSON
|
99
|
+
email:
|
100
|
+
- ops@theodi.org
|
101
|
+
executables:
|
102
|
+
- csv2rest
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- ".ruby-version"
|
109
|
+
- ".travis.yml"
|
110
|
+
- Gemfile
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- bin/console
|
115
|
+
- bin/setup
|
116
|
+
- csv2rest.gemspec
|
117
|
+
- exe/csv2rest
|
118
|
+
- lib/csv2rest.rb
|
119
|
+
- lib/csv2rest/cli.rb
|
120
|
+
- lib/csv2rest/version.rb
|
121
|
+
homepage: http://github.com/theodi/csv2rest
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata: {}
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 2.5.1
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: Turn CSVs into JSON
|
145
|
+
test_files: []
|