Class: Faraday::Adapter::EMHttp::Manager
- Inherits:
-
Object
- Object
- Faraday::Adapter::EMHttp::Manager
- 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
- #add(&block) ⇒ Object
- #check_finished ⇒ Object
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
- #perform_request ⇒ Object
-
#reset ⇒ Object
Re-initializes instance variables.
- #run ⇒ Object
- #running? ⇒ Boolean
Constructor Details
#initialize ⇒ Manager
Returns a new instance of Manager.
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_finished ⇒ Object
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_request ⇒ Object
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 |
#reset ⇒ Object
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 |
#run ⇒ Object
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
240 241 242 |
# File 'lib/faraday/adapter/em_http.rb', line 240 def running? @running end |