ss2json 0.3.2 → 1.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,95 @@
1
+
2
+ require 'roo'
3
+ require 'ss2json/row_converter'
4
+
5
+ module SS2JSON
6
+ class Converter
7
+
8
+ # Create a new converter the options are:
9
+ #
10
+ # * **:file** Input file.
11
+ # * **:sheet** Name of the sheet to use.
12
+ # * **:first_row** Which is the first row of real data
13
+ # * **:title_row** Where the title of the columns is.
14
+ # * **:check_column** Output only the results with a value present on the specific field.
15
+ # * **:key_column** Column of the keys for vertical mode.
16
+ # * **:value_column** Column of the values.
17
+ # * **:converter**: Options passed to the converter: Ss2Json::RowConverter
18
+ def initialize(options={})
19
+ @options = options
20
+ @doc = get_document_type.new(@options[:file])
21
+ set_default_sheet(@options[:sheet]) if @options[:sheet]
22
+ set_header_line(@options[:title_row]) if @options[:title_row]
23
+ end
24
+
25
+ def process_horizontal
26
+ @options[:first_row] += 1
27
+ @content = []
28
+ each_hash_row do |hash|
29
+ @options[:hash_key]
30
+ @content << hash
31
+ end
32
+ @content
33
+ end
34
+
35
+
36
+ def to_a
37
+ @doc.to_matrix.to_a
38
+ end
39
+
40
+ def process_vertical
41
+ hash = {}
42
+ each_row do |row|
43
+ key = @doc.cell(row, @options[:key_column])
44
+ value = @doc.cell(row, @options[:value_column])
45
+ hash[key] = value
46
+ end
47
+ @content = RowConverter.new(hash, @options[:converter])
48
+ @content
49
+ end
50
+
51
+ def sheets
52
+ @doc.sheets
53
+ end
54
+
55
+ protected
56
+
57
+ def set_default_sheet(sheet)
58
+ @doc.default_sheet = sheet
59
+ rescue RangeError => e
60
+ raise if @doc.sheets.include?(sheet)
61
+ raise "\nThe sheet #{sheet} did not exists. The available sheets are:\n" + sheets.map{|s| "\t* #{s}\n"}.join("")
62
+ end
63
+
64
+ def set_header_line(first_row)
65
+ @doc.header_line = first_row
66
+ end
67
+
68
+ def each_row
69
+ (@options[:first_row]).upto(@doc.last_row).each do |row_n|
70
+ yield row_n
71
+ end
72
+ end
73
+
74
+ def each_hash_row
75
+ each_row do |row|
76
+ row = @doc.find(row)[0]
77
+ object = RowConverter.new( row, @options[:converter] )
78
+ # TODO: Fix nested_hash to make set and get work on already converted items
79
+ next if @options[:check_column] && object[@options[:check_column]].nil?
80
+ yield object
81
+ end
82
+ end
83
+
84
+ def get_document_type
85
+ case @options[:file][/\.(.*)$/,1]
86
+ when /xlsx$/i then Excelx
87
+ when /xls$/i then Excel
88
+ when /ods$/i then Openoffice
89
+ else
90
+ raise "Unknown format"
91
+ end
92
+ end
93
+
94
+ end
95
+ end
@@ -1,6 +1,6 @@
1
1
  require 'nested_hash'
2
2
 
3
- class Ss2Json
3
+ module SS2JSON
4
4
  class RowConverter < NestedHash
5
5
 
6
6
  # Create a nested_hash from a hash with just one level (key,value).
@@ -19,7 +19,6 @@ class Ss2Json
19
19
  protected
20
20
 
21
21
  def is_valid_key?(key)
22
- return !(key =~ Regexp.new("/#{@options[:key_pattern]}/")) if @options[:key_pattern]
23
22
  super && ! (key =~ /^i\./i)
24
23
  end
25
24
 
