Class: Faraday::Adapter::EMHttp::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/adapter/em_http.rb

Overview

This parallel manager is designed to start an EventMachine loop and block until all registered requests have been completed.

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.

See Also:



226
227
228
# File 'lib/faraday/adapter/em_http.rb', line 226

def initialize
  reset
end

Instance Method Details

#add(&block) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/faraday/adapter/em_http.rb', line 244

def add(&block)
  if running?
    perform_request(&block)
  else
    @registered_procs << block
  end
  @num_registered += 1
end

#check_finishedObject



281
282
283
# File 'lib/faraday/adapter/em_http.rb', line 281

def check_finished
  EventMachine.stop if @num_succeeded + @errors.size == @num_registered
end

#perform_requestObject



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/faraday/adapter/em_http.rb', line 269

def perform_request
  client = yield
  client.callback do
    @num_succeeded += 1
    check_finished
  end
  client.errback do
    @errors << client.error
    check_finished
  end
end

#resetObject

Re-initializes instance variables



231
232
233
234
235
236
237
# File 'lib/faraday/adapter/em_http.rb', line 231

def reset
  @registered_procs = []
  @num_registered = 0
  @num_succeeded = 0
  @errors = []
  @running = false
end

#runObject



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/faraday/adapter/em_http.rb', line 253

def run
  if @num_registered.positive?
    @running = true
    EventMachine.run do
      @registered_procs.each do |proc|
        perform_request(&proc)
      end
    end
    unless @errors.empty?
      raise Faraday::ClientError, @errors.first || 'connection failed'
    end
  end
ensure
  reset
end

#running?Boolean

Returns:

  • (Boolean)


240
241
242
# File 'lib/faraday/adapter/em_http.rb', line 240

def running?
  @running
end