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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4427faef517a9c71fe0a3f9ffe05b14e6a72a3d57e91ddec45eb7e318dc0d2e7
4
- data.tar.gz: d65983416eb555e3b7ef4ea856aca6ac2fd3f25240280d2cf44f0fc78e874666
3
+ metadata.gz: 1fba139432757faa60a168a7d46de3db3bbbdee015b3b3fc35f4853196fd64da
4
+ data.tar.gz: '0154038bda8b133877c7f2dd8828dc8371d0563e4a7ff41c647a9a85a9655300'
5
5
  SHA512:
6
- metadata.gz: 1f2e652105f9f9e2e407107d5044e45f3a40d512b72b4b9c47bbc921cbf0744cd54f97535f07493ef6b5b21f31a9219c2a1c801c323c904541c213c59cce7a2e
7
- data.tar.gz: b5386fea58c59d2a76200ea73d902b65e7376a09563efe5d8c256867bffaaec6278d31d7d440de0a477248bfcfb132f02b790e6f59002ddd1ec46f63830048ba
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
@@ -3,46 +3,33 @@
3
3
  #
4
4
  # Bind html and convert to object
5
5
  #
6
- module Spidy::Binder::Html
7
- #
8
- # Describe the definition to get the necessary elements from the resource object
9
- #
10
- class Resource
11
- class_attribute :names, default: []
12
- attr_reader :html, :source
13
-
14
- def initialize(html)
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
- def url
24
- html.uri.to_s
25
- end
16
+ def to_s
17
+ to_h.to_json
18
+ end
26
19
 
27
- def to_h
28
- names.map { |name| [name, send(name)] }.to_h
29
- end
20
+ def to_h
21
+ names.map { |name| [name, send(name)] }.to_h
22
+ end
30
23
 
31
- def self.let(name, query = nil, &block)
32
- names << name
33
- define_method(name) do
34
- return html.at(query)&.text if block.nil?
35
- return instance_exec(&block) if query.blank?
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
- instance_exec(html.search(query), &block)
38
- rescue NoMethodError => e
39
- raise "#{html.uri} ##{name} => #{e.message}"
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
@@ -3,40 +3,31 @@
3
3
  #
4
4
  # Bind json and convert to object
5
5
  #
6
- module Spidy::Binder::Json
7
- #
8
- # Describe the definition to get the necessary elements from the resource object
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
- def initialize(json)
15
- @json = json
16
- @source = json
17
- end
10
+ def initialize(json, url: nil)
11
+ @json = json
12
+ @url = url
13
+ @source = json
14
+ end
18
15
 
19
- def to_s
20
- to_h.to_json
21
- end
16
+ def to_s
17
+ to_h.to_json
18
+ end
22
19
 
23
- def to_h
24
- names.map { |name| [name, send(name)] }.to_h
25
- end
20
+ def to_h
21
+ names.map { |name| [name, send(name)] }.to_h
22
+ end
26
23
 
27
- def self.let(name, *query, &block)
28
- names << name
29
- define_method(name) do
30
- result = json.dig(*query) if query.present?
31
- return result if block.nil?
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
- instance_exec(result, &block)
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
@@ -3,46 +3,33 @@
3
3
  #
4
4
  # Bind xml and convert to object
5
5
  #
6
- module Spidy::Binder::Xml
7
- #
8
- # Describe the definition to get the necessary elements from the resource object
9
- #
10
- class Resource
11
- class_attribute :names, default: []
12
- attr_reader :xml, :source
13
-
14
- def initialize(xml)
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
- def url
24
- xml.uri.to_s
25
- end
16
+ def to_s
17
+ to_h.to_json
18
+ end
26
19
 
27
- def to_h
28
- names.map { |name| [name, send(name)] }.to_h
29
- end
20
+ def to_h
21
+ names.map { |name| [name, send(name)] }.to_h
22
+ end
30
23
 
31
- def self.let(name, query = nil, &block)
32
- names << name
33
- define_method(name) do
34
- return xml.at(query)&.text if block.nil?
35
- return instance_exec(&block) if query.blank?
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
- instance_exec(xml.search(query), &block)
38
- rescue NoMethodError => e
39
- raise "#{xml} ##{name} => #{e.message}"
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Spidy
4
- VERSION = '0.0.17'
4
+ VERSION = '0.0.18'
5
5
  end
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.17
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-06 00:00:00.000000000 Z
11
+ date: 2019-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler