Module: Spidy::Binder::Html

Defined in:
lib/spidy/binder/html.rb

Overview

Bind html and convert to object

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object



22
23
24
# File 'lib/spidy/binder/html.rb', line 22

def self.extended(obj)
  obj.alias_method :html, :resource
end

Instance Method Details

#let(name, query = nil, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/spidy/binder/html.rb', line 5

def let(name, query = nil, &block)
  @attribute_names ||= []
  @attribute_names << name

  return define_method(name) { html.at(query)&.text&.strip } if block.nil?

  define_method(name) do
    if query.present?
      instance_exec(html.at(query), &block)
    else
      instance_exec(&block)
    end
  rescue StandardError => e
    raise Spidy::Binder::Error, "spidy(#{@define_name})##{name} => #{e.message}"
  end
end