json2csv 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 191926f29082349e752f721279ded4d00913f675
4
+ data.tar.gz: 66247e0ecc895a6b5c727987917ff2c9143e8bc7
5
+ SHA512:
6
+ metadata.gz: 37e2b307419c07c3b23d2936e776d8b44184f54805af39adca4ed95e19aee1d0d9d5bf121d775ef7d220a1e0cfb6a1fac6f1934bdb1a0811c20eafdc9340574e
7
+ data.tar.gz: 7611e767213416401db6c7f38ed25340641d24332004d197c0455700a612da269b59095a349c51e82fc4950cfe896fa15241c381dea566d5ca61cfbbca0a148f
data/.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ $:.push File.expand_path("../lib", __FILE__)
6
+
7
+ require 'json2csv/version.rb'
8
+
9
+ Gem::Specification.new do |s|
10
+ s.name = 'json2csv'
11
+ s.version = Json2Csv::VERSION
12
+ s.summary = 'Simple tool for converting json2csv'
13
+ s.description = 'Quick and Dirty - No time to waste time'
14
+ s.authors = [
15
+ 'Tomas Korcak'
16
+ ]
17
+ s.email = 'korczis@gmail.com'
18
+
19
+ s.homepage = 'https://github.com/korczis/json2csv'
20
+ s.license = 'MIT'
21
+ s.require_paths = ['lib']
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
25
+
26
+ s.add_dependency 'gli', '~> 2.11', '>= 2.11.0'
27
+ s.add_dependency 'json_pure', '~> 1.8.1'
28
+ s.add_dependency 'multi_json', '~> 1.10.0'
29
+ s.add_dependency 'rake', '~> 10.3', '>= 10.3.2'
30
+ s.add_dependency 'terminal-table', '~> 1.4', '>= 1.4.5'
31
+
32
+ s.add_development_dependency 'coveralls', '~> 0.7', '>= 0.7.0r'
33
+ s.add_development_dependency 'rspec', '~> 3.0', '>= 3.0.0'
34
+ s.add_development_dependency 'rubocop', '~> 0.24', '>= 0.24.0'
35
+ s.add_development_dependency 'simplecov', '~> 0.8', '>= 0.8.2'
36
+ end
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.box
2
+ .idea/
3
+ .vagrant/
4
+
5
+ Gemfile.lock
6
+
7
+ coverage/
8
+ packer_cache/
9
+ pkg/
10
+
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - ruby-head
6
+ - jruby-19mode
7
+
8
+ script: rake ci
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json2csv.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Tomas Korcak
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # json2csv
2
+
3
+ Simple tool for converting JSON to CSV
4
+
5
+ ## Status
6
+
7
+ [![Build Status](https://travis-ci.org/korczis/json2csv.svg)](https://travis-ci.org/korczis/json2csv)
8
+ [![Coverage Status](https://coveralls.io/repos/korczis/json2csv/badge.png)](https://coveralls.io/r/korczis/json2csv)
9
+
10
+ ## Getting started
11
+
12
+ ```
13
+ gem install json2csv
14
+ ```
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems'
4
+
5
+ require 'bundler/setup'
6
+ require 'bundler/gem_tasks'
7
+
8
+ require 'coveralls/rake/task'
9
+
10
+ require 'rspec/core/rake_task'
11
+
12
+ Coveralls::RakeTask.new
13
+
14
+ RSpec::Core::RakeTask.new(:test)
15
+
16
+ desc 'Run continuous integration test'
17
+ task :ci do
18
+ Rake::Task['test:unit'].invoke
19
+ # unless ENV['TRAVIS'] == 'true' && ENV['TRAVIS_SECURE_ENV_VARS'] == 'false'
20
+ # Rake::Task['test:integration'].invoke
21
+ # end
22
+ Rake::Task['test:cop'].invoke if RUBY_VERSION.start_with?('2.2') == false
23
+ Rake::Task['coveralls:push'].invoke
24
+ end
25
+
26
+ desc 'Run Rubocop'
27
+ task :cop do
28
+ exec 'rubocop lib/'
29
+ end
30
+
31
+ namespace :test do
32
+ desc 'Run unit tests'
33
+ RSpec::Core::RakeTask.new(:unit) do |t|
34
+ t.pattern = 'spec/unit/**/*.rb'
35
+ end
36
+
37
+ desc "Run coding style tests"
38
+ RSpec::Core::RakeTask.new(:cop) do |t|
39
+ Rake::Task['cop'].invoke
40
+ end
41
+
42
+ task :all => [:unit, :cop]
43
+ end
44
+
45
+ desc 'Get all tasks'
46
+ task :tasklist do
47
+ puts Rake.application.tasks
48
+ end
49
+
50
+ task :usage do
51
+ puts 'No rake task specified, use rake -T to list them'
52
+ # puts "No rake task specified so listing them ..."
53
+ # Rake.application['tasklist'].invoke
54
+ end
55
+ task :default => [:usage]
56
+
57
+ if $0 == __FILE__
58
+ Rake.application['usage'].invoke
59
+ end
data/bin/json2csv ADDED
@@ -0,0 +1,6 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require_relative '../lib/json2csv/lib'
4
+ require_relative '../lib/json2csv/version'
5
+
6
+ require_relative '../lib/json2csv/cli/cli'
data/data/sample.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "12345": {
3
+ "Firstname": "Joe",
4
+ "Lastname": "Doe",
5
+ "Address": {
6
+ "Street": "#123 Happy Street",
7
+ "City": "San Francisco"
8
+ }
9
+ }
10
+ }
data/lib/json2csv.rb ADDED
@@ -0,0 +1,3 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'json2csv/lib'
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+
3
+ require 'gli'
4
+ require 'pathname'
5
+ require 'pp'
6
+
7
+ require_relative 'shared'
8
+ require_relative '../version'
9
+
10
+ def launch(argv = ARGV)
11
+ run(argv)
12
+ end
13
+
14
+ include GLI::App
15
+
16
+ program_desc "json2csv #{Json2Csv::VERSION}"
17
+
18
+ module Json2Csv
19
+ # Apollon CLI
20
+ module Cli
21
+ # CLI Application
22
+ class App
23
+ extend Json2Csv::Cli::Shared
24
+
25
+ cmds = File.absolute_path(File.join(File.dirname(__FILE__), 'cmd'))
26
+ Dir.glob(cmds + '/*.rb').each do |file|
27
+ require file
28
+ end
29
+
30
+ program_desc 'Apollon CLI'
31
+
32
+ def main(argv = ARGV)
33
+ launch(argv)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ launch
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'pathname'
4
+
5
+ require_relative 'shared'
6
+
7
+ require_relative 'app'
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ require 'gli'
4
+
5
+ include GLI::App
6
+
7
+ require_relative '../shared'
8
+ require_relative '../convert/convert'
9
+
10
+ desc 'Convert json file'
11
+ command :convert do |c|
12
+ c.action do |_global_options, _options, args|
13
+ fail ArgumentError, 'No file to convert specified' if args.empty?
14
+
15
+ Json2Csv::Convert.convert(args)
16
+ end
17
+ end
18
+
19
+ # default_command :convert
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'gli'
4
+
5
+ include GLI::App
6
+
7
+ require_relative '../../version'
8
+
9
+ require_relative '../shared'
10
+
11
+ desc 'Print version info'
12
+ command :version do |c|
13
+ c.action do |_global_options, _options, _args|
14
+ pp Json2Csv::VERSION
15
+ end
16
+ end
@@ -0,0 +1,81 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'csv'
4
+ require 'multi_json'
5
+ require 'pathname'
6
+ require 'pp'
7
+
8
+ require_relative '../../version'
9
+
10
+ module Json2Csv
11
+ # Apollon bootstrap module
12
+ module Convert
13
+ class << self
14
+ def convert(paths)
15
+ paths = [paths] unless paths.is_a?(Array)
16
+ paths.each do |path|
17
+ puts "Converting #{path}"
18
+
19
+ json = load_file(path)
20
+
21
+ process(json, "#{path}.csv")
22
+ end
23
+ end
24
+
25
+ def load_file(path)
26
+ # Load input file
27
+ raw = IO.read(path)
28
+
29
+ # Try to parse json from loaded data
30
+ begin
31
+ return MultiJson.load(raw)
32
+ rescue Exception => e # rubocop:disable RescueException
33
+ log_exception(e)
34
+ end
35
+ nil
36
+ end
37
+
38
+ def log_exception(e)
39
+ puts 'Invalid json, see error.txt'
40
+ File.open('error.txt', 'wt') { |f| f.write(e.to_s) }
41
+ end
42
+
43
+ def process(json, out_path = 'out.txt')
44
+ # json = json[json.keys.first]
45
+
46
+ keys = json.keys
47
+
48
+ first = true # Flag indicating if we already written the "header"
49
+
50
+ # Open the CSV for write
51
+ CSV.open(out_path, 'wt') do |csv|
52
+ # Take each story - json['stories'][<ID_HERE>]
53
+ keys.each do |key|
54
+ obj = json[key]
55
+
56
+ # Write header if needed
57
+ csv << obj.keys && first = false if first
58
+
59
+ # Write row to output CSV
60
+ csv << process_row(obj)
61
+ end
62
+ end
63
+ end
64
+
65
+ def process_row(obj)
66
+ # Create empty array containing all values from this object/row
67
+ obj.keys.map do |subkey|
68
+ # Assing value/attribute to temp variable
69
+ tmp = obj[subkey]
70
+
71
+ # Make temp variable empty string if null
72
+ tmp = '' if tmp.nil?
73
+
74
+ # Remove all new lines - replace them with empty string
75
+ tmp = tmp.gsub(/\n/, '') if tmp.is_a?(String)
76
+ tmp
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'gli'
4
+
5
+ module Json2Csv
6
+ module Cli
7
+ # Shared CLI Stuff
8
+ module Shared
9
+ extend GLI::App
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'pathname'
4
+
5
+ base = Pathname(__FILE__).dirname.expand_path
6
+
7
+ res = []
8
+
9
+ Dir.glob(base + '*.rb').each do |file|
10
+ loaded_file = require file
11
+ res << {
12
+ file: file,
13
+ loaded: loaded_file
14
+ }
15
+ end
16
+
17
+ res
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ # Apollon module
4
+ module Json2Csv
5
+ VERSION = '0.0.1'
6
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'simplecov'
4
+ require 'rspec'
5
+ require 'coveralls'
6
+ require 'pathname'
7
+
8
+ Coveralls.wear_merged!
9
+
10
+ # Automagically include all helpers/*_helper.rb
11
+
12
+ base = Pathname(__FILE__).dirname.expand_path
13
+ Dir.glob(base + 'helpers/*_helper.rb').each do |file|
14
+ require file
15
+ end
16
+
17
+ RSpec.configure do |config|
18
+ config.filter_run_excluding :broken => true
19
+
20
+ config.before(:all) do
21
+ # TODO: Fully setup global environment
22
+ end
23
+
24
+ config.after(:all) do
25
+ # TODO: Fully setup global environment
26
+ end
27
+
28
+ config.before(:suite) do
29
+ # TODO: Setup test project
30
+ end
31
+
32
+ config.after(:suite) do
33
+ # TODO: Delete test project
34
+ end
35
+ end
36
+
37
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
38
+ SimpleCov::Formatter::HTMLFormatter,
39
+ Coveralls::SimpleCov::Formatter
40
+ ]
41
+
42
+ SimpleCov.start do
43
+ add_filter 'spec/'
44
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../../lib/json2csv'
2
+
3
+ describe Json2Csv do
4
+ it 'Is defined' do
5
+ expect(Json2Csv::VERSION.nil?).to be_falsey
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,232 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json2csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomas Korcak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.11'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.11.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.11'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.11.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: json_pure
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 1.8.1
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 1.8.1
47
+ - !ruby/object:Gem::Dependency
48
+ name: multi_json
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 1.10.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 1.10.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.3'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 10.3.2
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '10.3'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 10.3.2
81
+ - !ruby/object:Gem::Dependency
82
+ name: terminal-table
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '1.4'
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.4.5
91
+ type: :runtime
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '1.4'
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 1.4.5
101
+ - !ruby/object:Gem::Dependency
102
+ name: coveralls
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: '0.7'
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 0.7.0r
111
+ type: :development
112
+ prerelease: false
113
+ version_requirements: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.7'
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 0.7.0r
121
+ - !ruby/object:Gem::Dependency
122
+ name: rspec
123
+ requirement: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - "~>"
126
+ - !ruby/object:Gem::Version
127
+ version: '3.0'
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 3.0.0
131
+ type: :development
132
+ prerelease: false
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - "~>"
136
+ - !ruby/object:Gem::Version
137
+ version: '3.0'
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: 3.0.0
141
+ - !ruby/object:Gem::Dependency
142
+ name: rubocop
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '0.24'
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 0.24.0
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '0.24'
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: 0.24.0
161
+ - !ruby/object:Gem::Dependency
162
+ name: simplecov
163
+ requirement: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: '0.8'
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: 0.8.2
171
+ type: :development
172
+ prerelease: false
173
+ version_requirements: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - "~>"
176
+ - !ruby/object:Gem::Version
177
+ version: '0.8'
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: 0.8.2
181
+ description: Quick and Dirty - No time to waste time
182
+ email: korczis@gmail.com
183
+ executables:
184
+ - json2csv
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gemspec"
189
+ - ".gitignore"
190
+ - ".travis.yml"
191
+ - Gemfile
192
+ - LICENSE
193
+ - README.md
194
+ - Rakefile
195
+ - bin/json2csv
196
+ - data/sample.json
197
+ - lib/json2csv.rb
198
+ - lib/json2csv/cli/app.rb
199
+ - lib/json2csv/cli/cli.rb
200
+ - lib/json2csv/cli/cmd/convert_cmd.rb
201
+ - lib/json2csv/cli/cmd/version_cmd.rb
202
+ - lib/json2csv/cli/convert/convert.rb
203
+ - lib/json2csv/cli/shared.rb
204
+ - lib/json2csv/lib.rb
205
+ - lib/json2csv/version.rb
206
+ - spec/spec_helper.rb
207
+ - spec/unit/main_spec.rb
208
+ homepage: https://github.com/korczis/json2csv
209
+ licenses:
210
+ - MIT
211
+ metadata: {}
212
+ post_install_message:
213
+ rdoc_options: []
214
+ require_paths:
215
+ - lib
216
+ required_ruby_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ required_rubygems_version: !ruby/object:Gem::Requirement
222
+ requirements:
223
+ - - ">="
224
+ - !ruby/object:Gem::Version
225
+ version: '0'
226
+ requirements: []
227
+ rubyforge_project:
228
+ rubygems_version: 2.2.2
229
+ signing_key:
230
+ specification_version: 4
231
+ summary: Simple tool for converting json2csv
232
+ test_files: []