json_cli 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.rubocop.yml +3 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +54 -0
- data/bin/json_cli +60 -0
- data/json_cli.gemspec +32 -0
- data/lib/json_cli/join.rb +41 -0
- data/lib/json_cli/unwind.rb +43 -0
- data/lib/json_cli/version.rb +4 -0
- data/lib/json_cli.rb +7 -0
- data/spec/attribute.json +3 -0
- data/spec/empty.json +0 -0
- data/spec/json_cli/join_spec.rb +112 -0
- data/spec/json_cli/unwind_spec.rb +90 -0
- data/spec/json_cli_spec.rb +7 -0
- data/spec/logfile.json +4 -0
- data/spec/spec_helper.rb +5 -0
- metadata +213 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 29474e98dfc3d7fe8e3e3a3cd2237d3f3b2f7732
|
4
|
+
data.tar.gz: 890920b7bae3ac0a7c6c2bef4a1d134f7b736c79
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df304ae76aece5d5831d54ceb15c0d104096f88589f84c2d7beb8d5e804964e04f97c4804f40359de2c18bb2cd7a50312cee33a400d5632c8690a183d3732164
|
7
|
+
data.tar.gz: 6b88ca9485f19b68eb8352d347a1fc9999c1f42afe5ede5885ad2f3823b99717a92cef9e0c082f1d44c19d40e7555c7caa42d3b223bce04414f7c8b2487116ca
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Masaaki Kikuchi
|
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,43 @@
|
|
1
|
+
# JsonCli
|
2
|
+
|
3
|
+
JSON command line tools.
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/masa21kik/json_cli.png?branch=master)](https://travis-ci.org/masa21kik/json_cli)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/masa21kik/json_cli/badge.png)](https://coveralls.io/r/masa21kik/json_cli)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/masa21kik/json_cli.png?branch=master)](https://codeclimate.com/github/masa21kik/json_cli)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'json_cli'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install json_cli
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Join attributes where key is '_id'
|
26
|
+
|
27
|
+
$ cat logfile.json | json_cli left_join attribute.json -k _id
|
28
|
+
|
29
|
+
Unwind array or object
|
30
|
+
|
31
|
+
$ cat logfile.json | json_cli unwind -k tags
|
32
|
+
|
33
|
+
Select or ignore keys
|
34
|
+
|
35
|
+
$ cat logfile.json | json_cli select -k _id,tags
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
require 'flay'
|
4
|
+
require 'flay_task'
|
5
|
+
require 'flog'
|
6
|
+
require 'reek/rake/task'
|
7
|
+
require 'rubocop/rake_task'
|
8
|
+
|
9
|
+
ruby_source = FileList['lib/**/*.rb']
|
10
|
+
|
11
|
+
RSpec::Core::RakeTask.new(:spec)
|
12
|
+
|
13
|
+
task default: :spec
|
14
|
+
task quality: :rubocop
|
15
|
+
|
16
|
+
Reek::Rake::Task.new do |t|
|
17
|
+
t.fail_on_error = false
|
18
|
+
t.verbose = false
|
19
|
+
t.ruby_opts = ['-rubygems']
|
20
|
+
t.reek_opts = '--quiet'
|
21
|
+
t.source_files = ruby_source
|
22
|
+
end
|
23
|
+
|
24
|
+
FlayTask.new do |t|
|
25
|
+
t.dirs = ruby_source.map do |each|
|
26
|
+
each[/[^\/]+/]
|
27
|
+
end.uniq
|
28
|
+
t.threshold = 0
|
29
|
+
t.verbose = true
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Analyze for code complexity'
|
33
|
+
task :flog do
|
34
|
+
flog = Flog.new(continue: true)
|
35
|
+
flog.flog(*ruby_source)
|
36
|
+
threshold = 28
|
37
|
+
|
38
|
+
bad_methods = flog.totals.select do |name, score|
|
39
|
+
!(/##{flog.no_method}$/ =~ name) && score > threshold
|
40
|
+
end
|
41
|
+
bad_methods.sort { |a, b| a[1] <=> b[1] }.reverse.each do |name, score|
|
42
|
+
printf "%8.1f: %s\n", score, name
|
43
|
+
end
|
44
|
+
unless bad_methods.empty?
|
45
|
+
$stderr.puts "#{bad_methods.size} methods have a complexity > #{threshold}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
RuboCop::RakeTask.new do |task|
|
50
|
+
task.patterns = %w(lib/**/*.rb
|
51
|
+
spec/**/*.rb
|
52
|
+
Rakefile
|
53
|
+
Gemfile)
|
54
|
+
end
|
data/bin/json_cli
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'json_cli'
|
3
|
+
require 'thor'
|
4
|
+
|
5
|
+
module JsonCli
|
6
|
+
class CLI < Thor
|
7
|
+
desc "left_join RIGHT_FILE [LEFT_FILE]", "left outer join of json files"
|
8
|
+
option :key, :required => true, :aliases => :k
|
9
|
+
def left_join(right_file, left_file = nil)
|
10
|
+
left_io = left_file.nil? ? STDIN : File.open(left_file, 'r')
|
11
|
+
right_io = File.open(right_file, 'r')
|
12
|
+
JsonCli::JoinJson.left_join(left_io, right_io, options[:key])
|
13
|
+
left_io.close
|
14
|
+
right_io.close
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "right_join RIGHT_FILE [LEFT_FILE]", "right outer join of json files"
|
18
|
+
option :key, :required => true, :aliases => :k
|
19
|
+
def right_join(right_file, left_file = nil)
|
20
|
+
left_io = left_file.nil? ? STDIN : File.open(left_file, 'r')
|
21
|
+
right_io = File.open(right_file, 'r')
|
22
|
+
JsonCli::JoinJson.right_join(left_io, right_io, options[:key])
|
23
|
+
left_io.close
|
24
|
+
right_io.close
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "inner_join RIGHT_FILE [LEFT_FILE]", "inner join of json files"
|
28
|
+
option :key, :required => true, :aliases => :k
|
29
|
+
def inner_join(right_file, left_file = nil)
|
30
|
+
left_io = left_file.nil? ? STDIN : File.open(left_file, 'r')
|
31
|
+
right_io = File.open(right_file, 'r')
|
32
|
+
JsonCli::JoinJson.inner_join(left_io, right_io, options[:key])
|
33
|
+
left_io.close
|
34
|
+
right_io.close
|
35
|
+
end
|
36
|
+
|
37
|
+
desc "unwind_array [JSON_FILE]", "unwind array of json file"
|
38
|
+
option :key, :required => true, :aliases => :k
|
39
|
+
def unwind_array(json_file = nil)
|
40
|
+
io = json_file.nil? ? STDIN : File.open(json_file, 'r')
|
41
|
+
JsonCli::UnwindJson.unwind_array(io, options[:key])
|
42
|
+
io.close
|
43
|
+
end
|
44
|
+
|
45
|
+
desc "unwind_hash [JSON_FILE]", "unwind hash of json file"
|
46
|
+
option :key, :required => true, :aliases => :k
|
47
|
+
option :flatten, :type => :boolean, :aliases => :f
|
48
|
+
option :key_label
|
49
|
+
option :value_label
|
50
|
+
def unwind_hash(json_file = nil)
|
51
|
+
io = json_file.nil? ? STDIN : File.open(json_file, 'r')
|
52
|
+
opt = {}
|
53
|
+
options.select{|k,v| k != :key}.each{|k,v| opt[k.to_sym] = v}
|
54
|
+
JsonCli::UnwindJson.unwind_hash(io, options[:key], opt)
|
55
|
+
io.close
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
JsonCli::CLI.start(ARGV)
|
data/json_cli.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'json_cli/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "json_cli"
|
8
|
+
spec.version = JsonCli::VERSION
|
9
|
+
spec.authors = ["Masaaki Kikuchi"]
|
10
|
+
spec.email = ["masa21kik@gmail.com"]
|
11
|
+
spec.description = %q{JSON command line tools}
|
12
|
+
spec.summary = %q{JSON command line tools}
|
13
|
+
spec.homepage = ""
|
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
|
+
|
21
|
+
spec.add_dependency "multi_json"
|
22
|
+
spec.add_dependency "thor"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "coveralls"
|
28
|
+
spec.add_development_dependency "flay"
|
29
|
+
spec.add_development_dependency "flog"
|
30
|
+
spec.add_development_dependency "reek"
|
31
|
+
spec.add_development_dependency "rubocop", "~> 0.23"
|
32
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module JsonCli
|
5
|
+
# Join JSON class
|
6
|
+
class JoinJson
|
7
|
+
def self.left_join(left_io, right_io, join_key, out = STDOUT)
|
8
|
+
right = io2hash(right_io, join_key)
|
9
|
+
left_io.each do |l|
|
10
|
+
j = MultiJson.load(l.chomp)
|
11
|
+
j.merge!(right[j[join_key]] || {}) if j.key?(join_key)
|
12
|
+
out.puts MultiJson.dump(j)
|
13
|
+
end
|
14
|
+
out.flush
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.right_join(left_io, right_io, join_key, out = STDOUT)
|
18
|
+
left_join(right_io, left_io, join_key, out)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.inner_join(left_io, right_io, join_key, out = STDOUT)
|
22
|
+
right = io2hash(right_io, join_key)
|
23
|
+
left_io.each do |l|
|
24
|
+
j = MultiJson.load(l.chomp)
|
25
|
+
next if !j.key?(join_key) || !right.key?(j[join_key])
|
26
|
+
out.puts MultiJson.dump(j.merge(right[j[join_key]]))
|
27
|
+
end
|
28
|
+
out.flush
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.io2hash(io, key)
|
32
|
+
hash = {}
|
33
|
+
io.each do |l|
|
34
|
+
j = MultiJson.load(l.chomp)
|
35
|
+
next unless j.key?(key)
|
36
|
+
hash[j[key]] = j.select { |k, _| k != key }
|
37
|
+
end
|
38
|
+
hash
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
2
|
+
require 'multi_json'
|
3
|
+
|
4
|
+
module JsonCli
|
5
|
+
# Unwind JSON class
|
6
|
+
class UnwindJson
|
7
|
+
def self.unwind_array(io, unwind_key, opt = {})
|
8
|
+
opt[:out] ||= STDOUT
|
9
|
+
io.each do |l|
|
10
|
+
j = MultiJson.load(l.chomp)
|
11
|
+
if j.key?(unwind_key) && j[unwind_key].is_a?(Array)
|
12
|
+
j[unwind_key].each do |v|
|
13
|
+
opt[:out].puts MultiJson.dump(j.merge(unwind_key => v))
|
14
|
+
end
|
15
|
+
else
|
16
|
+
opt[:out].puts MultiJson.dump(j)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.unwind_hash(io, unwind_key, opt = {})
|
22
|
+
opt[:out] ||= STDOUT
|
23
|
+
opt[:key_label] ||= 'key'
|
24
|
+
opt[:value_label] ||= 'value'
|
25
|
+
io.each do |l|
|
26
|
+
j = MultiJson.load(l.chomp)
|
27
|
+
if j.key?(unwind_key) && j[unwind_key].is_a?(Hash)
|
28
|
+
base = j.select { |k, _| k != unwind_key } if opt[:flatten]
|
29
|
+
j[unwind_key].each do |k, v|
|
30
|
+
if opt[:flatten]
|
31
|
+
jj = base.merge(opt[:key_label] => k, opt[:value_label] => v)
|
32
|
+
else
|
33
|
+
jj = j.merge(unwind_key => { k => v })
|
34
|
+
end
|
35
|
+
opt[:out].puts MultiJson.dump(jj)
|
36
|
+
end
|
37
|
+
else
|
38
|
+
opt[:out].puts MultiJson.dump(j)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/json_cli.rb
ADDED
data/spec/attribute.json
ADDED
data/spec/empty.json
ADDED
File without changes
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe JsonCli::JoinJson do
|
4
|
+
describe '#left_join' do
|
5
|
+
context 'when normal file input' do
|
6
|
+
it 'joins right to left' do
|
7
|
+
@left_io = File.open('spec/logfile.json', 'r')
|
8
|
+
@right_io = File.open('spec/attribute.json', 'r')
|
9
|
+
key = '_id'
|
10
|
+
result = StringIO.new
|
11
|
+
JsonCli::JoinJson.left_join(@left_io, @right_io, key, result)
|
12
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
13
|
+
expect(lines.size).to eq(4)
|
14
|
+
expect(lines[0]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
15
|
+
%q("tags":["news","sports"],"words":{"baseball":3,"soccer":2,) +
|
16
|
+
%q("ichiro":1,"honda":2},"title":"A","authors":["alice"]}))
|
17
|
+
expect(lines[1]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
18
|
+
%q("tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2},) +
|
19
|
+
%q("title":"B","authors":["bob","john"]}))
|
20
|
+
expect(lines[2]).to eq(%q({"_id":"0003","timestamp":1385274100,) +
|
21
|
+
%q("tags":["drama"],"words":{"furuhata":2,"ichiro":3}}))
|
22
|
+
expect(lines[3]).to eq(%q({"broken":"data"}))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when left is empty' do
|
27
|
+
it 'outputs nothing' do
|
28
|
+
@left_io = File.open('spec/empty.json', 'r')
|
29
|
+
@right_io = File.open('spec/attribute.json', 'r')
|
30
|
+
key = '_id'
|
31
|
+
result = StringIO.new
|
32
|
+
JsonCli::JoinJson.left_join(@left_io, @right_io, key, result)
|
33
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
34
|
+
expect(lines).to be_empty
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'when right is empty' do
|
39
|
+
it 'outputs just left' do
|
40
|
+
@left_io = File.open('spec/logfile.json', 'r')
|
41
|
+
@right_io = File.open('spec/empty.json', 'r')
|
42
|
+
key = '_id'
|
43
|
+
result = StringIO.new
|
44
|
+
JsonCli::JoinJson.left_join(@left_io, @right_io, key, result)
|
45
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
46
|
+
@left_io.rewind
|
47
|
+
left_lines = @left_io.each.to_a.map { |l| l.chomp }
|
48
|
+
expect(lines).to eq(left_lines)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#right_join' do
|
54
|
+
context 'when normal file input' do
|
55
|
+
it 'joins left to right' do
|
56
|
+
@left_io = File.open('spec/logfile.json', 'r')
|
57
|
+
@right_io = File.open('spec/attribute.json', 'r')
|
58
|
+
key = '_id'
|
59
|
+
result = StringIO.new
|
60
|
+
JsonCli::JoinJson.right_join(@left_io, @right_io, key, result)
|
61
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
62
|
+
expect(lines.size).to eq(3)
|
63
|
+
expect(lines[0]).to eq(%q({"_id":"0001","title":"A",) +
|
64
|
+
%q("authors":["alice"],"timestamp":1385273700,) +
|
65
|
+
%q("tags":["news","sports"],"words":{"baseball":3,) +
|
66
|
+
%q("soccer":2,"ichiro":1,"honda":2}}))
|
67
|
+
expect(lines[1]).to eq(%q({"_id":"0002","title":"B",) +
|
68
|
+
%q("authors":["bob","john"],"timestamp":1385273730,) +
|
69
|
+
%q("tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2}}))
|
70
|
+
expect(lines[2]).to eq(%q({"_id":"0004","title":"D",) +
|
71
|
+
%q("authors":["dave"]}))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#inner_join' do
|
77
|
+
context 'when normal file input' do
|
78
|
+
it 'joins inner' do
|
79
|
+
@left_io = File.open('spec/logfile.json', 'r')
|
80
|
+
@right_io = File.open('spec/attribute.json', 'r')
|
81
|
+
key = '_id'
|
82
|
+
result = StringIO.new
|
83
|
+
JsonCli::JoinJson.inner_join(@left_io, @right_io, key, result)
|
84
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
85
|
+
expect(lines.size).to eq(2)
|
86
|
+
expect(lines[0]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
87
|
+
%q("tags":["news","sports"],"words":{"baseball":3,"soccer":2,) +
|
88
|
+
%q("ichiro":1,"honda":2},"title":"A","authors":["alice"]}))
|
89
|
+
expect(lines[1]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
90
|
+
%q("tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2},) +
|
91
|
+
%q("title":"B","authors":["bob","john"]}))
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'when right is empty' do
|
96
|
+
it 'outputs nothing' do
|
97
|
+
@left_io = File.open('spec/logfile.json', 'r')
|
98
|
+
@right_io = File.open('spec/empty.json', 'r')
|
99
|
+
key = '_id'
|
100
|
+
result = StringIO.new
|
101
|
+
JsonCli::JoinJson.inner_join(@left_io, @right_io, key, result)
|
102
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
103
|
+
expect(lines).to be_empty
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
after do
|
109
|
+
@left_io.close if @left_io
|
110
|
+
@right_io.close if @right_io
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# -*- mode: ruby; coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe JsonCli::UnwindJson do
|
5
|
+
describe '#unwind_array' do
|
6
|
+
context 'when normal file input' do
|
7
|
+
it 'unwinds array values' do
|
8
|
+
@io = File.open('spec/logfile.json', 'r')
|
9
|
+
key = 'tags'
|
10
|
+
result = StringIO.new
|
11
|
+
JsonCli::UnwindJson.unwind_array(@io, key, out: result)
|
12
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
13
|
+
expect(lines.size).to eq(5)
|
14
|
+
expect(lines[0]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
15
|
+
%q("tags":"news","words":{"baseball":3,"soccer":2,"ichiro":1,) +
|
16
|
+
%q("honda":2}}))
|
17
|
+
expect(lines[1]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
18
|
+
%q("tags":"sports","words":{"baseball":3,"soccer":2,"ichiro":1,) +
|
19
|
+
%q("honda":2}}))
|
20
|
+
expect(lines[2]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
21
|
+
%q("tags":"sports","words":{"sumo":3,"tennis":1,"japan":2}}))
|
22
|
+
expect(lines[3]).to eq(%q({"_id":"0003","timestamp":1385274100,) +
|
23
|
+
%q("tags":"drama","words":{"furuhata":2,"ichiro":3}}))
|
24
|
+
expect(lines[4]).to eq(%q({"broken":"data"}))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#unwind_array' do
|
30
|
+
context 'when normal file input' do
|
31
|
+
it 'unwinds hash values without flatten' do
|
32
|
+
@io = File.open('spec/logfile.json', 'r')
|
33
|
+
key = 'words'
|
34
|
+
result = StringIO.new
|
35
|
+
JsonCli::UnwindJson.unwind_hash(@io, key, out: result)
|
36
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
37
|
+
expect(lines.size).to eq(10)
|
38
|
+
expect(lines[0]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
39
|
+
%q("tags":["news","sports"],"words":{"baseball":3}}))
|
40
|
+
expect(lines[1]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
41
|
+
%q("tags":["news","sports"],"words":{"soccer":2}}))
|
42
|
+
expect(lines[2]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
43
|
+
%q("tags":["news","sports"],"words":{"ichiro":1}}))
|
44
|
+
expect(lines[3]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
45
|
+
%q("tags":["news","sports"],"words":{"honda":2}}))
|
46
|
+
expect(lines[4]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
47
|
+
%q("tags":["sports"],"words":{"sumo":3}}))
|
48
|
+
expect(lines[5]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
49
|
+
%q("tags":["sports"],"words":{"tennis":1}}))
|
50
|
+
expect(lines[6]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
51
|
+
%q("tags":["sports"],"words":{"japan":2}}))
|
52
|
+
expect(lines[7]).to eq(%q({"_id":"0003","timestamp":1385274100,) +
|
53
|
+
%q("tags":["drama"],"words":{"furuhata":2}}))
|
54
|
+
expect(lines[8]).to eq(%q({"_id":"0003","timestamp":1385274100,) +
|
55
|
+
%q("tags":["drama"],"words":{"ichiro":3}}))
|
56
|
+
expect(lines[9]).to eq(%q({"broken":"data"}))
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'unwinds hash values with flatten' do
|
60
|
+
@io = File.open('spec/logfile.json', 'r')
|
61
|
+
key = 'words'
|
62
|
+
result = StringIO.new
|
63
|
+
JsonCli::UnwindJson.unwind_hash(
|
64
|
+
@io, key,
|
65
|
+
out: result, flatten: true, key_label: 'word', value_label: 'count')
|
66
|
+
lines = result.string.each_line.to_a.map { |l| l.chomp }
|
67
|
+
expect(lines.size).to eq(10)
|
68
|
+
expect(lines[0]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
69
|
+
%q("tags":["news","sports"],"word":"baseball","count":3}))
|
70
|
+
expect(lines[1]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
71
|
+
%q("tags":["news","sports"],"word":"soccer","count":2}))
|
72
|
+
expect(lines[2]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
73
|
+
%q("tags":["news","sports"],"word":"ichiro","count":1}))
|
74
|
+
expect(lines[3]).to eq(%q({"_id":"0001","timestamp":1385273700,) +
|
75
|
+
%q("tags":["news","sports"],"word":"honda","count":2}))
|
76
|
+
expect(lines[4]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
77
|
+
%q("tags":["sports"],"word":"sumo","count":3}))
|
78
|
+
expect(lines[5]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
79
|
+
%q("tags":["sports"],"word":"tennis","count":1}))
|
80
|
+
expect(lines[6]).to eq(%q({"_id":"0002","timestamp":1385273730,) +
|
81
|
+
%q("tags":["sports"],"word":"japan","count":2}))
|
82
|
+
expect(lines[7]).to eq(%q({"_id":"0003","timestamp":1385274100,) +
|
83
|
+
%q("tags":["drama"],"word":"furuhata","count":2}))
|
84
|
+
expect(lines[8]).to eq(%q({"_id":"0003","timestamp":1385274100,) +
|
85
|
+
%q("tags":["drama"],"word":"ichiro","count":3}))
|
86
|
+
expect(lines[9]).to eq(%q({"broken":"data"}))
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/logfile.json
ADDED
@@ -0,0 +1,4 @@
|
|
1
|
+
{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"words":{"baseball":3,"soccer":2,"ichiro":1,"honda":2}}
|
2
|
+
{"_id":"0002","timestamp":1385273730,"tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2}}
|
3
|
+
{"_id":"0003","timestamp":1385274100,"tags":["drama"],"words":{"furuhata":2,"ichiro":3}}
|
4
|
+
{"broken":"data"}
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: json_cli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masaaki Kikuchi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: multi_json
|
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: thor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
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: rspec
|
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: coveralls
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: flay
|
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: flog
|
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
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: reek
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.23'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.23'
|
153
|
+
description: JSON command line tools
|
154
|
+
email:
|
155
|
+
- masa21kik@gmail.com
|
156
|
+
executables:
|
157
|
+
- json_cli
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- ".travis.yml"
|
165
|
+
- Gemfile
|
166
|
+
- LICENSE.txt
|
167
|
+
- README.md
|
168
|
+
- Rakefile
|
169
|
+
- bin/json_cli
|
170
|
+
- json_cli.gemspec
|
171
|
+
- lib/json_cli.rb
|
172
|
+
- lib/json_cli/join.rb
|
173
|
+
- lib/json_cli/unwind.rb
|
174
|
+
- lib/json_cli/version.rb
|
175
|
+
- spec/attribute.json
|
176
|
+
- spec/empty.json
|
177
|
+
- spec/json_cli/join_spec.rb
|
178
|
+
- spec/json_cli/unwind_spec.rb
|
179
|
+
- spec/json_cli_spec.rb
|
180
|
+
- spec/logfile.json
|
181
|
+
- spec/spec_helper.rb
|
182
|
+
homepage: ''
|
183
|
+
licenses:
|
184
|
+
- MIT
|
185
|
+
metadata: {}
|
186
|
+
post_install_message:
|
187
|
+
rdoc_options: []
|
188
|
+
require_paths:
|
189
|
+
- lib
|
190
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
196
|
+
requirements:
|
197
|
+
- - ">="
|
198
|
+
- !ruby/object:Gem::Version
|
199
|
+
version: '0'
|
200
|
+
requirements: []
|
201
|
+
rubyforge_project:
|
202
|
+
rubygems_version: 2.2.0
|
203
|
+
signing_key:
|
204
|
+
specification_version: 4
|
205
|
+
summary: JSON command line tools
|
206
|
+
test_files:
|
207
|
+
- spec/attribute.json
|
208
|
+
- spec/empty.json
|
209
|
+
- spec/json_cli/join_spec.rb
|
210
|
+
- spec/json_cli/unwind_spec.rb
|
211
|
+
- spec/json_cli_spec.rb
|
212
|
+
- spec/logfile.json
|
213
|
+
- spec/spec_helper.rb
|