Skip to content

Commit

Permalink
Merge branch 'code-quality'
Browse files Browse the repository at this point in the history
  • Loading branch information
masa21kik committed Jun 4, 2014
2 parents 23aefb2 + 6d790b6 commit b3d0989
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MethodLength:
Max: 20

54 changes: 51 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'flay'
require 'flay_task'
require 'flog'
require 'reek/rake/task'
require 'rubocop/rake_task'

ruby_source = FileList['lib/**/*.rb']

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
task quality: :rubocop

Reek::Rake::Task.new do |t|
t.fail_on_error = false
t.verbose = false
t.ruby_opts = ['-rubygems']
t.reek_opts = '--quiet'
t.source_files = ruby_source
end

FlayTask.new do |t|
t.dirs = ruby_source.map do |each|
each[/[^\/]+/]
end.uniq
t.threshold = 0
t.verbose = true
end

desc 'Analyze for code complexity'
task :flog do
flog = Flog.new(continue: true)
flog.flog(*ruby_source)
threshold = 28

bad_methods = flog.totals.select do |name, score|
!(/##{flog.no_method}$/ =~ name) && score > threshold
end
bad_methods.sort { |a, b| a[1] <=> b[1] }.reverse.each do |name, score|
printf "%8.1f: %s\n", score, name
end
unless bad_methods.empty?
$stderr.puts "#{bad_methods.size} methods have a complexity > #{threshold}"
end
end

Rubocop::RakeTask.new do |task|
task.patterns = %w(lib/**/*.rb
spec/**/*.rb
Rakefile
Gemfile)
end
4 changes: 4 additions & 0 deletions json_cli.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "coveralls"
spec.add_development_dependency "flay"
spec.add_development_dependency "flog"
spec.add_development_dependency "reek"
spec.add_development_dependency "rubocop"
end
3 changes: 2 additions & 1 deletion lib/json_cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "json_cli/version"
require 'json_cli/version'

# Base module
module JsonCli
require 'json_cli/join'
require 'json_cli/unwind'
Expand Down
6 changes: 4 additions & 2 deletions lib/json_cli/join.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- mode: ruby; coding: utf-8 -*-
require "multi_json"
require 'multi_json'

module JsonCli
# Join JSON class
class JoinJson
def self.left_join(left_io, right_io, join_key, out = STDOUT)
right = io2hash(right_io, join_key)
Expand Down Expand Up @@ -32,12 +33,13 @@ def self.inner_join(left_io, right_io, join_key, out = STDOUT)
end

private

def self.io2hash(io, key)
hash = {}
io.each_line do |l|
j = MultiJson.load(l.chomp)
next unless j.key?(key)
hash[j[key]] = j.select{|k,v| k != key}
hash[j[key]] = j.select { |k, v| k != key }
end
hash
end
Expand Down
13 changes: 7 additions & 6 deletions lib/json_cli/unwind.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# -*- mode: ruby; coding: utf-8 -*-
require "multi_json"
require 'multi_json'

module JsonCli
# Unwind JSON class
class UnwindJson
def self.unwind_array(io, unwind_key, opt = {})
opt[:out] ||= STDOUT
io.each_line do |l|
j = MultiJson.load(l.chomp)
if j.key?(unwind_key) && j[unwind_key].is_a?(Array)
j[unwind_key].each do |v|
opt[:out].puts MultiJson.dump(j.merge({unwind_key => v}))
opt[:out].puts MultiJson.dump(j.merge(unwind_key => v))
end
else
opt[:out].puts MultiJson.dump(j)
Expand All @@ -24,12 +25,12 @@ def self.unwind_hash(io, unwind_key, opt = {})
io.each_line do |l|
j = MultiJson.load(l.chomp)
if j.key?(unwind_key) && j[unwind_key].is_a?(Hash)
base = j.select{|k,v| k != unwind_key} if opt[:flatten]
j[unwind_key].each do |k,v|
base = j.select { |k, v| k != unwind_key } if opt[:flatten]
j[unwind_key].each do |k, v|
if opt[:flatten]
jj = base.merge({opt[:key_label] => k, opt[:value_label] => v,})
jj = base.merge(opt[:key_label] => k, opt[:value_label] => v)
else
jj = j.merge({unwind_key => {k => v}})
jj = j.merge(unwind_key => { k => v })
end
opt[:out].puts MultiJson.dump(jj)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/json_cli/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Base module
module JsonCli
VERSION = "0.0.1"
VERSION = '0.0.1'
end
45 changes: 30 additions & 15 deletions spec/json_cli/join_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
key = '_id'
result = StringIO.new
JsonCli::JoinJson.left_join(@left_io, @right_io, key, result)
lines = result.string.each_line.to_a.map{|l| l.chomp}
lines = result.string.each_line.to_a.map { |l| l.chomp }
expect(lines.size).to eq(4)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"words":{"baseball":3,"soccer":2,"ichiro":1,"honda":2},"title":"A","authors":["alice"]}!)
expect(lines[1]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2},"title":"B","authors":["bob","john"]}!)
expect(lines[2]).to eq(%q!{"_id":"0003","timestamp":1385274100,"tags":["drama"],"words":{"furuhata":2,"ichiro":3}}!)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"words":{"baseball":3,"soccer":2,! +
%q!"ichiro":1,"honda":2},"title":"A","authors":["alice"]}!)
expect(lines[1]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2},! +
%q!"title":"B","authors":["bob","john"]}!)
expect(lines[2]).to eq(%q!{"_id":"0003","timestamp":1385274100,! +
%q!"tags":["drama"],"words":{"furuhata":2,"ichiro":3}}!)
expect(lines[3]).to eq(%q!{"broken":"data"}!)
end
end
Expand All @@ -25,7 +30,7 @@
key = '_id'
result = StringIO.new
JsonCli::JoinJson.left_join(@left_io, @right_io, key, result)
lines = result.string.each_line.to_a.map{|l| l.chomp}
lines = result.string.each_line.to_a.map { |l| l.chomp }
expect(lines).to be_empty
end
end
Expand All @@ -37,9 +42,9 @@
key = '_id'
result = StringIO.new
JsonCli::JoinJson.left_join(@left_io, @right_io, key, result)
lines = result.string.each_line.to_a.map{|l| l.chomp}
lines = result.string.each_line.to_a.map { |l| l.chomp }
@left_io.rewind
left_lines = @left_io.each_line.to_a.map{|l| l.chomp}
left_lines = @left_io.each_line.to_a.map { |l| l.chomp }
expect(lines).to eq(left_lines)
end
end
Expand All @@ -53,11 +58,17 @@
key = '_id'
result = StringIO.new
JsonCli::JoinJson.right_join(@left_io, @right_io, key, result)
lines = result.string.each_line.to_a.map{|l| l.chomp}
lines = result.string.each_line.to_a.map { |l| l.chomp }
expect(lines.size).to eq(3)
expect(lines[0]).to eq(%q!{"_id":"0001","title":"A","authors":["alice"],"timestamp":1385273700,"tags":["news","sports"],"words":{"baseball":3,"soccer":2,"ichiro":1,"honda":2}}!)
expect(lines[1]).to eq(%q!{"_id":"0002","title":"B","authors":["bob","john"],"timestamp":1385273730,"tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2}}!)
expect(lines[2]).to eq(%q!{"_id":"0004","title":"D","authors":["dave"]}!)
expect(lines[0]).to eq(%q!{"_id":"0001","title":"A",! +
%q!"authors":["alice"],"timestamp":1385273700,! +
%q!"tags":["news","sports"],"words":{"baseball":3,! +
%q!"soccer":2,"ichiro":1,"honda":2}}!)
expect(lines[1]).to eq(%q!{"_id":"0002","title":"B",! +
%q!"authors":["bob","john"],"timestamp":1385273730,! +
%q!"tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2}}!)
expect(lines[2]).to eq(%q!{"_id":"0004","title":"D",! +
%q!"authors":["dave"]}!)
end
end
end
Expand All @@ -70,10 +81,14 @@
key = '_id'
result = StringIO.new
JsonCli::JoinJson.inner_join(@left_io, @right_io, key, result)
lines = result.string.each_line.to_a.map{|l| l.chomp}
lines = result.string.each_line.to_a.map { |l| l.chomp }
expect(lines.size).to eq(2)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"words":{"baseball":3,"soccer":2,"ichiro":1,"honda":2},"title":"A","authors":["alice"]}!)
expect(lines[1]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2},"title":"B","authors":["bob","john"]}!)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"words":{"baseball":3,"soccer":2,! +
%q!"ichiro":1,"honda":2},"title":"A","authors":["alice"]}!)
expect(lines[1]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":["sports"],"words":{"sumo":3,"tennis":1,"japan":2},! +
%q!"title":"B","authors":["bob","john"]}!)
end
end

