jsoner 0.0.1.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OTZkOTEzMGYxM2QzMzFkNzdjMjBmZjU2OGZmODdkYzc2YzAwZTBiOQ==
5
+ data.tar.gz: !binary |-
6
+ MWI5NWFiNTk2NTdkM2NmZjFlOGY1NzAzNWMwZDRiZjk1NTIzN2U0ZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ZGY0NzFiMGQ5OWIxOGNiYTY4ZTBkNTg5OThiOTAzNGY3MjY3ZmJkYzRlZmJk
10
+ NmQyNmFlNDc5ZWQwYjQ3ZjI5YzlmNDAzY2IzNGU5MTI3YTJjYmIwYjNkYjE4
11
+ Mzk3ZTUyYzAwNjg1MmUxZTExNjBiYTE1NTRlNTVkNGIyOWQ3NjY=
12
+ data.tar.gz: !binary |-
13
+ MTc4MjE3NTFkYWNiODkwMTUxZGYyMDA1YzYxYzExMjRiMTBmNWY2NzkzNDkx
14
+ NDkwMmZmNzcxMjViYTk1ZmFjMzViYzhlNmZlN2I5MjhmNWMwY2E4YWRiYWQ4
15
+ ZmM1MTkyNzI3MDY0YTAyMzA4MjE4MTFkYjU5NjgzN2EzYjU2YWM=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,12 @@
1
+ rvm:
2
+ - 1.9.3
3
+
4
+ script: "bundle exec rspec spec/"
5
+
6
+ notifications:
7
+ email:
8
+ - simlegate@163.com
9
+
10
+ branches:
11
+ only:
12
+ - master
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 simlegate
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.
@@ -0,0 +1,75 @@
1
+ # Jsoner
2
+
3
+ Serialize HTML tables into JSON in Ruby.
4
+ [![Build Status](https://travis-ci.org/simlegate/jsoner.png?branch=master)](https://travis-ci.org/simlegate/jsoner)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'jsoner'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install jsoner
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ html = <<-eohtml
24
+ <table id='example-table'>
25
+ <thead>
26
+ <tr>
27
+ <th>First Name</th>
28
+ <th>Last Name</th>
29
+ <th>Points</th></tr>
30
+ </thead>
31
+ <tbody>
32
+ <tr>
33
+ <td>Jill</td>
34
+ <td>Smith</td>
35
+ <td>50</td></tr>
36
+ <tr>
37
+ <td>Eve</td>
38
+ <td>Jackson</td>
39
+ <td>94</td></tr>
40
+ <tr>
41
+ <td>John</td>
42
+ <td>Doe</td>
43
+ <td>80</td></tr>
44
+ <tr>
45
+ <td>Adam</td>
46
+ <td>Johnson</td>
47
+ <td>67</td></tr>
48
+ </tbody>
49
+ </table>
50
+ eohtml
51
+
52
+ # Convert HTML table into Json
53
+
54
+ json = Jsoner.parse(html)
55
+
56
+ # output json =>
57
+ #
58
+ # [ {"First Name"=>"Jill", "Last Name"=>"Smith", "Points"=>"50"},
59
+ # {"First Name"=>"Eve", "Last Name"=>"Jackson", "Points"=>"94"},
60
+ # {"First Name"=>"John", "Last Name"=>"Doe", "Points"=>"80"},
61
+ # {"First Name"=>"Adam", "Last Name"=>"Johnson", "Points"=>"67"} ]
62
+
63
+ ```
64
+
65
+ ## THANKS
66
+
67
+ [table-to-json](https://github.com/lightswitch05/table-to-json) written by [@lightswitch05](https://github.com/lightswitch05) in JavaScript.
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jsoner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsoner"
8
+ spec.version = Jsoner::VERSION
9
+ spec.authors = ["simlegate"]
10
+ spec.email = ["simlegate@163.com"]
11
+ spec.description = %q{Serialize HTML tables into JSON in Ruby}
12
+ spec.summary = %q{Serialize HTML tables into JSON in Ruby}
13
+ spec.homepage = "https://github.com/simlegate/jsoner"
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "nokogiri", "~> 1.6.0"
24
+ spec.add_development_dependency "rspec", "~> 2.14.1"
25
+ end
@@ -0,0 +1,14 @@
1
+ require "jsoner/version"
2
+ require "jsoner/table"
3
+ require "jsoner/table_factory"
4
+
5
+ require 'nokogiri'
6
+
7
+ module Jsoner
8
+ class << self
9
+
10
+ def parse(html)
11
+ Jsoner::Table.new(Jsoner::TableFactory.new(Nokogiri::HTML.parse(html))).to_json
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,48 @@
1
+ require 'json'
2
+
3
+ module Jsoner
4
+ class Table
5
+
6
+ def initialize factory
7
+ @table = factory.create
8
+ end
9
+
10
+ def row_number
11
+ @table[:body].count
12
+ end
13
+
14
+ def to_json
15
+ convert.to_json
16
+ end
17
+
18
+ #
19
+ # convert Hash from factory into anthor Hash will be serialized into JSON
20
+ #
21
+ # Example:
22
+ # table = { :header => ["First Name", "Last Name", "Points"]
23
+ # :body => [["Jill", "Smith", "50"],
24
+ # ["Eve", "Jackson", "94"],
25
+ # ["John", "Doe", "80"],
26
+ # ["Adam", "Johnson", "67"]] }
27
+ #
28
+ # Output:
29
+ # table == [{"First Name"=>"Jill", "Last Name"=>"Smith", "Points"=>"50"},
30
+ # {"First Name"=>"Eve", "Last Name"=>"Jackson", "Points"=>"94"},
31
+ # {"First Name"=>"John", "Last Name"=>"Doe", "Points"=>"80"},
32
+ # {"First Name"=>"Adam", "Last Name"=>"Johnson", "Points"=>"67"}]
33
+ def convert
34
+ (0...row_number).map do |index|
35
+
36
+ #
37
+ # Combine two Arrays into Hash
38
+ # a = ["a", "b", "c"]
39
+ # b = ["d", "e", "f"]
40
+ # Hash[a.zip(b)]
41
+ # => {"a" => "d", "b" => "e", "b" => "f" }
42
+ Hash[@table[:header].zip(@table[:body][index])]
43
+ end
44
+ end
45
+
46
+ private :convert
47
+ end
48
+ end
@@ -0,0 +1,51 @@
1
+ module Jsoner
2
+ #
3
+ # build Hash below from doc parsed by Nokogiki
4
+ # table = { :header => ["First Name", "Last Name", "Score"]
5
+ # :body => [["Jill", "Smith", "50"],
6
+ # ["Eve", "Jackson", "94"],
7
+ # ["John", "Doe", "80"],
8
+ # ["Adam", "Johnson", "67"]
9
+ # ]
10
+ # }
11
+ class TableFactory
12
+
13
+ def initialize doc
14
+ @table_rows = doc.search('tr')
15
+ end
16
+
17
+ def create
18
+ { :header => build_header, :body => build_body }
19
+ end
20
+
21
+ def build_header
22
+ @table_rows[0].search('th').map(&:content)
23
+ end
24
+
25
+ def build_body
26
+ row_number = @table_rows.count - 1
27
+ (1..row_number).map do |row|
28
+ @table_rows[row].search('td').map(&:content)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ #
35
+ # Remove other elements beside table and keep it clean
36
+ #
37
+ # Example:
38
+ # <body>
39
+ # <table>
40
+ # // other elements
41
+ # </table>
42
+ # </body>
43
+ #
44
+ # Output:
45
+ # <table>
46
+ # // other elements
47
+ # </table>
48
+ def remove_housing
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module Jsoner
2
+ VERSION = "0.0.1.rc1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'json'
2
+ def pre_json
3
+ [ {"First Name"=>"Jill", "Last Name"=>"Smith", "Points"=>"50"},
4
+ {"First Name"=>"Eve", "Last Name"=>"Jackson", "Points"=>"94"},
5
+ {"First Name"=>"John", "Last Name"=>"Doe", "Points"=>"80"},
6
+ {"First Name"=>"Adam", "Last Name"=>"Johnson", "Points"=>"67"} ]
7
+ end
8
+
9
+ def json
10
+ pre_json.to_json
11
+ end
@@ -0,0 +1,30 @@
1
+ def table_str
2
+ <<-eohtml
3
+ <table id='example-table'>
4
+ <thead>
5
+ <tr>
6
+ <th>First Name</th>
7
+ <th>Last Name</th>
8
+ <th data-override="Score">Points</th></tr>
9
+ </thead>
10
+ <tbody>
11
+ <tr>
12
+ <td>Jill</td>
13
+ <td>Smith</td>
14
+ <td data-override="disqualified">50</td></tr>
15
+ <tr>
16
+ <td>Eve</td>
17
+ <td>Jackson</td>
18
+ <td>94</td></tr>
19
+ <tr>
20
+ <td>John</td>
21
+ <td>Doe</td>
22
+ <td>80</td></tr>
23
+ <tr>
24
+ <td>Adam</td>
25
+ <td>Johnson</td>
26
+ <td>67</td></tr>
27
+ </tbody>
28
+ </table>
29
+ eohtml
30
+ end
@@ -0,0 +1,54 @@
1
+ require "#{File.dirname(__FILE__)}/../fixtures/table"
2
+ require "#{File.dirname(__FILE__)}/../../lib/jsoner"
3
+
4
+ describe 'build Hash below from doc parsed by Nokogiki' do
5
+
6
+ before :each do
7
+ @obj = Nokogiri::HTML.parse(table_str)
8
+ @factory = Jsoner::TableFactory.new(@obj)
9
+ end
10
+
11
+ it "@table_rows should be instance of NodeSet" do
12
+ @factory.instance_variable_get(:@table_rows).should be_instance_of Nokogiri::XML::NodeSet
13
+ end
14
+
15
+ context "Building header" do
16
+ it "should return Array" do
17
+ @factory.build_header.should be_instance_of Array
18
+ end
19
+
20
+ it "should match data from fixtures" do
21
+ @factory.build_header.should == ["First Name", "Last Name", "Points"]
22
+ end
23
+ end
24
+
25
+ context "Building body" do
26
+ it "should return Double Dimensional Array" do
27
+ @factory.build_body.should be_instance_of Array
28
+ @factory.build_body.each{ |column| column.should be_instance_of Array }
29
+ end
30
+ end
31
+
32
+ context "Building full hash defined table" do
33
+ it "should have _header_ key" do
34
+ @factory.create.should have_key :header
35
+ end
36
+
37
+ it "should have _body_ key" do
38
+ @factory.create.should have_key :header
39
+ end
40
+
41
+ it "data mapped by _header_ key should match fixtures" do
42
+ @factory.create[:header].should == ["First Name", "Last Name", "Points"]
43
+ end
44
+
45
+ it "data mapped by _body_ key should match fixtures" do
46
+ @factory.create[:body].should == [["Jill", "Smith", "50"],
47
+ ["Eve", "Jackson", "94"],
48
+ ["John", "Doe", "80"],
49
+ ["Adam", "Johnson", "67"]]
50
+ end
51
+
52
+ # TODO testing when having no header in table
53
+ end
54
+ end
@@ -0,0 +1,41 @@
1
+ Dir["#{File.dirname(__FILE__)}/fixtures/*.rb"].each {|file| require file }
2
+ require "#{File.dirname(__FILE__)}/../../lib/jsoner"
3
+
4
+ describe "parse structure of table" do
5
+
6
+ before :each do
7
+ obj = Nokogiri::HTML.parse(table_str)
8
+ factory = Jsoner::TableFactory.new(obj)
9
+ @table = Jsoner::Table.new(factory)
10
+ end
11
+
12
+ context "the number of row beside header in table" do
13
+ it "should == 4 " do
14
+ @table.row_number.should == 4
15
+ end
16
+ end
17
+
18
+ context "convert Hash from factory" do
19
+ it "_convert_ should private method" do
20
+ expect{ @table.convert }.to raise_error NoMethodError
21
+ end
22
+
23
+ it "should be instance of Array" do
24
+ @table.send(:convert).should be_instance_of Array
25
+ end
26
+
27
+ it "everyone should be instance of Hash in Array" do
28
+ @table.send(:convert).map{ |h| h.should be_instance_of Hash}
29
+ end
30
+
31
+ it "everyone should have keys including _First Name_,_Last Name_,_Score_ in Array" do
32
+ @table.send(:convert).map{ |h| h.should have_key "First Name"}
33
+ @table.send(:convert).map{ |h| h.should have_key "Last Name"}
34
+ @table.send(:convert).map{ |h| h.should have_key "Points"}
35
+ end
36
+
37
+ it "match full data" do
38
+ @table.send(:convert).should == pre_json
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,9 @@
1
+ Dir["#{File.dirname(__FILE__)}/fixtures/*.rb"].each {|file| require file }
2
+ require "#{File.dirname(__FILE__)}/../lib/jsoner"
3
+
4
+ describe Jsoner do
5
+
6
+ it "should match data from json of fixtures" do
7
+ Jsoner.parse(table_str).should == json
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsoner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1.rc1
5
+ platform: ruby
6
+ authors:
7
+ - simlegate
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: nokogiri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.6.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.6.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.1
69
+ description: Serialize HTML tables into JSON in Ruby
70
+ email:
71
+ - simlegate@163.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - jsoner.gemspec
83
+ - lib/jsoner.rb
84
+ - lib/jsoner/table.rb
85
+ - lib/jsoner/table_factory.rb
86
+ - lib/jsoner/version.rb
87
+ - spec/fixtures/json.rb
88
+ - spec/fixtures/table.rb
89
+ - spec/jsoner/table_factory_spec.rb
90
+ - spec/jsoner/table_spec.rb
91
+ - spec/jsoner_spec.rb
92
+ homepage: https://github.com/simlegate/jsoner
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>'
108
+ - !ruby/object:Gem::Version
109
+ version: 1.3.1
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.6
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Serialize HTML tables into JSON in Ruby
116
+ test_files:
117
+ - spec/fixtures/json.rb
118
+ - spec/fixtures/table.rb
119
+ - spec/jsoner/table_factory_spec.rb
120
+ - spec/jsoner/table_spec.rb
121
+ - spec/jsoner_spec.rb