Class: Typhoeus::Hydra

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_pool_size = 10) ⇒ Hydra

Returns a new instance of Hydra.



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

def initialize(initial_pool_size = 10)
  @memoize_requests = true
  @multi       = Multi.new
  @easy_pool   = []
  initial_pool_size.times { @easy_pool << Easy.new }
  @stubs       = []
  @memoized_requests = {}
  @retrieved_from_cache = {}
end

Class Method Details

.hydraObject



13
14
15
# File 'lib/typhoeus/hydra.rb', line 13

def self.hydra
  @hydra ||= new
end

.hydra=(val) ⇒ Object



17
18
19
# File 'lib/typhoeus/hydra.rb', line 17

def self.hydra=(val)
  @hydra = val
end

Instance Method Details

#cache_getter(&block) ⇒ Object



65
66
67
# File 'lib/typhoeus/hydra.rb', line 65

def cache_getter(&block)
  @cache_getter = block
end

#cache_setter(&block) ⇒ Object



69
70
71
# File 'lib/typhoeus/hydra.rb', line 69

def cache_setter(&block)
  @cache_setter = block
end

#clear_stubsObject



21
22
23
# File 'lib/typhoeus/hydra.rb', line 21

def clear_stubs
  @stubs = []
end

#disable_memoizationObject



61
62
63
# File 'lib/typhoeus/hydra.rb', line 61

def disable_memoization
  @memoize_requests = false
end

#fire_and_forgetObject



25
26
27
# File 'lib/typhoeus/hydra.rb', line 25

def fire_and_forget
  @multi.fire_and_forget
end

#on_complete(&block) ⇒ Object



73
74
75
# File 'lib/typhoeus/hydra.rb', line 73

def on_complete(&block)
  @on_complete = block
end

#on_complete=(proc) ⇒ Object



77
78
79
# File 'lib/typhoeus/hydra.rb', line 77

def on_complete=(proc)
  @on_complete = proc
end

#queue(request) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/typhoeus/hydra.rb', line 29

def queue(request)
  return if assign_to_stub(request)

  if request.method == :get
    if @memoize_requests && @memoized_requests.has_key?(request.url)
      if response = @retrieved_from_cache[request.url]
        request.response = response
        request.call_handlers
      else
        @memoized_requests[request.url] << request
      end
    else
      @memoized_requests[request.url] = [] if @memoize_requests
      get_from_cache_or_queue(request)
    end
  else
    get_from_cache_or_queue(request)
  end
end

#runObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/typhoeus/hydra.rb', line 49

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

#stub(method, url) ⇒ Object



81
82
83
84
# File 'lib/typhoeus/hydra.rb', line 81

def stub(method, url)
  @stubs << HydraMock.new(url, method)
  @stubs.last
end