Class: Spiderman::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderman/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



6
7
8
9
10
# File 'lib/spiderman/runner.rb', line 6

def initialize
  @urls = []
  @handlers = {}
  @headers = {}
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



4
5
6
# File 'lib/spiderman/runner.rb', line 4

def headers
  @headers
end

#urlsObject (readonly)

Returns the value of attribute urls.



4
5
6
# File 'lib/spiderman/runner.rb', line 4

def urls
  @urls
end

Instance Method Details

#dupObject



34
35
36
37
38
39
40
41
# File 'lib/spiderman/runner.rb', line 34

def dup
  self.class.new.tap do |obj|
    obj.urls.replace(urls)
    obj.handlers.update(handlers)
    obj.headers.update(headers)
    obj.logger = logger
  end
end

#handler_for(name) ⇒ Object



20
21
22
# File 'lib/spiderman/runner.rb', line 20

def handler_for(name)
  @handlers[name]
end

#httpObject



24
25
26
# File 'lib/spiderman/runner.rb', line 24

def http
  HTTP.use(logging: {logger: logger}).headers(headers).follow
end

#register(name, &handler) ⇒ Object



16
17
18
# File 'lib/spiderman/runner.rb', line 16

def register(name, &handler)
  @handlers[name] = handler
end

#request(url) ⇒ Object



28
29
30
31
32
# File 'lib/spiderman/runner.rb', line 28

def request(url)
  http.get(url).tap do |response|
    response.extend HTTP::ActAsNokogiriDocument
  end
end

#start_at(*urls) ⇒ Object



12
13
14
# File 'lib/spiderman/runner.rb', line 12

def start_at(*urls)
  @urls.append(*urls)
end