ryoba 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +11 -0
- data/README.md +9 -3
- data/lib/ryoba.rb +1 -0
- data/lib/ryoba/error.rb +0 -0
- data/lib/ryoba/nokogiri/xml/document.rb +19 -0
- data/lib/ryoba/nokogiri/xml/node.rb +71 -19
- data/lib/ryoba/nokogiri/xml/searchable.rb +72 -6
- data/lib/ryoba/version.rb +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 16fd5950cac40b739a0578ed3af49099f4dc4866ea1e52b1e11b2120b4901320
|
4
|
+
data.tar.gz: 4899e26d14262f21ce419615ac1a574f9a6355fd24c7bf42c6b96044000a701b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae9ff3dca31fad1510852a3af7270bab7641957f7a6b0f0fd54a2eafda9ecd062ca16fe3974eb791e4ef10cf74f162e81b4abc6d20a87637ca796d15ddf67b1f
|
7
|
+
data.tar.gz: 56298bda462fe9c8fe5bd6f4066bf7d83c89d9a806c2732e06915ff98d26a40743fa6528f39c5ed4a918dfe2674a2eb185b00e0378d49d644c54bcabea7f2140
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -5,11 +5,17 @@
|
|
5
5
|
|
6
6
|
## API
|
7
7
|
|
8
|
-
- Nokogiri::XML::
|
8
|
+
- [Nokogiri::XML::Document](https://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Document)
|
9
|
+
- [#uri](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Document:uri)
|
10
|
+
- [Nokogiri::XML::Node](https://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node)
|
11
|
+
- [#content!](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node:content%21)
|
12
|
+
- aliased as [#inner_text!](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node:inner_text%21)
|
13
|
+
- aliased as [#text!](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node:text%21)
|
14
|
+
- [#content?](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node:content%3F)
|
15
|
+
- aliased as [#inner_text?](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node:inner_text%3F)
|
9
16
|
- [#matches!](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node:matches%21)
|
10
|
-
- [#text!](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node:text%21)
|
11
17
|
- [#uri](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Node:uri)
|
12
|
-
- Nokogiri::XML::Searchable
|
18
|
+
- [Nokogiri::XML::Searchable](https://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Searchable)
|
13
19
|
- [#ancestor](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Searchable:ancestor)
|
14
20
|
- [#ancestor!](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Searchable:ancestor%21)
|
15
21
|
- [#ancestors!](http://www.rubydoc.info/gems/ryoba/Nokogiri/XML/Searchable:ancestors%21)
|
data/lib/ryoba.rb
CHANGED
data/lib/ryoba/error.rb
CHANGED
File without changes
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "uri"
|
2
|
+
|
3
|
+
class Nokogiri::XML::Document
|
4
|
+
|
5
|
+
# Constructs a URI from the document's +url+ if +url+ is not nil.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# xml = Nokogiri::XML(<<-XML, "https://www.example.com/foo")
|
9
|
+
# <body>FOO</body>
|
10
|
+
# XML
|
11
|
+
#
|
12
|
+
# xml.uri # == URI("https://www.example.com/foo")
|
13
|
+
#
|
14
|
+
# @return [URI, nil]
|
15
|
+
def uri
|
16
|
+
URI(url) if url
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -2,25 +2,79 @@ require "uri"
|
|
2
2
|
|
3
3
|
class Nokogiri::XML::Node
|
4
4
|
|
5
|
-
# Equivalent to +.
|
5
|
+
# Equivalent to +.content.strip+, but returns nil if the result is an
|
6
6
|
# empty string.
|
7
7
|
#
|
8
|
+
# @example
|
9
|
+
# xml = Nokogiri::XML(<<-XML)
|
10
|
+
# <body>
|
11
|
+
# <h1>
|
12
|
+
# Headline 1
|
13
|
+
# </h1>
|
14
|
+
# <h2> Headline 2 </h2>
|
15
|
+
# <h3> </h3>
|
16
|
+
# </body>
|
17
|
+
# XML
|
18
|
+
#
|
19
|
+
# xml.at("h1").content? # == "Headline 1"
|
20
|
+
# xml.at("h2").content? # == "Headline 2"
|
21
|
+
# xml.at("h3").content? # == nil
|
22
|
+
#
|
23
|
+
# @return [String, nil]
|
24
|
+
def content?
|
25
|
+
result = self.content.strip
|
26
|
+
result unless result.empty?
|
27
|
+
end
|
28
|
+
|
29
|
+
alias_method :inner_text?, :content?
|
30
|
+
|
31
|
+
# Equivalent to +.content.strip+, but raises an error if the result is
|
32
|
+
# an empty string.
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# xml = Nokogiri::XML(<<-XML)
|
36
|
+
# <body>
|
37
|
+
# <h1>
|
38
|
+
# Headline 1
|
39
|
+
# </h1>
|
40
|
+
# <h2> Headline 2 </h2>
|
41
|
+
# <h3> </h3>
|
42
|
+
# </body>
|
43
|
+
# XML
|
44
|
+
#
|
45
|
+
# xml.at("h1").content! # == "Headline 1"
|
46
|
+
# xml.at("h2").content! # == "Headline 2"
|
47
|
+
# xml.at("h3").content! # raise error
|
48
|
+
#
|
8
49
|
# @return [String]
|
9
50
|
# @raise [Ryoba::Error]
|
10
51
|
# if result is an empty string
|
11
|
-
def
|
12
|
-
result = self.
|
52
|
+
def content!
|
53
|
+
result = self.content.strip
|
13
54
|
if result.empty?
|
14
55
|
raise Ryoba::Error.new("No text in:\n#{self.to_html}")
|
15
56
|
end
|
16
57
|
result
|
17
58
|
end
|
18
59
|
|
19
|
-
|
20
|
-
|
60
|
+
alias_method :inner_text!, :content!
|
61
|
+
alias_method :text!, :content!
|
62
|
+
|
63
|
+
# Like +Node#matches?+, but returns the Node if +selector+ matches,
|
64
|
+
# and raises an error otherwise.
|
65
|
+
#
|
66
|
+
# @example
|
67
|
+
# xml = Nokogiri::XML(<<-XML)
|
68
|
+
# <body>
|
69
|
+
# <div id="a" class="c" />
|
70
|
+
# <div id="b" />
|
71
|
+
# </body>
|
72
|
+
# XML
|
73
|
+
#
|
74
|
+
# xml.at("#a").matches(".c")! # == Node div#a.c
|
75
|
+
# xml.at("#b").matches(".c")! # raise error
|
21
76
|
#
|
22
77
|
# @param selector [String]
|
23
|
-
# selector to match
|
24
78
|
# @return [self]
|
25
79
|
# @raise [Ryoba::Error]
|
26
80
|
# if Node does not match +selector+
|
@@ -38,35 +92,33 @@ class Nokogiri::XML::Node
|
|
38
92
|
"form" => "action",
|
39
93
|
}
|
40
94
|
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# {HTML_ELEMENT_URI_ATTRIBUTES}
|
44
|
-
#
|
45
|
-
#
|
95
|
+
# Builds a URI from a specified attribute. If no attribute is
|
96
|
+
# specified, an element-appropriate attribute will be chosen from
|
97
|
+
# {HTML_ELEMENT_URI_ATTRIBUTES}, if possible. Relative URIs will be
|
98
|
+
# converted to absolute URIs using the Node document's +url+, if
|
99
|
+
# possible.
|
46
100
|
#
|
47
101
|
# @example
|
48
|
-
# xml = Nokogiri::XML(<<-XML, "http://localhost/qux")
|
102
|
+
# xml = Nokogiri::XML(<<-XML, "http://localhost/qux/")
|
49
103
|
# <body>
|
50
104
|
# <a href="https://www.example.com/foo">FOO</a>
|
51
|
-
# <
|
105
|
+
# <img src="/bar" />
|
106
|
+
# <div data-src="baz" />
|
52
107
|
# <p>blah</p>
|
53
108
|
# </body>
|
54
109
|
# XML
|
55
110
|
#
|
56
111
|
# xml.at("a").uri # == URI("https://www.example.com/foo")
|
57
|
-
# xml.at("
|
112
|
+
# xml.at("img").uri # == URI("http://localhost/bar")
|
113
|
+
# xml.at("div").uri("data-src") # == URI("http://localhost/qux/baz")
|
58
114
|
# xml.at("p").uri # == nil
|
59
115
|
#
|
60
116
|
# @param attribute_name [String]
|
61
|
-
# name of the attribute to return as a URI
|
62
117
|
# @return [URI, nil]
|
63
118
|
def uri(attribute_name = nil)
|
64
119
|
attribute_name ||= HTML_ELEMENT_URI_ATTRIBUTES[self.name]
|
65
120
|
url = self[attribute_name]
|
66
|
-
|
67
|
-
if url
|
68
|
-
self.document.url ? URI.join(self.document.url, url) : URI(url)
|
69
|
-
end
|
121
|
+
URI.join(*self.document.url, url) if url
|
70
122
|
end
|
71
123
|
|
72
124
|
end
|
@@ -1,10 +1,21 @@
|
|
1
1
|
module Nokogiri::XML::Searchable
|
2
2
|
|
3
|
-
# Like +Searchable#search+, but raises an
|
3
|
+
# Like +Searchable#search+, but raises an error if there are no
|
4
4
|
# results.
|
5
5
|
#
|
6
|
+
# @example
|
7
|
+
# xml = Nokogiri::XML(<<-XML)
|
8
|
+
# <body>
|
9
|
+
# <div id="a" />
|
10
|
+
# <div id="b" />
|
11
|
+
# </body>
|
12
|
+
# XML
|
13
|
+
#
|
14
|
+
# xml.search!("div") # == NodeSet [div#a, div#b]
|
15
|
+
# xml.search!("img") # raise error
|
16
|
+
#
|
6
17
|
# @param queries [Array<String>]
|
7
|
-
# @return [
|
18
|
+
# @return [Nokogiri::XML::NodeSet]
|
8
19
|
# @raise [Ryoba::Error]
|
9
20
|
# if all queries yield no results
|
10
21
|
def search!(*queries)
|
@@ -15,8 +26,18 @@ module Nokogiri::XML::Searchable
|
|
15
26
|
results
|
16
27
|
end
|
17
28
|
|
18
|
-
# Like +Searchable#at+, but raises an
|
19
|
-
#
|
29
|
+
# Like +Searchable#at+, but raises an error if there are no results.
|
30
|
+
#
|
31
|
+
# @example
|
32
|
+
# xml = Nokogiri::XML(<<-XML)
|
33
|
+
# <body>
|
34
|
+
# <div id="a" />
|
35
|
+
# <div id="b" />
|
36
|
+
# </body>
|
37
|
+
# XML
|
38
|
+
#
|
39
|
+
# xml.at!("div") # == Node div#a
|
40
|
+
# xml.at!("img") # raise error
|
20
41
|
#
|
21
42
|
# @param queries [Array<String>]
|
22
43
|
# @return [Nokogiri::XML::Element]
|
@@ -30,11 +51,26 @@ module Nokogiri::XML::Searchable
|
|
30
51
|
result
|
31
52
|
end
|
32
53
|
|
33
|
-
# Like +Searchable#ancestors+, but raises an
|
54
|
+
# Like +Searchable#ancestors+, but raises an error if there are no
|
34
55
|
# matching ancestors.
|
35
56
|
#
|
57
|
+
# @example
|
58
|
+
# xml = Nokogiri::XML(<<-XML)
|
59
|
+
# <body>
|
60
|
+
# <div id="a">
|
61
|
+
# <div id="b">
|
62
|
+
# <img src="cat.jpg">
|
63
|
+
# </div>
|
64
|
+
# </div>
|
65
|
+
# </body>
|
66
|
+
# XML
|
67
|
+
#
|
68
|
+
# xml.at("img").ancestors!("div") # == NodeSet [div#b, div#a]
|
69
|
+
# xml.at("img").ancestors!("#a") # == NodeSet [div#a]
|
70
|
+
# xml.at("img").ancestors!("#z") # raise error
|
71
|
+
#
|
36
72
|
# @param selector [String]
|
37
|
-
# @return [
|
73
|
+
# @return [Nokogiri::XML::NodeSet]
|
38
74
|
# @raise [Ryoba::Error]
|
39
75
|
# if no ancestors match +selector+
|
40
76
|
def ancestors!(selector = nil)
|
@@ -48,6 +84,21 @@ module Nokogiri::XML::Searchable
|
|
48
84
|
# Like +Searchable#ancestors+, but returns only the first matching
|
49
85
|
# ancestor.
|
50
86
|
#
|
87
|
+
# @example
|
88
|
+
# xml = Nokogiri::XML(<<-XML)
|
89
|
+
# <body>
|
90
|
+
# <div id="a">
|
91
|
+
# <div id="b">
|
92
|
+
# <img src="cat.jpg">
|
93
|
+
# </div>
|
94
|
+
# </div>
|
95
|
+
# </body>
|
96
|
+
# XML
|
97
|
+
#
|
98
|
+
# xml.at("img").ancestor("div") # == Node div#b
|
99
|
+
# xml.at("img").ancestor("#a") # == Node div#a
|
100
|
+
# xml.at("img").ancestor("#z") # == nil
|
101
|
+
#
|
51
102
|
# @param selector [String]
|
52
103
|
# @return [Nokogiri::XML::Element, nil]
|
53
104
|
def ancestor(selector = nil)
|
@@ -57,6 +108,21 @@ module Nokogiri::XML::Searchable
|
|
57
108
|
# Like +Searchable#ancestors!+, but returns only the first matching
|
58
109
|
# ancestor.
|
59
110
|
#
|
111
|
+
# @example
|
112
|
+
# xml = Nokogiri::XML(<<-XML)
|
113
|
+
# <body>
|
114
|
+
# <div id="a">
|
115
|
+
# <div id="b">
|
116
|
+
# <img src="cat.jpg">
|
117
|
+
# </div>
|
118
|
+
# </div>
|
119
|
+
# </body>
|
120
|
+
# XML
|
121
|
+
#
|
122
|
+
# xml.at("img").ancestor!("div") # == Node div#b
|
123
|
+
# xml.at("img").ancestor!("#a") # == Node div#a
|
124
|
+
# xml.at("img").ancestor!("#z") # raise error
|
125
|
+
#
|
60
126
|
# @param selector [String]
|
61
127
|
# @return [Nokogiri::XML::Element]
|
62
128
|
# @raise [Ryoba::Error]
|
data/lib/ryoba/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ryoba
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Hefner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|
@@ -95,12 +95,14 @@ extra_rdoc_files: []
|
|
95
95
|
files:
|
96
96
|
- ".gitignore"
|
97
97
|
- ".travis.yml"
|
98
|
+
- CHANGELOG.md
|
98
99
|
- Gemfile
|
99
100
|
- LICENSE.txt
|
100
101
|
- README.md
|
101
102
|
- Rakefile
|
102
103
|
- lib/ryoba.rb
|
103
104
|
- lib/ryoba/error.rb
|
105
|
+
- lib/ryoba/nokogiri/xml/document.rb
|
104
106
|
- lib/ryoba/nokogiri/xml/node.rb
|
105
107
|
- lib/ryoba/nokogiri/xml/searchable.rb
|
106
108
|
- lib/ryoba/version.rb
|
@@ -124,8 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
126
|
- !ruby/object:Gem::Version
|
125
127
|
version: '0'
|
126
128
|
requirements: []
|
127
|
-
|
128
|
-
rubygems_version: 2.6.13
|
129
|
+
rubygems_version: 3.0.1
|
129
130
|
signing_key:
|
130
131
|
specification_version: 4
|
131
132
|
summary: Nokogiri utility methods
|