@@ -1,3 +1,3 @@
1
- class Ss2Json
2
- VERSION = "0.3.2"
1
+ module SS2JSON
2
+ VERSION = "1.0.0.pre"
3
3
  end
data/ss2json.gemspec CHANGED
@@ -4,7 +4,7 @@ require "ss2json/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "ss2json"
7
- s.version = Ss2Json::VERSION
7
+ s.version = SS2JSON::VERSION
8
8
  s.authors = ["Guillermo Álvarez"]
9
9
  s.email = ["guillermo@cientifico.net"]
10
10
  s.homepage = ""
@@ -19,5 +19,16 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_runtime_dependency "nested_hash"
22
- s.add_runtime_dependency "roo", "1.10.1"
22
+ s.add_runtime_dependency "roo"
23
+ s.add_runtime_dependency "terminal-table"
24
+ s.add_runtime_dependency "json"
25
+ # s.add_runtime_dependency "system_timer"
26
+
27
+ # s.add_development_dependency "ronn"
28
+ # s.add_development_dependency "rake"
29
+ # s.add_development_dependency "ruby-debug"
30
+
31
+ # s.post_install_message = <<-EOF
32
+ # This project have man pages. Install gem-man and follow common instructions on google to be able to access that man pages through man(1)
33
+ # EOF
23
34
  end
