ctoD 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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/bin/ctoD +5 -0
- data/ctoD.gemspec +30 -0
- data/lib/ctoD.rb +7 -0
- data/lib/ctoD/cli.rb +26 -0
- data/lib/ctoD/db.rb +81 -0
- data/lib/ctoD/version.rb +3 -0
- data/spec/ctoD_spec.rb +11 -0
- data/spec/spec_helper.rb +2 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 13d9a4dc741cabfcadcfbc989d7237cb178a3b9d
|
4
|
+
data.tar.gz: abadab6fbb41f6ce0b8427961280d7eb625c6b81
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2b48cbd722d1cfe665ebe585f870859472154e06fcfc306babe0cc144c20631c5f00e7bebce32c5c8a0104313894f6c40864adfd33731e69b8fb8212cef2010a
|
7
|
+
data.tar.gz: b2b02fa54ddc8b970bac620e2d43341f1e8ec1e73f09841cb4188f0fb064588d6463e928d53911c22f75d9389722019012350b0d12e73bb090a224002d61fa55
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 kyoendo
|
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,42 @@
|
|
1
|
+
# CtoD
|
2
|
+
|
3
|
+
CtoD is a tool for exporting a CSV data to database.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ctoD'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ctoD
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
It comes with `ctoD` command.
|
22
|
+
|
23
|
+
# Display help.
|
24
|
+
% ctoD
|
25
|
+
Commands:
|
26
|
+
ctoD create_table CSV DATABASE # Create a database table for CSV
|
27
|
+
ctoD export CSV DATABASE # Export CSV data to a DATABASE
|
28
|
+
ctoD help [COMMAND] # Describe available commands or one specific command
|
29
|
+
|
30
|
+
# Export movies.csv data to a table named 'movies' at postgres movies database.
|
31
|
+
% ctoD export movies.csv postgres://localhost/movies
|
32
|
+
|
33
|
+
# Export movies.csv data to movies.sqlite3 database file.
|
34
|
+
% ctoD export movies.csv sqlite3://localhost/${HOME}/.db/movies.sqlite3
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/ctoD
ADDED
data/ctoD.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 'ctoD/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ctoD"
|
8
|
+
spec.version = CtoD::VERSION
|
9
|
+
spec.authors = ["kyoendo"]
|
10
|
+
spec.email = ["postagie@gmail.com"]
|
11
|
+
spec.description = %q{Export CSV data to database}
|
12
|
+
spec.summary = %q{Export CSV data to database}
|
13
|
+
spec.homepage = "https://github.com/melborne/ctoD"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.required_ruby_version = '>= 2.0.0'
|
21
|
+
|
22
|
+
spec.add_dependency "thor"
|
23
|
+
spec.add_dependency "activerecord", "~> 3.2"
|
24
|
+
spec.add_development_dependency "mysql2"
|
25
|
+
spec.add_development_dependency "pg"
|
26
|
+
spec.add_development_dependency "sqlite3"
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
spec.add_development_dependency "rspec"
|
30
|
+
end
|
data/lib/ctoD.rb
ADDED
data/lib/ctoD/cli.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'thor'
|
2
|
+
|
3
|
+
module CtoD
|
4
|
+
class CLI < Thor
|
5
|
+
desc "export CSV DATABASE", "Export CSV data to DATABASE"
|
6
|
+
option :string_size, aliases:"-s", default:100
|
7
|
+
def export(from, to)
|
8
|
+
db = create_table(from, to)
|
9
|
+
db.export
|
10
|
+
puts "CSV data exported successfully."
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "create_table CSV DATABASE", "Create a database table for CSV"
|
14
|
+
option :string_size, aliases:"-s", default:100
|
15
|
+
def create_table(from, to)
|
16
|
+
db = DB.new(from, to, string_size: options[:string_size])
|
17
|
+
if db.table_exists?
|
18
|
+
puts "Table '#{db.table_name}' exsist."
|
19
|
+
else
|
20
|
+
db.create_table
|
21
|
+
puts "Table '#{db.table_name}' created at #{db.uri}."
|
22
|
+
end
|
23
|
+
db
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/ctoD/db.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'uri'
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
module CtoD
|
6
|
+
class DB
|
7
|
+
AR = ActiveRecord::Base
|
8
|
+
attr_accessor :string_size
|
9
|
+
attr_reader :table_name, :class_name, :uri
|
10
|
+
def initialize(csv, uri, string_size:100)
|
11
|
+
@table_name = File.basename(csv, '.csv').intern
|
12
|
+
@class_name = @table_name[0..-2].capitalize
|
13
|
+
@csv = CSV.table(csv)
|
14
|
+
@string_size = string_size
|
15
|
+
@uri = DB.connect(uri)
|
16
|
+
end
|
17
|
+
|
18
|
+
def table_exists?
|
19
|
+
AR.connection.table_exists?(@table_name)
|
20
|
+
rescue => e
|
21
|
+
puts e
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_table
|
25
|
+
conn = AR.connection
|
26
|
+
conn.create_table(@table_name) do |t|
|
27
|
+
@csv.headers.zip(column_types).each do |name, type|
|
28
|
+
t.column name, type
|
29
|
+
end
|
30
|
+
t.timestamps
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def export
|
35
|
+
self.class.const_set(@class_name, Class.new(AR))
|
36
|
+
self.class.const_get(@class_name).create! @csv.map(&:to_hash)
|
37
|
+
rescue => e
|
38
|
+
puts "Something go wrong at export: #{e}"
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.connect(uri)
|
42
|
+
uri = URI.parse(uri)
|
43
|
+
uri.scheme = 'postgresql' if uri.scheme=='postgres'
|
44
|
+
settings = {
|
45
|
+
adapter: uri.scheme,
|
46
|
+
host: uri.host,
|
47
|
+
username: uri.user,
|
48
|
+
password: uri.password,
|
49
|
+
database: uri.path[1..-1],
|
50
|
+
encoding: 'utf8'
|
51
|
+
}
|
52
|
+
AR.establish_connection(settings)
|
53
|
+
uri
|
54
|
+
rescue => e
|
55
|
+
puts "Something go wrong at connect: #{e}"
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def column_types
|
60
|
+
is_date = /^\s*\d{1,4}(\-|\/)?\d{1,2}(\-|\/)?\d{1,2}\s*$/
|
61
|
+
@csv.first.to_hash.inject([]) do |mem, (k, v)|
|
62
|
+
mem << begin
|
63
|
+
case v
|
64
|
+
when 'true', 'false'
|
65
|
+
:boolean
|
66
|
+
when is_date
|
67
|
+
:date
|
68
|
+
when String, Symbol
|
69
|
+
@csv[k].compact.max_by(&:size).size > string_size ? :text : :string
|
70
|
+
when Fixnum, Float
|
71
|
+
@csv[k].any? { |e| e.is_a? Float } ? :float : :integer
|
72
|
+
when NilClass
|
73
|
+
:string
|
74
|
+
else
|
75
|
+
v.class.name.downcase.intern
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/ctoD/version.rb
ADDED
data/spec/ctoD_spec.rb
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ctoD
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kyoendo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mysql2
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pg
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Export CSV data to database
|
126
|
+
email:
|
127
|
+
- postagie@gmail.com
|
128
|
+
executables:
|
129
|
+
- ctoD
|
130
|
+
extensions: []
|
131
|
+
extra_rdoc_files: []
|
132
|
+
files:
|
133
|
+
- .gitignore
|
134
|
+
- .rspec
|
135
|
+
- .travis.yml
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/ctoD
|
141
|
+
- ctoD.gemspec
|
142
|
+
- lib/ctoD.rb
|
143
|
+
- lib/ctoD/cli.rb
|
144
|
+
- lib/ctoD/db.rb
|
145
|
+
- lib/ctoD/version.rb
|
146
|
+
- spec/ctoD_spec.rb
|
147
|
+
- spec/spec_helper.rb
|
148
|
+
homepage: https://github.com/melborne/ctoD
|
149
|
+
licenses:
|
150
|
+
- MIT
|
151
|
+
metadata: {}
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
require_paths:
|
155
|
+
- lib
|
156
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - '>='
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: 2.0.0
|
161
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - '>='
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
requirements: []
|
167
|
+
rubyforge_project:
|
168
|
+
rubygems_version: 2.1.9
|
169
|
+
signing_key:
|
170
|
+
specification_version: 4
|
171
|
+
summary: Export CSV data to database
|
172
|
+
test_files:
|
173
|
+
- spec/ctoD_spec.rb
|
174
|
+
- spec/spec_helper.rb
|
175
|
+
has_rdoc:
|