Class: Spidy::Connector::Html

Inherits:
Object
  • Object
show all
Includes:
StaticAccessor
Defined in:
lib/spidy/connector/html.rb

Overview

Mechanize wrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent:) ⇒ Html

Returns a new instance of Html.



9
10
11
12
13
# File 'lib/spidy/connector/html.rb', line 9

def initialize(user_agent:)
  @agent = Mechanize.new
  @user_agent = user_agent
  @agent.user_agent = user_agent
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



15
16
17
# File 'lib/spidy/connector/html.rb', line 15

def agent
  @agent
end

Instance Method Details

#call(url, encoding: nil, &yielder) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/spidy/connector/html.rb', line 17

def call(url, encoding: nil, &yielder)
  fail 'url is not specified' if url.blank?
  if encoding
    agent.default_encoding = encoding
    agent.force_default_encoding = true
  end
  connect(url, yielder)
end

#refresh!Object



26
27
28
29
# File 'lib/spidy/connector/html.rb', line 26

def refresh!
  @agent = Mechanize.new
  @agent.user_agent = @user_agent
end