@@ -0,0 +1,9 @@
1
+ +-----+------------+-----------+--------------+-------------+--------------+-------------+-----------------+
2
+ | id | name.first | name.last | child.1.name | child.1.age | child.2.name | child.2.age | i.fiscal_id |
3
+ | 1.0 | Guillermo | Alvarez | pepe | 2.0 | Juanjo | | 7123478912123.0 |
4
+ | 2.0 | Martin | Luther | Jr | | | | 478792345132.0 |
5
+ | 3.0 | Jesper | | | | | | 13478912347.0 |
6
+ | 4.0 | | | | | | | |
7
+ | 5.0 | | | | | | | |
8
+ | 6.0 | | | | | | | |
9
+ +-----+------------+-----------+--------------+-------------+--------------+-------------+-----------------+
@@ -0,0 +1,7 @@
1
+ +-------------------------------+--------+
2
+ | version | 1.2.3 |
3
+ | initial_options.coins | 450.0 |
4
+ | initial_options.lives | 5.0 |
5
+ | initial_options.plants.1.name | pepe |
6
+ | initial_options.plants.2.name | juanjo |
7
+ +-------------------------------+--------+
data/test/cli_test.rb ADDED
@@ -0,0 +1,61 @@
1
+
2
+
3
+ require 'ss2json'
4
+ require 'test/unit'
5
+
6
+ class Ss2Json::RowConverterTest < Test::Unit::TestCase
7
+
8
+ def test_class
9
+ nested_hash = Ss2Json::RowConverter.new({})
10
+ nested_hash.is_a?(Hash)
11
+ end
12
+
13
+ def test_ignored_values_option
14
+ options = {:ignored_values => ["???"]}
15
+ new_hash = Ss2Json::RowConverter.new({"adsf" => '???'},options)
16
+ assert_equal({}, new_hash)
17
+ new_hash = Ss2Json::RowConverter.new({"asdf" => 'asdf'}, options)
18
+ assert_equal({"asdf" => 'asdf' }, new_hash)
19
+ end
20
+
21
+ def test_show_null_option
22
+ initial_hash = {"asdf" => nil}
23
+ new_hash = Ss2Json::RowConverter.new(initial_hash,{:show_null => false})
24
+ assert_equal({}, new_hash)
25
+
26
+ new_hash = Ss2Json::RowConverter.new(initial_hash,{:show_null => true})
27
+ assert_equal({"asdf" => nil}, new_hash)
28
+ end
29
+
30
+ def test_dont_convert_option
31
+ initial_hash = { "asdf" => 3.0 }
32
+
33
+ new_hash = Ss2Json::RowConverter.new(initial_hash, {:dont_convert => true})
34
+ assert_equal(initial_hash, new_hash)
35
+
36
+ new_hash = Ss2Json::RowConverter.new(initial_hash, {:dont_convert => false})
37
+ assert_equal(initial_hash, { "asdf" => 3 })
38
+ end
39
+
40
+ def test_downcase_first_letter
41
+ initial_hash = { "Asdf.pepe" => 3, "asdf.Jose" => 5 }
42
+
43
+ new_hash = Ss2Json::RowConverter.new(initial_hash, {:downcase_first_letter => false})
44
+ assert_equal({"Asdf"=>{"pepe"=>3}, "asdf"=>{"Jose"=>5}}, new_hash)
45
+
46
+ new_hash = Ss2Json::RowConverter.new(initial_hash, {:downcase_first_letter => true})
47
+ assert_equal({"asdf" => {"pepe" => 3, "jose" => 5}}, new_hash)
48
+ end
49
+
50
+ def test_it_ignore_ignored_fields
51
+ initial_hash = { "i.asdf" => 5}
52
+
53
+ new_hash = Ss2Json::RowConverter.new(initial_hash)
54
+ assert_equal({}, new_hash)
55
+
56
+ initial_hash = { "name.i" => 5}
57
+
58
+ new_hash = Ss2Json::RowConverter.new(initial_hash)
59
+ assert_equal({"name" => {"i" => 5}}, new_hash)
60
+ end
61
+ end
@@ -0,0 +1,90 @@
1
+ require 'test/unit'
2
+ require 'json'
3
+
4
+ class Ss2JsonIntegrationTest < Test::Unit::TestCase
5
+
6
+
7
+ def call(cmd, args)
8
+ `ruby -I lib -rubygems bin/#{cmd} #{args}`
9
+ end
10
+
11
+ def callP(cmd,args)
12
+ JSON.parse(call(cmd,args))
13
+ end
14
+
15
+
16
+
17
+ # def test_vertical
18
+ # data = [ { "child"=>[{"name"=>"pepe", "age"=>2}, {"name"=>"Juanjo"}], "id"=>1, "name"=>{"last"=>"Alvarez", "first"=>"Guillermo"} }, { "child"=>[{"name"=>"Jr"}], "id"=>2, "name"=>{"last"=>"Luther", "first"=>"Martin"} }, {"id"=>3, "name"=>{"first"=>"Jesper"}}, {"id"=>4}, {"id"=>5}, {"id"=>6}]
19
+ # assert_equal data, ss2jsonP("-f test/ss2json.xls -s Test1")
20
+ # end
21
+
22
+ # def test_check_flag
23
+ # data = [{"child"=>[{"name"=>"pepe", "age"=>2}, {"name"=>"Juanjo"}], "name"=>{"last"=>"Alvarez", "first"=>"Guillermo"}, "id"=>1}, {"child"=>[{"name"=>"Jr"}], "name"=>{"last"=>"Luther", "first"=>"Martin"}, "id"=>2}, {"name"=>{"first"=>"Jesper"}, "id"=>3}]
24
+ # assert_equal data, ss2jsonP("-f test/ss2json.xls -s Test1 -c 'name'")
25
+ # end
26
+
27
+ # def test_use_key
28
+ # data = []
29
+ # assert_equal data, ss2jsonP("-f test/ss2json.xls -s Test1 -u 'uuid'")
30
+ # end
31
+
32
+
33
+
34
+ def test_lsss
35
+ assert_equal "Test1\nTest2\nTest3\nTest4\n", call('lsss', 'test/ss2json.xls')
36
+ end
37
+
38
+ def test_catss
39
+ expected = File.read('test/catss.fixture')
40
+ assert_equal expected , call('catss', 'test/ss2json.xls')
41
+ end
42
+
43
+ def test_catss_with_sheet
44
+ expected = File.read('test/catss2.fixture')
45
+ assert_equal expected , call('catss', 'test/ss2json.xls Test3')
46
+ end
47
+
48
+ def test_ss2json_horizontal
49
+ expected = [
50
+ { "child"=> [ { "age"=> 2, "name"=> "pepe" }, { "name"=> "Juanjo" } ], "id"=> 1, "name"=> { "last"=> "Alvarez", "first"=> "Guillermo" } },
51
+ { "child"=> [ { "name"=> "Jr" } ], "id"=> 2, "name"=> { "last"=> "Luther", "first"=> "Martin" } },
52
+ { "id"=> 3, "name"=> { "first"=> "Jesper" } },
53
+ { "id"=> 4 },
54
+ { "id"=> 5 },
55
+ { "id"=> 6 }
56
+ ]
57
+ assert_equal expected, callP('ss2json-horizontal', 'test/ss2json.xls -s Test1')
58
+ end
59
+
60
+ def test_ss2json_horizontal_with_check
61
+ expected = [
62
+ { "child"=> [ { "age"=> 2, "name"=> "pepe" }, { "name"=> "Juanjo" } ], "id"=> 1, "name"=> { "last"=> "Alvarez", "first"=> "Guillermo" } },
63
+ { "child"=> [ { "name"=> "Jr" } ], "id"=> 2, "name"=> { "last"=> "Luther", "first"=> "Martin" } },
64
+ { "id"=> 3, "name"=> { "first"=> "Jesper" } }
65
+ ]
66
+ assert_equal expected, callP('ss2json-horizontal', 'test/ss2json.xls -s Test1 -c name')
67
+ end
68
+
69
+ def test_ss2json_hash
70
+ expected = [
71
+ { "child"=> [ { "age"=> 2, "name"=> "pepe" }, { "name"=> "Juanjo" } ], "id"=> 1, "name"=> { "last"=> "Alvarez", "first"=> "Guillermo" } },
72
+ { "child"=> [ { "name"=> "Jr" } ], "id"=> 2, "name"=> { "last"=> "Luther", "first"=> "Martin" } },
73
+ { "id"=> 3, "name"=> { "first"=> "Jesper" } }
74
+ ]
75
+ assert_equal expected, callP('ss2json-horizontal-hash', 'test/ss2json.xls id -s Test1')
76
+ end
77
+
78
+ def test_ss2json_vertical
79
+ expected = {}
80
+ assert_equal expected, callP('ss2json-vertical', 'test/ss2json.xls -s Test3')
81
+ end
82
+
83
+
84
+ # compress-json
85
+ # merge-jsons
86
+ # order-json
87
+ # ss2json-horizontal
88
+ # ss2json-horizontal-hash
89
+ # ss2json-vertical
90
+ end
@@ -57,16 +57,4 @@ class Ss2Json::RowConverterTest < Test::Unit::TestCase
57
57
  new_hash = Ss2Json::RowConverter.new(initial_hash)
