spidy 0.0.17 → 0.0.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/spidy/binder.rb +19 -1
- data/lib/spidy/binder/html.rb +23 -36
- data/lib/spidy/binder/json.rb +20 -29
- data/lib/spidy/binder/xml.rb +23 -36
- data/lib/spidy/definition.rb +1 -1
- data/lib/spidy/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1fba139432757faa60a168a7d46de3db3bbbdee015b3b3fc35f4853196fd64da
|
4
|
+
data.tar.gz: '0154038bda8b133877c7f2dd8828dc8371d0563e4a7ff41c647a9a85a9655300'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae4a08546ff539c544768f2dc8bc4450acd66e9104ca3678d387c4b42fb67d13727700aa6b91fe6a2cb714114927bee1735b6a86b2a126ae35052f54dc4ae209
|
7
|
+
data.tar.gz: 89606fe1dcf46fe64f2b2e8b360f62a54c17b0dadf3788124a47eb5d3652262eea0532e6828558dbad81b37fa2a85cefefcc741b2f73f1f105e22ef7f2dae860
|
data/lib/spidy/binder.rb
CHANGED
@@ -9,8 +9,26 @@ module Spidy::Binder
|
|
9
9
|
autoload :Html
|
10
10
|
autoload :Xml
|
11
11
|
|
12
|
+
module Base
|
13
|
+
def self.call(html, url: nil, define: nil)
|
14
|
+
binder = Class.new(const_get(:Resource)) { instance_exec(&define) }
|
15
|
+
yield binder.new(html, url: url)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Caller
|
20
|
+
def initialize(binder)
|
21
|
+
@binder = binder
|
22
|
+
end
|
23
|
+
|
24
|
+
def call(source, url: nil, define: nil)
|
25
|
+
binder = Class.new(@binder) { instance_exec(&define) }
|
26
|
+
yield binder.new(source, url: url)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
12
30
|
def self.get(value)
|
13
|
-
return const_get(value.to_s.classify) if name.is_a?(String) || name.is_a?(Symbol)
|
31
|
+
return Caller.new(const_get(value.to_s.classify)) if name.is_a?(String) || name.is_a?(Symbol)
|
14
32
|
|
15
33
|
value
|
16
34
|
end
|
data/lib/spidy/binder/html.rb
CHANGED
@@ -3,46 +3,33 @@
|
|
3
3
|
#
|
4
4
|
# Bind html and convert to object
|
5
5
|
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@html = html
|
16
|
-
@source = html.body
|
17
|
-
end
|
18
|
-
|
19
|
-
def to_s
|
20
|
-
to_h.to_json
|
21
|
-
end
|
6
|
+
class Spidy::Binder::Html
|
7
|
+
class_attribute :names, default: []
|
8
|
+
attr_reader :html, :source, :url
|
9
|
+
|
10
|
+
def initialize(html, url: nil)
|
11
|
+
@html = html
|
12
|
+
@url = url
|
13
|
+
@source = html.body
|
14
|
+
end
|
22
15
|
|
23
|
-
|
24
|
-
|
25
|
-
|
16
|
+
def to_s
|
17
|
+
to_h.to_json
|
18
|
+
end
|
26
19
|
|
27
|
-
|
28
|
-
|
29
|
-
|
20
|
+
def to_h
|
21
|
+
names.map { |name| [name, send(name)] }.to_h
|
22
|
+
end
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
24
|
+
def self.let(name, query = nil, &block)
|
25
|
+
names << name
|
26
|
+
define_method(name) do
|
27
|
+
return html.at(query)&.text if block.nil?
|
28
|
+
return instance_exec(&block) if query.blank?
|
36
29
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
30
|
+
instance_exec(html.search(query), &block)
|
31
|
+
rescue NoMethodError => e
|
32
|
+
raise "#{html.uri} ##{name} => #{e.message}"
|
41
33
|
end
|
42
34
|
end
|
43
|
-
|
44
|
-
def self.call(html, define_block)
|
45
|
-
binder = Class.new(Resource) { instance_exec(&define_block) }
|
46
|
-
yield binder.new(html)
|
47
|
-
end
|
48
35
|
end
|
data/lib/spidy/binder/json.rb
CHANGED
@@ -3,40 +3,31 @@
|
|
3
3
|
#
|
4
4
|
# Bind json and convert to object
|
5
5
|
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
#
|
10
|
-
class Resource
|
11
|
-
class_attribute :names, default: []
|
12
|
-
attr_reader :json
|
6
|
+
class Spidy::Binder::Json
|
7
|
+
class_attribute :names, default: []
|
8
|
+
attr_reader :json, :source, :url
|
13
9
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
10
|
+
def initialize(json, url: nil)
|
11
|
+
@json = json
|
12
|
+
@url = url
|
13
|
+
@source = json
|
14
|
+
end
|
18
15
|
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
def to_s
|
17
|
+
to_h.to_json
|
18
|
+
end
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
def to_h
|
21
|
+
names.map { |name| [name, send(name)] }.to_h
|
22
|
+
end
|
26
23
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
24
|
+
def self.let(name, *query, &block)
|
25
|
+
names << name
|
26
|
+
define_method(name) do
|
27
|
+
result = json.dig(*query) if query.present?
|
28
|
+
return result if block.nil?
|
32
29
|
|
33
|
-
|
34
|
-
end
|
30
|
+
instance_exec(result, &block)
|
35
31
|
end
|
36
32
|
end
|
37
|
-
|
38
|
-
def self.call(resource, define_block)
|
39
|
-
binder = Class.new(Resource) { instance_exec(&define_block) }
|
40
|
-
yield binder.new(resource)
|
41
|
-
end
|
42
33
|
end
|
data/lib/spidy/binder/xml.rb
CHANGED
@@ -3,46 +3,33 @@
|
|
3
3
|
#
|
4
4
|
# Bind xml and convert to object
|
5
5
|
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@xml = xml
|
16
|
-
@source = xml.to_s
|
17
|
-
end
|
18
|
-
|
19
|
-
def to_s
|
20
|
-
to_h.to_json
|
21
|
-
end
|
6
|
+
class Spidy::Binder::Xml
|
7
|
+
class_attribute :names, default: []
|
8
|
+
attr_reader :xml, :source, :url
|
9
|
+
|
10
|
+
def initialize(xml, url: nil)
|
11
|
+
@xml = xml
|
12
|
+
@url = url
|
13
|
+
@source = xml.to_s
|
14
|
+
end
|
22
15
|
|
23
|
-
|
24
|
-
|
25
|
-
|
16
|
+
def to_s
|
17
|
+
to_h.to_json
|
18
|
+
end
|
26
19
|
|
27
|
-
|
28
|
-
|
29
|
-
|
20
|
+
def to_h
|
21
|
+
names.map { |name| [name, send(name)] }.to_h
|
22
|
+
end
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
24
|
+
def self.let(name, query = nil, &block)
|
25
|
+
names << name
|
26
|
+
define_method(name) do
|
27
|
+
return xml.at(query)&.text if block.nil?
|
28
|
+
return instance_exec(&block) if query.blank?
|
36
29
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
30
|
+
instance_exec(xml.search(query), &block)
|
31
|
+
rescue NoMethodError => e
|
32
|
+
raise "#{xml} ##{name} => #{e.message}"
|
41
33
|
end
|
42
34
|
end
|
43
|
-
|
44
|
-
def self.call(xml, define_block)
|
45
|
-
binder = Class.new(Resource) { instance_exec(&define_block) }
|
46
|
-
yield binder.new(xml)
|
47
|
-
end
|
48
35
|
end
|
data/lib/spidy/definition.rb
CHANGED
@@ -54,7 +54,7 @@ module Spidy::Definition
|
|
54
54
|
fail 'block is not specified' if yielder.nil?
|
55
55
|
|
56
56
|
connection_yielder = lambda do |resource|
|
57
|
-
binder.call(resource, define_block) { |object| yielder.call(object) }
|
57
|
+
binder.call(resource, url: url, define: define_block) { |object| yielder.call(object) }
|
58
58
|
end
|
59
59
|
connector.call(url, &connection_yielder)
|
60
60
|
end
|
data/lib/spidy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spidy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- aileron
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|