Expand All @@ -84,7 +99,7 @@
key = '_id'
result = StringIO.new
JsonCli::JoinJson.inner_join(@left_io, @right_io, key, result)
lines = result.string.each_line.to_a.map{|l| l.chomp}
lines = result.string.each_line.to_a.map { |l| l.chomp }
expect(lines).to be_empty
end
end
Expand Down
82 changes: 54 additions & 28 deletions spec/json_cli/unwind_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
@io = File.open('spec/logfile.json', 'r')
key = 'tags'
result = StringIO.new
JsonCli::UnwindJson.unwind_array(@io, key, :out => result)
lines = result.string.each_line.to_a.map{|l| l.chomp}
JsonCli::UnwindJson.unwind_array(@io, key, out: result)
lines = result.string.each_line.to_a.map { |l| l.chomp }
expect(lines.size).to eq(5)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":"news","words":{"baseball":3,"soccer":2,"ichiro":1,"honda":2}}!)
expect(lines[1]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":"sports","words":{"baseball":3,"soccer":2,"ichiro":1,"honda":2}}!)
expect(lines[2]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":"sports","words":{"sumo":3,"tennis":1,"japan":2}}!)
expect(lines[3]).to eq(%q!{"_id":"0003","timestamp":1385274100,"tags":"drama","words":{"furuhata":2,"ichiro":3}}!)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":"news","words":{"baseball":3,"soccer":2,"ichiro":1,! +
%q!"honda":2}}!)
expect(lines[1]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":"sports","words":{"baseball":3,"soccer":2,"ichiro":1,! +
%q!"honda":2}}!)
expect(lines[2]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":"sports","words":{"sumo":3,"tennis":1,"japan":2}}!)
expect(lines[3]).to eq(%q!{"_id":"0003","timestamp":1385274100,! +
%q!"tags":"drama","words":{"furuhata":2,"ichiro":3}}!)
expect(lines[4]).to eq(%q!{"broken":"data"}!)
end
end
Expand All @@ -26,37 +32,57 @@
@io = File.open('spec/logfile.json', 'r')
key = 'words'
result = StringIO.new
JsonCli::UnwindJson.unwind_hash(@io, key, :out => result)
lines = result.string.each_line.to_a.map{|l| l.chomp}
JsonCli::UnwindJson.unwind_hash(@io, key, out: result)
lines = result.string.each_line.to_a.map { |l| l.chomp }
expect(lines.size).to eq(10)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"words":{"baseball":3}}!)
expect(lines[1]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"words":{"soccer":2}}!)
expect(lines[2]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"words":{"ichiro":1}}!)
expect(lines[3]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"words":{"honda":2}}!)
expect(lines[4]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":["sports"],"words":{"sumo":3}}!)
expect(lines[5]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":["sports"],"words":{"tennis":1}}!)
expect(lines[6]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":["sports"],"words":{"japan":2}}!)
expect(lines[7]).to eq(%q!{"_id":"0003","timestamp":1385274100,"tags":["drama"],"words":{"furuhata":2}}!)
expect(lines[8]).to eq(%q!{"_id":"0003","timestamp":1385274100,"tags":["drama"],"words":{"ichiro":3}}!)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"words":{"baseball":3}}!)
expect(lines[1]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"words":{"soccer":2}}!)
expect(lines[2]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"words":{"ichiro":1}}!)
expect(lines[3]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"words":{"honda":2}}!)
expect(lines[4]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":["sports"],"words":{"sumo":3}}!)
expect(lines[5]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":["sports"],"words":{"tennis":1}}!)
expect(lines[6]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":["sports"],"words":{"japan":2}}!)
expect(lines[7]).to eq(%q!{"_id":"0003","timestamp":1385274100,! +
%q!"tags":["drama"],"words":{"furuhata":2}}!)
expect(lines[8]).to eq(%q!{"_id":"0003","timestamp":1385274100,! +
%q!"tags":["drama"],"words":{"ichiro":3}}!)
expect(lines[9]).to eq(%q!{"broken":"data"}!)
end

it 'unwinds hash values with flatten' do
@io = File.open('spec/logfile.json', 'r')
key = 'words'
result = StringIO.new
JsonCli::UnwindJson.unwind_hash(@io, key, :out => result, :flatten => true, :key_label => 'word', :value_label => 'count')
lines = result.string.each_line.to_a.map{|l| l.chomp}
JsonCli::UnwindJson.unwind_hash(
@io, key,
out: result, flatten: true, key_label: 'word', value_label: 'count')
lines = result.string.each_line.to_a.map { |l| l.chomp }
expect(lines.size).to eq(10)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"word":"baseball","count":3}!)
expect(lines[1]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"word":"soccer","count":2}!)
expect(lines[2]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"word":"ichiro","count":1}!)
expect(lines[3]).to eq(%q!{"_id":"0001","timestamp":1385273700,"tags":["news","sports"],"word":"honda","count":2}!)
expect(lines[4]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":["sports"],"word":"sumo","count":3}!)
expect(lines[5]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":["sports"],"word":"tennis","count":1}!)
expect(lines[6]).to eq(%q!{"_id":"0002","timestamp":1385273730,"tags":["sports"],"word":"japan","count":2}!)
expect(lines[7]).to eq(%q!{"_id":"0003","timestamp":1385274100,"tags":["drama"],"word":"furuhata","count":2}!)
expect(lines[8]).to eq(%q!{"_id":"0003","timestamp":1385274100,"tags":["drama"],"word":"ichiro","count":3}!)
expect(lines[0]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"word":"baseball","count":3}!)
expect(lines[1]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"word":"soccer","count":2}!)
expect(lines[2]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"word":"ichiro","count":1}!)
expect(lines[3]).to eq(%q!{"_id":"0001","timestamp":1385273700,! +
%q!"tags":["news","sports"],"word":"honda","count":2}!)
expect(lines[4]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":["sports"],"word":"sumo","count":3}!)
expect(lines[5]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":["sports"],"word":"tennis","count":1}!)
expect(lines[6]).to eq(%q!{"_id":"0002","timestamp":1385273730,! +
%q!"tags":["sports"],"word":"japan","count":2}!)
expect(lines[7]).to eq(%q!{"_id":"0003","timestamp":1385274100,! +
%q!"tags":["drama"],"word":"furuhata","count":2}!)
expect(lines[8]).to eq(%q!{"_id":"0003","timestamp":1385274100,! +
%q!"tags":["drama"],"word":"ichiro","count":3}!)
expect(lines[9]).to eq(%q!{"broken":"data"}!)
end
end
Expand Down

0 comments on commit b3d0989

Please sign in to comment.