58
58
  assert_equal({"name" => {"i" => 5}}, new_hash)
59
59
  end
60
-
61
- def test_it_applies_key_pattern
62
- initial_hash = { "@name.i" => 5}
63
- options = {:key_pattern => '\A[\.\w\-@]+\z'}
64
-
65
- new_hash = Ss2Json::RowConverter.new(initial_hash, options)
66
- assert_equal({"@name" => {"i" => 5}}, new_hash)
67
-
68
- new_hash = Ss2Json::RowConverter.new(initial_hash)
69
- assert_equal({}, new_hash)
70
- end
71
-
72
60
  end
data/test/ss2json.xls CHANGED
Binary file
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ss2json
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
4
+ hash: 1964060728
5
+ prerelease: 6
6
6
  segments:
7
+ - 1
7
8
  - 0
8
- - 3
9
- - 2
10
- version: 0.3.2
9
+ - 0
10
+ - pre
11
+ version: 1.0.0.pre
11
12
  platform: ruby
12
13
  authors:
13
14
  - "Guillermo A\xCC\x81lvarez"
@@ -15,12 +16,12 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2013-06-07 00:00:00 Z
19
+ date: 2012-09-10 00:00:00 Z
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
- name: nested_hash
22
22
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
23
+ name: nested_hash
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
25
  none: false
