Class: Typhoeus::Hydra

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus/hydra.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHydra

Returns a new instance of Hydra.



3
4
5
6
7
8
# File 'lib/typhoeus/hydra.rb', line 3

def initialize
  @multi       = Multi.new
  @easy_pool   = []
  @mocks       = []
  @memoized_requests = {}
end

Class Method Details

.hydraObject



10
11
12
# File 'lib/typhoeus/hydra.rb', line 10

def self.hydra
  @hydra ||= new
end

Instance Method Details

#cache_getter(&block) ⇒ Object



40
41
42
# File 'lib/typhoeus/hydra.rb', line 40

def cache_getter(&block)
  @cache_getter = block
end

#cache_setter(&block) ⇒ Object



44
45
46
# File 'lib/typhoeus/hydra.rb', line 44

def cache_setter(&block)
  @cache_setter = block
end

#mock(method, url) ⇒ Object



56
57
58
59
# File 'lib/typhoeus/hydra.rb', line 56

def mock(method, url)
  @mocks << HydraMock.new(url, method)
  @mocks.last
end

#on_complete(&block) ⇒ Object



48
49
50
# File 'lib/typhoeus/hydra.rb', line 48

def on_complete(&block)
  @on_complete = block
end

#on_complete=(proc) ⇒ Object



52
53
54
# File 'lib/typhoeus/hydra.rb', line 52

def on_complete=(proc)
  @on_complete = proc
end

#queue(request) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/typhoeus/hydra.rb', line 14

def queue(request)
  return if assign_to_mock(request)
  
  if request.method == :get
    if @memoized_requests.has_key? request.url
      @memoized_requests[request.url] << request
    else
      @memoized_requests[request.url] = []
      get_from_cache_or_queue(request)
    end
  else
    get_from_cache_or_queue(request)
  end
end

#runObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/typhoeus/hydra.rb', line 29

def run
  @mocks.each do |m|
    m.requests.each do |request|
      m.response.request = request
      handle_request(request, m.response)
    end
  end
  @multi.perform
  @memoized_requests = {}
end