Skip to content

Commit

Permalink
Merge pull request #2 from masa21kik/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
masa21kik committed May 31, 2014
2 parents e37fe0b + 13857d2 commit ace80b9
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bin/csvconv
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ if $PROGRAM_NAME == __FILE__

inputs = ARGV.empty? ? [STDIN] : ARGV.map { |f| File.open(f) }
inputs.each do |input|
CSVConv.send("to_#{format.to_s}", input, output, options)
output.puts CSVConv.send("csv2#{format.to_s}", input, options)
end

output.close
Expand Down
14 changes: 7 additions & 7 deletions lib/csvconv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

# CSV Converter
module CSVConv
def to_json(input, output, options)
def csv2json(input, options)
cv = Converter.new(:json, options)
cv.convert_stream(input, output)
cv.convert(input)
end

def to_yaml(input, output, options)
def csv2yaml(input, options)
cv = Converter.new(:yaml, options)
cv.convert(input, output)
cv.convert(input)
end

def to_ltsv(input, output, options)
def csv2ltsv(input, options)
cv = Converter.new(:ltsv, options)
cv.convert_stream(input, output)
cv.convert(input)
end

module_function :to_json, :to_yaml, :to_ltsv
module_function :csv2json, :csv2yaml, :csv2ltsv
end
4 changes: 2 additions & 2 deletions lib/csvconv/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def initialize(format, options)
@header = options[:header]
end

def convert(input, output)
def convert(input)
@header ||= Parser.read_header(input, @sep)
hash_array = []
while (line = input.gets)
hash_array << Parser.parse_line(line, @header, @sep)
end
output.puts Formatter.send(@format, hash_array)
Formatter.send(@format, hash_array)
end

def convert_stream(input, output)
Expand Down
8 changes: 4 additions & 4 deletions lib/csvconv/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module CSVConv
module Formatter
def json(hash_array)
hash_array.map do |hash|
hash.to_json
end
hash.to_json + "\n"
end.join
end

def yaml(hash_array)
Expand All @@ -16,8 +16,8 @@ def yaml(hash_array)

def ltsv(hash_array)
hash_array.map do |hash|
hash.map { |key, val| [key, val].join(':') }.join("\t")
end
hash.map { |key, val| [key, val].join(':') }.join("\t") + "\n"
end.join
end

module_function :json, :yaml, :ltsv
Expand Down
40 changes: 40 additions & 0 deletions spec/csvconv/converter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'spec_helper'

describe CSVConv::Converter do
shared_examples_for 'convert file format stream' do
before do
@input = File.open(input_path)
@output = StringIO.new
end

it 'convert file format stream' do
cv = CSVConv::Converter.new(format, options)
cv.convert_stream(@input, @output)
actual = @output.string
expected = File.read(input_path.sub(/[^\.]+$/, format))
expect(actual).to eq expected
end

after do
@input.close
@output.close
end
end

let(:fixture_dir) { File.expand_path('../../fixtures', __FILE__) }

context 'CSV with header (books.csv)' do
let(:input_path) { File.join(fixture_dir, 'books.csv') }
let(:options) { {} }

context 'json' do
let(:format) { 'json' }
it_behaves_like 'convert file format stream'
end

context 'ltsv' do
let(:format) { 'ltsv' }
it_behaves_like 'convert file format stream'
end
end
end
64 changes: 54 additions & 10 deletions spec/csvconv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,83 @@

shared_examples_for 'convert file format' do
before do
@input = File.open(input_csv)
@output = StringIO.new
@input = File.open(input_path)
end

def expect_file_path
dir = File.dirname(input_path)
base = File.basename(input_path, '.*').sub('_noheader', '')
File.join(dir, "#{base}.#{format}")
end

it 'convert file format' do
CSVConv.send("to_#{format}", @input, @output, options)
expected_result = File.read(input_csv.sub(/csv$/, format))
expect(@output.string).to eq expected_result
actual = CSVConv.send("csv2#{format}", @input, options)
expected = File.read(expect_file_path)
expect(actual).to eq expected
end

after do
@input.close
@output.close
end
end

let(:fixture_dir) { File.expand_path('../fixtures', __FILE__) }

context 'CSV with header (books.csv)' do
let(:input_csv) { File.join(fixture_dir, 'books.csv') }
let(:input_path) { File.join(fixture_dir, 'books.csv') }
let(:options) { {} }

describe '#to_json' do
describe '#csv2json' do
let(:format) { 'json' }
it_behaves_like 'convert file format'
end

describe '#csv2yaml' do
let(:format) { 'yaml' }
it_behaves_like 'convert file format'
end

describe '#csv2ltsv' do
let(:format) { 'ltsv' }
it_behaves_like 'convert file format'
end
end

context 'CSV without header (books.csv)' do
let(:input_path) { File.join(fixture_dir, 'books_noheader.csv') }
let(:options) { { header: %w(Title Author Price) } }

describe '#csv2json' do
let(:format) { 'json' }
it_behaves_like 'convert file format'
end

describe '#csv2yaml' do
let(:format) { 'yaml' }
it_behaves_like 'convert file format'
end

describe '#csv2ltsv' do
let(:format) { 'ltsv' }
it_behaves_like 'convert file format'
end
end

context 'TSV with header (books.tsv)' do
let(:input_path) { File.join(fixture_dir, 'books.tsv') }
let(:options) { { sep: "\t" } }

describe '#csv2json' do
let(:format) { 'json' }
it_behaves_like 'convert file format'
end

describe '#to_yaml' do
describe '#csv2yaml' do
let(:format) { 'yaml' }
it_behaves_like 'convert file format'
end

describe '#to_ltsv' do
describe '#csv2ltsv' do
let(:format) { 'ltsv' }
it_behaves_like 'convert file format'
end
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/books.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Title Author Price
The Fault in Our Stars John Green 12.99
City of Heavenly Fire (The Mortal Instruments) Cassandra Clare 24.99
Oh, The Places You'll Go! Dr. Seuss 17.99
Capital in the Twenty-First Century Thomas Piketty 39.95
Skin Game (Dresden Files) Jim Butcher 27.95
5 changes: 5 additions & 0 deletions spec/fixtures/books_noheader.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The Fault in Our Stars,John Green,12.99
City of Heavenly Fire (The Mortal Instruments),Cassandra Clare,24.99
"Oh, The Places You'll Go!",Dr. Seuss,17.99
Capital in the Twenty-First Century,Thomas Piketty,39.95
Skin Game (Dresden Files),Jim Butcher,27.95

0 comments on commit ace80b9

Please sign in to comment.