Class: Docs::Requester

Inherits:
Typhoeus::Hydra
  • Object
show all
Defined in:
lib/docs/core/requester.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Requester

Returns a new instance of Requester


13
14
15
16
17
# File 'lib/docs/core/requester.rb', line 13

def initialize(options = {})
  @request_options = options.extract!(:request_options)[:request_options].try(:dup) || {}
  options[:max_concurrency] ||= 20
  super
end

Instance Attribute Details

#request_optionsObject (readonly)

Returns the value of attribute request_options


3
4
5
# File 'lib/docs/core/requester.rb', line 3

def request_options
  @request_options
end

Class Method Details

.run(urls, options = {}, &block) ⇒ Object


5
6
7
8
9
10
11
# File 'lib/docs/core/requester.rb', line 5

def self.run(urls, options = {}, &block)
  requester = new(options)
  requester.on_response(&block) if block
  requester.request(urls)
  requester.run
  requester
end

Instance Method Details

#on_response(&block) ⇒ Object


31
32
33
34
35
# File 'lib/docs/core/requester.rb', line 31

def on_response(&block)
  @on_response ||= []
  @on_response << block if block
  @on_response
end

#queue(request) ⇒ Object


26
27
28
29
# File 'lib/docs/core/requester.rb', line 26

def queue(request)
  request.on_complete(&method(:handle_response))
  super
end

#request(urls, options = {}, &block) ⇒ Object


19
20
21
22
23
24
# File 'lib/docs/core/requester.rb', line 19

def request(urls, options = {}, &block)
  requests = [urls].flatten.map do |url|
    build_and_queue_request(url, options, &block)
  end
  requests.length == 1 ? requests.first : requests
end