25
26
  requirements:
26
27
  - - ">="
@@ -29,29 +30,54 @@ dependencies:
29
30
  segments:
30
31
  - 0
31
32
  version: "0"
33
+ requirement: *id001
32
34
  type: :runtime
33
- version_requirements: *id001
34
35
  - !ruby/object:Gem::Dependency
36
+ prerelease: false
35
37
  name: roo
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ requirement: *id002
48
+ type: :runtime
49
+ - !ruby/object:Gem::Dependency
36
50
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
51
+ name: terminal-table
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
38
53
  none: false
39
54
  requirements:
40
- - - "="
55
+ - - ">="
41
56
  - !ruby/object:Gem::Version
42
- hash: 61
57
+ hash: 3
43
58
  segments:
44
- - 1
45
- - 10
46
- - 1
47
- version: 1.10.1
59
+ - 0
60
+ version: "0"
61
+ requirement: *id003
62
+ type: :runtime
63
+ - !ruby/object:Gem::Dependency
64
+ prerelease: false
65
+ name: json
66
+ version_requirements: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ requirement: *id004
48
76
  type: :runtime
49
- version_requirements: *id002
50
77
  description: Convert SpreadSheet documents to json following some conventions.
51
78
  email:
52
79
  - guillermo@cientifico.net
53
80
  executables:
54
- - merge_jsons
55
81
  - ss2json
56
82
  extensions: []
57
83
 
@@ -63,16 +89,27 @@ files:
63
89
  - Gemfile
64
90
  - README.md
65
91
  - Rakefile
66
- - bin/merge_jsons
92
+ - balance.xlsx
67
93
  - bin/ss2json
68
94
  - doc/ss2json-1.png
69
95
  - doc/ss2json-2.png
70
- - lib/ss2json.rb
71
- - lib/ss2json/cli.rb
72
- - lib/ss2json/options.rb
96
+ - lib/ss2json-commands/cat.rb
97
+ - lib/ss2json-commands/compress.rb
98
+ - lib/ss2json-commands/constants.rb
99
+ - lib/ss2json-commands/humanize.rb
100
+ - lib/ss2json-commands/ls.rb
101
+ - lib/ss2json-commands/merge.rb
102
+ - lib/ss2json-commands/to_array.rb
103
+ - lib/ss2json-commands/to_hash.rb
104
+ - lib/ss2json/command.rb
105
+ - lib/ss2json/converter.rb
73
106
  - lib/ss2json/row_converter.rb
74
107
  - lib/ss2json/version.rb
75
108
  - ss2json.gemspec
109
+ - test/catss.fixture
110
+ - test/catss2.fixture
111
+ - test/cli_test.rb
112
+ - test/integration_test.rb
76
113
  - test/row_converter_test.rb
77
114
  - test/ss2json (1).ods
78
115
  - test/ss2json.ods
@@ -97,12 +134,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
134
  required_rubygems_version: !ruby/object:Gem::Requirement
98
135
  none: false
99
136
  requirements:
100
- - - ">="
137
+ - - ">"
101
138
  - !ruby/object:Gem::Version
102
- hash: 3
139
+ hash: 25
103
140
  segments:
104
- - 0
105
- version: "0"
141
+ - 1
142
+ - 3
143
+ - 1
144
+ version: 1.3.1
106
145
  requirements: []
107
146
 
108
147
  rubyforge_project: ss2json
@@ -112,4 +151,3 @@ specification_version: 3
112
151
  summary: SpreadSheet to Json convert
113
152
  test_files: []
114
153
 
115
- has_rdoc: