yasuri 2.0.12 → 2.0.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a4fed4a13bb125758515e3c0ced665b1ca3d20b6
4
- data.tar.gz: e9dfb2ed6256a367db2e5b6a78d23fa097c422d7
3
+ metadata.gz: 5788b7152b19e7be6d0da7ae376784eeedc52b5f
4
+ data.tar.gz: 40b187d47b1bf468c8d37fe688906a0c7c3f2858
5
5
  SHA512:
6
- metadata.gz: 8b9d6345f3f49b1f7d9445ce18bca736b8cbeedc69979a45d541b59af4e09092d7c1d12886801a24296e9e3d73f39a7c2d53a7c2de12e1a0ff890623b47cfe84
7
- data.tar.gz: 6d755f266062052dd5244599deefefea85f7570c827a898e48eee22c44510dde287b0554ed2cae85e3b94b44fe4eb6f74b512c44047e6cc1bb43fe27a93143b0
6
+ metadata.gz: a338eecbdb1412b8da0f7201fc333b30aaab59e9065f6b92eef1c0d542705fa8a536907e9084809f2a7a2156d39462e4f397ac0e4e9352b94d9cef9617bab6b2
7
+ data.tar.gz: 99a7ecad43ba424566ae4a3635d03dab35f9224de800a3f865c01f8075c91ba5db1451f8481b35e397ab4877c790da3ecea6fce646e2981190a86835f0abc3ca
data/.gitignore CHANGED
@@ -66,5 +66,4 @@ tramp
66
66
  # cask packages
67
67
  .cask/
68
68
 
69
- .ruby-version
70
- Gemfile.lock
69
+ Gemfile.lock
@@ -0,0 +1 @@
1
+ 2.3.2
@@ -1,9 +1,7 @@
1
1
  language: ruby
2
- rvm:
3
- - 2.2.0
4
2
  script:
5
3
  - ruby --version
6
4
  - rspec spec
7
5
  addons:
8
6
  code_climate:
9
- repo_token: 0dc78d33107a7f11f257c0218ac1a37e0073005bb9734f2fd61d0f7e803fc151
7
+ repo_token: 0dc78d33107a7f11f257c0218ac1a37e0073005bb9734f2fd61d0f7e803fc151
data/README.md CHANGED
@@ -52,6 +52,23 @@ root = Yasuri.links_root '//*[@id="menu"]/ul/li/a' do
52
52
  text_content '//*[@id="contents"]/p[1]'
53
53
  end
54
54
 
55
+
56
+ # Node tree constructing by YAML
57
+ src = <<-EOYAML
58
+ root:
59
+ node: links
60
+ path: "//*[@id='menu']/ul/li/a"
61
+ children:
62
+ - title:
63
+ node: text
64
+ path: "//*[@id='contents']/h2"
65
+ - content:
66
+ node: text
67
+ path: "//*[@id='contents']/p[1]"
68
+ EOYAML
69
+ root = Yasuri.yaml2tree(src)
70
+
71
+
55
72
  # Node tree constructing by JSON
56
73
  src = <<-EOJSON
57
74
  { "node" : "links",
@@ -67,7 +67,7 @@ page = agent.get(uri)
67
67
  tree.inject(agent, page)
68
68
  ```
69
69
 
70
- ツリーは、DSLまたはjsonで定義することができます.上の例ではDSLで定義しています.
70
+ ツリーは、json,yaml,またはDSLで定義することができます.上の例ではDSLで定義しています.
71
71
  以下は、jsonで上記と等価な解析ツリーを定義した例です.
72
72
 
73
73
  ```ruby
@@ -87,6 +87,19 @@ EOJSON
87
87
  tree = Yasuri.json2tree(src)
88
88
  ```
89
89
 
90
+ ```ruby
91
+ # yaml で構成する場合
92
+ src = <<-EOYAML
93
+ title:
94
+ node: links
95
+ path: "/html/body/a"
96
+ children:
97
+ - name:
98
+ node: text
99
+ path: "/html/body/p"
100
+ EOYAML
101
+ tree = Yasuri.yaml2tree(src)
102
+ ```
90
103
 
91
104
  ### Node
92
105
  ツリーは入れ子になった *Node* で構成されます.
data/USAGE.md CHANGED
@@ -69,7 +69,7 @@ page = agent.get(uri)
69
69
  tree.inject(agent, page)
70
70
  ```
71
71
 
72
- Tree is definable by 2(+1) ways, DSL and json (and basic ruby code). In above example, DSL.
72
+ Tree is definable by 3(+1) ways, json, yaml, and DSL (or basic ruby code). In above example, DSL.
73
73
 
74
74
  ```ruby
75
75
  # Construct by json.
@@ -88,6 +88,21 @@ EOJSON
88
88
  tree = Yasuri.json2tree(src)
89
89
  ```
90
90
 
91
+ ```ruby
92
+ # Construct by yaml.
93
+ src = <<-EOYAML
94
+ title:
95
+ node: links
96
+ path: "/html/body/a"
97
+ children:
98
+ - name:
99
+ node: text
100
+ path: "/html/body/p"
101
+ EOYAML
102
+ tree = Yasuri.yaml2tree(src)
103
+ ```
104
+
105
+
91
106
  ### Node
92
107
  Tree is constructed by nested Nodes.
93
108
  Node has `Type`, `Name`, `Path`, `Childlen`, and `Options`.
@@ -1,3 +1,3 @@
1
1
  module Yasuri
2
- VERSION = "2.0.12"
2
+ VERSION = "2.0.13"
3
3
  end
@@ -4,6 +4,7 @@
4
4
 
5
5
  require 'mechanize'
6
6
  require 'json'
7
+ require 'yaml'
7
8
 
8
9
  require_relative 'yasuri_node'
9
10
  require_relative 'yasuri_text_node'
@@ -23,6 +24,36 @@ module Yasuri
23
24
  Yasuri.node2hash(node).to_json
24
25
  end
25
26
 
27
+ def self.yaml2tree(yaml_string)
28
+ raise RuntimeError if yaml_string.nil? or yaml_string.empty?
29
+
30
+ yaml = YAML.load(yaml_string)
31
+ raise RuntimeError if yaml.keys.size < 1
32
+
33
+ root_key, root = yaml.keys.first, yaml.values.first
34
+ hash = Yasuri.yaml2tree_sub(root_key, root)
35
+
36
+ Yasuri.hash2node(hash)
37
+ end
38
+
39
+ private
40
+ def self.yaml2tree_sub(name, body)
41
+ return nil if name.nil? or body.nil?
42
+
43
+ new_body = Hash[:name, name]
44
+ body.each{|k,v| new_body[k.to_sym] = v}
45
+ body = new_body
46
+
47
+ return body if body[:children].nil?
48
+
49
+ body[:children] = body[:children].map do |c|
50
+ k, b = c.keys.first, c.values.first
51
+ Yasuri.yaml2tree_sub(k, b)
52
+ end
53
+
54
+ body
55
+ end
56
+
26
57
  def self.method_missing(name, *args, &block)
27
58
  generated = Yasuri::NodeGenerator.gen(name, *args, &block)
28
59
  generated || super(name, args)
@@ -44,9 +75,9 @@ module Yasuri
44
75
  end
45
76
  children ||= []
46
77
 
47
- fail "Not found 'node' value in json" if node.nil?
48
- fail "Not found 'name' value in json" if name.nil?
49
- fail "Not found 'path' value in json" if path.nil?
78
+ fail "Not found 'node' value in map" if node.nil?
79
+ fail "Not found 'name' value in map" if name.nil?
80
+ fail "Not found 'path' value in map" if path.nil?
50
81
 
51
82
  childnodes = children.map{|c| Yasuri.hash2node(c) }
52
83
  ReservedKeys.each{|key| node_h.delete(key)}
@@ -13,6 +13,89 @@ describe 'Yasuri' do
13
13
  @index_page = @agent.get(@uri)
14
14
  end
15
15
 
16
+ ############
17
+ # yam2tree #
18
+ ############
19
+ describe '.yaml2tree' do
20
+ it "fail if empty yaml" do
21
+ expect { Yasuri.yaml2tree(nil) }.to raise_error(RuntimeError)
22
+ end
23
+
24
+ it "return text node" do
25
+ src = <<-EOB
26
+ content:
27
+ node: text
28
+ path: "/html/body/p[1]"
29
+ EOB
30
+ generated = Yasuri.yaml2tree(src)
31
+ original = Yasuri::TextNode.new('/html/body/p[1]', "content")
32
+
33
+ compare_generated_vs_original(generated, original, @index_page)
34
+ end
35
+
36
+ it "return text node as symbol" do
37
+ src = <<-EOB
38
+ :content:
39
+ :node: text
40
+ :path: "/html/body/p[1]"
41
+ EOB
42
+ generated = Yasuri.yaml2tree(src)
43
+ original = Yasuri::TextNode.new('/html/body/p[1]', "content")
44
+
45
+ compare_generated_vs_original(generated, original, @index_page)
46
+ end
47
+
48
+ it "return LinksNode/TextNode" do
49
+
50
+ src = <<-EOB
51
+ root:
52
+ node: links
53
+ path: "/html/body/a"
54
+ children:
55
+ - content:
56
+ node: text
57
+ path: "/html/body/p"
58
+ EOB
59
+ generated = Yasuri.yaml2tree(src)
60
+ original = Yasuri::LinksNode.new('/html/body/a', "root", [
61
+ Yasuri::TextNode.new('/html/body/p', "content"),
62
+ ])
63
+
64
+ compare_generated_vs_original(generated, original, @index_page)
65
+ end
66
+
67
+ it "return StructNode/StructNode/[TextNode,TextNode]" do
68
+ src = <<-EOB
69
+ tables:
70
+ node: struct
71
+ path: "/html/body/table"
72
+ children:
73
+ - table:
74
+ node: struct
75
+ path: "./tr"
76
+ children:
77
+ - title:
78
+ node: text
79
+ path: "./td[1]"
80
+ - pub_date:
81
+ node: text
82
+ path: "./td[2]"
83
+ EOB
84
+
85
+ generated = Yasuri.yaml2tree(src)
86
+ original = Yasuri::StructNode.new('/html/body/table', "tables", [
87
+ Yasuri::StructNode.new('./tr', "table", [
88
+ Yasuri::TextNode.new('./td[1]', "title"),
89
+ Yasuri::TextNode.new('./td[2]', "pub_date"),
90
+ ])
91
+ ])
92
+ page = @agent.get(@uri + "/struct/structual_text.html")
93
+ compare_generated_vs_original(generated, original, page)
94
+ end
95
+
96
+ end # end of describe '.yaml2tree'
97
+
98
+
16
99
  #############
17
100
  # json2tree #
18
101
  #############
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.7"
22
- spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "fuubar"
25
25
  spec.add_development_dependency "glint"
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yasuri
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.12
4
+ version: 2.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - TAC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-15 00:00:00.000000000 Z
11
+ date: 2017-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.7'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.7'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -146,6 +146,7 @@ files:
146
146
  - ".coveralls.yml"
147
147
  - ".gitignore"
148
148
  - ".rspec"
149
+ - ".ruby-version"
149
150
  - ".travis.yml"
150
151
  - Gemfile
151
152
  - LICENSE