json_csv_converter 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,2 +1,5 @@
1
1
  # Mac .DS_Store
2
2
  .DS_Store
3
+ *.gem
4
+ coverage
5
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ - 2.0.0
11
+ - ruby-head
12
+ - jruby-head
13
+ # uncomment this line if your project needs to run something other than `rake`:
14
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENCE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2012 Michael Wagner <mitch.wagna@gmail.com>
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,7 +1,12 @@
1
1
  # JSON CSV Converter
2
+
2
3
  This gem is built to help at the migration between existing JSON files into the mappings of existing CSV tables and vice versa.
3
4
  To map the keys you provide a YAML file.
4
5
 
6
+ ## Status
7
+
8
+ [![Build Status](https://travis-ci.org/mitch000001/json_csv_converter.png?branch=master)](https://travis-ci.org/mitch000001/json_csv_converter) [![Dependency Status](https://gemnasium.com/mitch000001/json_csv_converter.png)](https://gemnasium.com/mitch000001/json_csv_converter) [![Code Climate](https://codeclimate.com/github/mitch000001/json_csv_converter.png)](https://codeclimate.com/github/mitch000001/json_csv_converter)
9
+
5
10
  ## Installation
6
11
 
7
12
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -7,9 +7,10 @@ RSpec::Core::RakeTask.new('spec')
7
7
  task :default => :spec
8
8
 
9
9
  desc "Create spec for class or module"
10
- task :create_spec, [:class_name] do |t,args|
10
+ task :create_spec, [:class_name,:spec_file_name] do |t,args|
11
11
  class_name = args.class_name
12
- File.open("spec/#{class_name}_spec","w") do |file|
12
+ file_name = args.spec_file_name || class_name.split("::").last
13
+ File.open("#{Dir.pwd}/spec/#{file_name.to_s.downcase}_spec.rb","w") do |file|
13
14
  file.puts "require 'spec_helper'"
14
15
  file.puts
15
16
  file.puts %(describe #{class_name} do)
@@ -18,5 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.require_paths = ["lib"]
19
19
 
20
20
  s.add_dependency 'json'
21
+ s.add_dependency 'fastercsv'
21
22
  s.add_development_dependency 'rspec'
23
+ s.add_development_dependency 'rake'
24
+ s.add_development_dependency 'simplecov'
22
25
  end
@@ -1,14 +1,14 @@
1
1
  require 'rubygems'
2
- require 'csv'
3
2
  require 'json'
4
- require 'psych'
3
+ require 'yaml'
5
4
 
6
5
  require 'json_csv_converter/version'
6
+ require 'json_csv_converter/csv'
7
7
  require 'json_csv_converter/converter'
8
8
 
9
9
  module JSONCSVConverter
10
10
 
11
- def json_to_csv json,csv,mapping,option={}
11
+ def json_to_csv json,csv,mapping,options={}
12
12
  Converter.new(json,csv,mapping,options).json_to_csv.csv_table
13
13
  end
14
14
 
@@ -10,7 +10,12 @@ module JSONCSVConverter
10
10
  @json = JSON.load(File.open(json))
11
11
  @csv_path = csv
12
12
  @csv_table = CSV.table(csv,options)
13
- @mapping = Psych.load_file(mapping)
13
+ @mapping = YAML.load_file(mapping)
14
+ end
15
+
16
+ def csv_table
17
+ json_to_csv
18
+ @csv_table
14
19
  end
15
20
 
16
21
  def json_to_csv
@@ -21,7 +26,7 @@ module JSONCSVConverter
21
26
  column_hash = {}
22
27
  mapping_key = @mapping["to_csv"][column.to_s]
23
28
  if mapping_key.kind_of? Array
24
- column_hash[column] = mapping_key.inject(element) {|whole,map_key|whole = whole[map_key]}
29
+ column_hash[column] = mapping_key.inject(element) {|whole,map_key| whole[map_key]}
25
30
  else
26
31
  column_hash[column] = element[mapping_key]
27
32
  end
@@ -31,6 +36,19 @@ module JSONCSVConverter
31
36
  end
32
37
  self
33
38
  end
39
+
40
+ def csv_to_json
41
+ mapped_csv = @csv_table.dup
42
+ mapped_csv.each do |row|
43
+ row_hash = row.to_hash
44
+ mapped_row_hash = {}
45
+ row_hash.each do |key,value|
46
+ mapped_row_hash[@mapping["to_csv"][key.to_s]] = value
47
+ end
48
+ @json[@json_name] << mapped_row_hash
49
+ end
50
+ @json
51
+ end
34
52
 
35
53
  end
36
54
 
@@ -0,0 +1,10 @@
1
+ module JSONCSVConverter
2
+
3
+ if RUBY_VERSION < "1.9"
4
+ require 'fastercsv'
5
+ const_set(:CSV,FasterCSV)
6
+ else
7
+ require 'csv'
8
+ end
9
+
10
+ end
@@ -1,3 +1,3 @@
1
1
  module JSONCSVConverter
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSONCSVConverter::Converter do
4
+
5
+ before do
6
+ @converter = JSONCSVConverter::Converter.new("spec/fixtures/tests.json","spec/fixtures/test.csv","spec/fixtures/test_mapping.yml")
7
+ end
8
+
9
+ subject {@converter}
10
+
11
+ it {should_not be_nil}
12
+ it {should respond_to(:csv_table)}
13
+ it {should respond_to(:json)}
14
+ it {should respond_to(:json_to_csv)}
15
+ it {should respond_to(:csv_to_json)}
16
+
17
+ describe "migrate data from json to csv" do
18
+
19
+ it "#csv_table should return the old csv table with the json data appended" do
20
+ expected = <<-EOF
21
+ fohoo,bahaar
22
+ tidy up,41
23
+ 3,string
24
+ EOF
25
+ @converter.csv_table.to_csv.should == expected
26
+ end
27
+
28
+ end
29
+
30
+ describe "migrate data from csv to json" do
31
+
32
+ it "#json should return the old json object with the csv data appended" do
33
+ expected = %q({"tests":[{"foo":3,"bar":"string"},{"foo":"tidy up","bar":41}]})
34
+ @converter.csv_to_json.to_json.should == expected
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSONCSVConverter::Converter do
4
+
5
+ before do
6
+ @converter = JSONCSVConverter::Converter.new("spec/fixtures/tests.json","spec/fixtures/test_semicolon.csv","spec/fixtures/test_mapping.yml", {:col_sep => ";"})
7
+ end
8
+
9
+ subject {@converter}
10
+
11
+ it {should_not be_nil}
12
+
13
+ describe "migrate data from json to csv with options" do
14
+
15
+ it "#csv_table should return the old csv table with the json data appended" do
16
+ expected = <<-EOF
17
+ fohoo,bahaar
18
+ tidy up,41
19
+ 3,string
20
+ EOF
21
+ @converter.csv_table.to_csv.should == expected
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSONCSVConverter::Converter do
4
+
5
+ before do
6
+ @converter = JSONCSVConverter::Converter.new('spec/fixtures/tests.json','spec/fixtures/test_semicolon.csv','spec/fixtures/test_mapping.yml', {:col_sep => ';', :skip_blanks => true})
7
+ end
8
+
9
+ subject {@converter}
10
+
11
+ it {should_not be_nil}
12
+
13
+ describe 'migrate data from json to csv with options' do
14
+
15
+ it '#csv_table should return the old csv table with the json data appended' do
16
+ expected = <<-EOF
17
+ fohoo,bahaar
18
+ tidy up,41
19
+ 3,string
20
+ EOF
21
+ @converter.csv_table.to_csv.should == expected
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -0,0 +1,2 @@
1
+ fohoo,bahaar
2
+ tidy up,41
@@ -0,0 +1,3 @@
1
+ fohoo;bahaar
2
+
3
+ tidy up;41
@@ -0,0 +1,7 @@
1
+ to_csv:
2
+ fohoo: "foo"
3
+ bahaar: "bar"
4
+
5
+ to_json:
6
+ foo: "fohoo"
7
+ bar: "bahaar"
@@ -0,0 +1,2 @@
1
+ fohoo;bahaar
2
+ tidy up;41
@@ -0,0 +1,6 @@
1
+ {"tests":[{
2
+ "foo":3,
3
+ "bar":"string"
4
+ }
5
+ ]
6
+ }
@@ -1,6 +1,4 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
1
3
  require 'rspec'
2
4
  require 'json_csv_converter'
3
-
4
- RSpec.configure do |config|
5
- config.color = true
6
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_csv_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-02 00:00:00.000000000 Z
12
+ date: 2013-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: fastercsv
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: rspec
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -43,6 +59,38 @@ dependencies:
43
59
  - - ! '>='
44
60
  - !ruby/object:Gem::Version
45
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
46
94
  description: This gem provides a converter between an existing JSON file and an existing
47
95
  CSV file. The Mapping is provided via YAML.
48
96
  email:
@@ -53,13 +101,26 @@ extensions: []
53
101
  extra_rdoc_files: []
54
102
  files:
55
103
  - .gitignore
104
+ - .rspec
105
+ - .travis.yml
106
+ - Gemfile
107
+ - LICENCE
56
108
  - README.md
57
109
  - Rakefile
58
110
  - bin/json_csv_converter
59
111
  - json_csv_converter.gemspec
60
112
  - lib/json_csv_converter.rb
61
113
  - lib/json_csv_converter/converter.rb
114
+ - lib/json_csv_converter/csv.rb
62
115
  - lib/json_csv_converter/version.rb
116
+ - spec/converter_spec.rb
117
+ - spec/converter_with_options_spec.rb
118
+ - spec/converter_with_skip_blanks_option_spec.rb
119
+ - spec/fixtures/test.csv
120
+ - spec/fixtures/test_blanks.csv
121
+ - spec/fixtures/test_mapping.yml
122
+ - spec/fixtures/test_semicolon.csv
123
+ - spec/fixtures/tests.json
63
124
  - spec/spec_helper.rb
64
125
  homepage: https://github.com/mitch000001/json_csv_converter
65
126
  licenses: []
@@ -81,10 +142,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
142
  version: '0'
82
143
  requirements: []
83
144
  rubyforge_project:
84
- rubygems_version: 1.8.24
145
+ rubygems_version: 1.8.23
85
146
  signing_key:
86
147
  specification_version: 3
87
148
  summary: A converter between JSON and CSV files
88
149
  test_files:
150
+ - spec/converter_spec.rb
151
+ - spec/converter_with_options_spec.rb
152
+ - spec/converter_with_skip_blanks_option_spec.rb
153
+ - spec/fixtures/test.csv
154
+ - spec/fixtures/test_blanks.csv
155
+ - spec/fixtures/test_mapping.yml
156
+ - spec/fixtures/test_semicolon.csv
157
+ - spec/fixtures/tests.json
89
158
  - spec/spec_helper.rb
90
159
  has_rdoc: