data-converter 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ab86a4fc881ba1446b4bb1391d6e0f3776a6e75
4
+ data.tar.gz: e14ecc460c9f2dde92f29ff1623e45c8ecf6ed5e
5
+ SHA512:
6
+ metadata.gz: 8bd5f20a1e14effd29ee387639c25108056164cb146adcace677a6035b5d75180025f803c8a23c3d5fa5fa78af117a4f65382d0038cf14fb450e907b9892f7bd
7
+ data.tar.gz: 6852ffd724bd2f8bdff35a51ebf5a556bcd2e6122fa65404ad34daa26f79403d9f487f44d745b510027fe5a6011047b07c70057e6c62bc02e13c06ec02a7866e
@@ -0,0 +1,35 @@
1
+ # data-converter
2
+
3
+ DataConverter convert Array and Hash into HTML.
4
+
5
+ ## Installation
6
+
7
+ add this line to your Gemfile.
8
+
9
+ `gem 'data-converter'`
10
+
11
+ or
12
+
13
+ `$ gem install data-converter`
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require 'data-converter'
19
+
20
+ DataConverter.parse([0,1,2,3]) #=> "<ul><li>0</li><li>1</li><li>2</li></ul>"
21
+ DataConverter.parse(:foo => "bar") #=> "<dl><dt>foo</dt><dd>bar</dd></dl>"
22
+ ```
23
+
24
+ ## Contributing to Botch
25
+
26
+ 1. fork the project.
27
+ 2. create your feature branch. (`git checkout -b my-feature`)
28
+ 3. commit your changes. (`git commit -am 'commit message.'`)
29
+ 4. push to the branch. (`git push origin my-feature`)
30
+ 5. send pull request.
31
+
32
+ ## License
33
+
34
+ MIT
35
+
@@ -1,17 +1,16 @@
1
- Gem::Specification.new do | spec |
2
- spec.name = "data-converter"
3
- spec.version = "0.1.6"
4
- spec.summary = "It is under the devlopment."
5
- spec.description = "Convert hash and array into HTML Element (Array => ul, Hash => dl)."
6
- spec.homepage = "https://github.com/namusyaka/data-converter"
7
- spec.author = "namusyaka"
8
- spec.email = "namusyaka@gmail.com"
9
- spec.files = %w(
10
- data-converter.gemspec
11
- README.rdoc
12
- lib
13
- test
14
- lib/data-converter.rb
15
- test/test.rb
16
- )
1
+ require File.expand_path("../lib/version", __FILE__)
2
+
3
+ Gem::Specification.new "data-converter", DataConverter::VERSION do |s|
4
+ s.summary = "Convert hash and array into HTML Element."
5
+ s.description = "Convert hash and array into HTML Element."
6
+ s.authors = ["namusyaka"]
7
+ s.email = "namusyaka@gmail.com"
8
+ s.homepage = "https://github.com/namusyaka/data-converter"
9
+ s.files = `git ls-files`.split("\n") - %w(.gitignore)
10
+ s.test_files = s.files.select { |path| path =~ /^spec\/.*_spec\.rb/ }
11
+ s.license = "MIT"
12
+
13
+ s.add_development_dependency "rake"
14
+ s.add_development_dependency "rspec"
15
+ s.add_development_dependency "rspec-html-matchers"
17
16
  end
@@ -1,12 +1,15 @@
1
- class String
2
- def escapeHTML
3
- self.gsub(/&/n, '&amp;').gsub(/\"/n, '&quot;').gsub(/>/n, '&gt;').gsub(/</n, '&lt;').gsub(/'/, '&#039;')
4
- end
5
- end
6
-
7
1
  class DataConverter
8
2
  attr_reader :data
9
3
 
4
+ ESCAPE_VALUES = {
5
+ "&" => "&amp;",
6
+ "<" => "&lt;",
7
+ ">" => "&gt;",
8
+ '"' => "&quot;"
9
+ }
10
+
11
+ ESCAPE_REGEXP = Regexp.union(*ESCAPE_VALUES.keys)
12
+
10
13
  def initialize(data)
11
14
  @data = data
12
15
  end
@@ -21,20 +24,12 @@ class DataConverter
21
24
 
22
25
  case data
23
26
  when Array
24
- list = <<-UL
25
- <ul>
26
- #{ convert_to_li(data).join("\n") }
27
- </ul>
28
- UL
27
+ tag(:ul, convert_to_li(data), :escaped => true)
29
28
  when Hash
30
- list = <<-DL
31
- <dl>
32
- #{ convert_to_dl_childs(data) }</dl>
33
- DL
29
+ tag(:dl, convert_to_dl_childs(data), :escaped => true)
34
30
  else
35
- return data
31
+ data
36
32
  end
37
- list
38
33
  end
39
34
 
40
35
  private
@@ -44,25 +39,34 @@ class DataConverter
44
39
  end
45
40
 
46
41
  def convert_to_li(data)
47
- data.map! do | e |
48
- if check_data?(e)
49
- "<li>#{ convert(e) }</li>"
42
+ data.map do |caption|
43
+ if check_data?(caption)
44
+ tag(:li, convert(caption), :escaped => true)
50
45
  else
51
- "<li>#{ e.to_s.escapeHTML }</li>"
46
+ tag(:li, caption)
52
47
  end
53
- end
48
+ end.join("\n")
54
49
  end
55
50
 
56
51
  def convert_to_dl_childs(data)
57
- childs = ""
52
+ childs = []
58
53
  data.each_pair do | key, value |
59
- childs += "<dt>#{ key.to_s.escapeHTML }</dt>\n"
54
+ childs << tag(:dt, key)
60
55
  if check_data?(value)
61
- childs += "<dd>#{ convert(value) }</dd>\n"
56
+ childs << tag(:dd, convert(value), :escaped => true)
62
57
  else
63
- childs += "<dd>#{ value.to_s.escapeHTML }</dd>\n"
58
+ childs << tag(:dd, value)
64
59
  end
65
60
  end
66
- childs
61
+ childs.join("\n")
62
+ end
63
+
64
+ def tag(name, caption, options = {})
65
+ caption = caption.to_s
66
+ "<#{name}>#{options[:escaped] ? caption : escape_html(caption)}</#{name}>"
67
+ end
68
+
69
+ def escape_html(str)
70
+ str.to_s.gsub(ESCAPE_REGEXP) { |char| ESCAPE_VALUES[char] }
67
71
  end
68
72
  end
@@ -0,0 +1,3 @@
1
+ class DataConverter
2
+ VERSION = '0.1.7'
3
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path("../../lib/data-converter", __FILE__)
2
+ require 'rubygems'
3
+ require 'rspec-html-matchers'
4
+ require 'rspec'
5
+
6
+ describe DataConverter do
7
+ before(:all) do
8
+ @array_data = [0, 1, 2, ["string", DataConverter, :symbol, 0.1]]
9
+ @hash_data = { :foo => ["bar", "baz"], :hoge => { :fuga => :symbol }, :const => DataConverter, :float => 0.1 }
10
+ end
11
+
12
+ it "should support array." do
13
+ actual_html = DataConverter.parse(@array_data)
14
+ expect(actual_html).to have_tag('ul') do
15
+ with_tag 'li', :text => 0
16
+ with_tag 'li', :text => 1
17
+ with_tag 'li', :text => 2
18
+ with_tag 'li > ul'
19
+ with_tag 'li > ul li', :text => "string"
20
+ with_tag 'li > ul li', :text => "DataConverter"
21
+ with_tag 'li > ul li', :text => "symbol"
22
+ with_tag 'li > ul li', :text => "0.1"
23
+ end
24
+ end
25
+
26
+ it "should support hash." do
27
+ actual_html = DataConverter.parse(@hash_data)
28
+ expect(actual_html).to have_tag('dl') do
29
+ with_tag 'dt', :text => 'foo'
30
+ with_tag 'dt', :text => 'hoge'
31
+ with_tag 'dt', :text => 'const'
32
+ with_tag 'dt', :text => 'float'
33
+ with_tag 'dd > ul'
34
+ with_tag 'dd > ul > li', :text => 'bar'
35
+ with_tag 'dd > ul > li', :text => 'baz'
36
+ with_tag 'dd > dl'
37
+ with_tag 'dd > dl > dt', :text => 'fuga'
38
+ with_tag 'dd > dl > dd', :text => 'symbol'
39
+ with_tag 'dd', :text => "DataConverter"
40
+ with_tag 'dd', :text => '0.1'
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -1,68 +1,91 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: data-converter
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 6
10
- version: 0.1.6
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - namusyaka
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2012-09-29 00:00:00 Z
19
- dependencies: []
20
-
21
- description: Convert hash and array into HTML Element (Array => ul, Hash => dl).
11
+ date: 2013-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
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: rspec-html-matchers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Convert hash and array into HTML Element.
22
56
  email: namusyaka@gmail.com
23
57
  executables: []
24
-
25
58
  extensions: []
26
-
27
59
  extra_rdoc_files: []
28
-
29
- files:
60
+ files:
61
+ - README.md
30
62
  - data-converter.gemspec
31
- - README.rdoc
32
63
  - lib/data-converter.rb
33
- - test/test.rb
64
+ - lib/version.rb
65
+ - test/data_converter_spec.rb
34
66
  homepage: https://github.com/namusyaka/data-converter
35
- licenses: []
36
-
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
37
70
  post_install_message:
38
71
  rdoc_options: []
39
-
40
- require_paths:
72
+ require_paths:
41
73
  - lib
42
- required_ruby_version: !ruby/object:Gem::Requirement
43
- none: false
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- hash: 3
48
- segments:
49
- - 0
50
- version: "0"
51
- required_rubygems_version: !ruby/object:Gem::Requirement
52
- none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
60
84
  requirements: []
61
-
62
85
  rubyforge_project:
63
- rubygems_version: 1.8.1
86
+ rubygems_version: 2.0.2
64
87
  signing_key:
65
- specification_version: 3
66
- summary: It is under the devlopment.
88
+ specification_version: 4
89
+ summary: Convert hash and array into HTML Element.
67
90
  test_files: []
68
-
91
+ has_rdoc:
@@ -1,24 +0,0 @@
1
- =data-converter
2
-
3
-
4
- ==Install
5
- gem install data-converter
6
-
7
- ==Description
8
- Convert hash and array into HTML Element (Array => ul, Hash => dl).
9
- It's under the development.
10
-
11
- ==Code
12
- require 'rubygems'
13
- require 'data-converter'
14
-
15
- puts DataConverter.parse({
16
- :a => "b",
17
- :c => { :d => ["<>&\"'", 1, 2] }
18
- })
19
-
20
- ==License
21
- the MIT License
22
-
23
- ==Author
24
- namusyaka
@@ -1,59 +0,0 @@
1
- require 'test/unit'
2
- require File.expand_path('../../lib/data-converter', __FILE__)
3
-
4
- class Test_DataConverter < Test::Unit::TestCase
5
- HASH_RESULT = <<-EOS
6
- <dl>
7
- <dt>a</dt>
8
- <dd><dl>
9
- <dt>b</dt>
10
- <dd><dl>
11
- <dt>c</dt>
12
- <dd><ul>
13
- <li>0</li>
14
- <li>1</li>
15
- <li>2</li>
16
- <li>3</li>
17
- </ul>
18
- </dd>
19
- </dl>
20
- </dd>
21
- </dl>
22
- </dd>
23
- </dl>
24
- EOS
25
-
26
- ARRAY_RESULT = <<-EOS
27
- <ul>
28
- <li><dl>
29
- <dt>f</dt>
30
- <dd><ul>
31
- <li>4</li>
32
- <li>5</li>
33
- <li>6</li>
34
- <li>7</li>
35
- </ul>
36
- </dd>
37
- </dl>
38
- </li>
39
- </ul>
40
- EOS
41
-
42
- def setup
43
- @hash_data = { :a => { :b => { :c => [0, 1, 2, 3] } } }
44
- @array_data = [{ :f => [4, 5, 6, 7] }]
45
- @class = DataConverter
46
- end
47
-
48
- def test_array_parse
49
- assert_equal(ARRAY_RESULT, @class.parse(@array_data))
50
- end
51
-
52
- def test_hash_parse
53
- assert_equal(HASH_RESULT, @class.parse(@hash_data))
54
- end
55
-
56
- def test_other_approach
57
- assert_equal(HASH_RESULT, DataConverter.new(@hash_data).convert)
58
- end
59
- end