module Acfs::Request::Callbacks

Module containing callback handling for Requests. Current the only callback type is `on_complete`:

request = Request.new 'URL'
request.on_complete { |response| ... }

Public Instance Methods

callbacks() click to toggle source

Return array of all callbacks.

@return [ Array<Block> ] All callbacks.

# File lib/acfs/request/callbacks.rb, line 32
def callbacks
  @callbacks ||= []
end
complete!(response) click to toggle source

Trigger all callback for given response.

@return [ Acfs::Request ] The request itself.

# File lib/acfs/request/callbacks.rb, line 40
def complete!(response)
  call_callback response, 0
  self
end
on_complete(&block) click to toggle source

Add a new `on_complete` callback for this request.

@example Set on_complete.

request.on_complete { |response| print response.body }

@param [ Block ] block The callback block to execute.

@yield [ Acfs::Response ]

@return [ Acfs::Request ] The request itself.

# File lib/acfs/request/callbacks.rb, line 23
def on_complete(&block)
  callbacks.insert 0, block if block_given?
  self
end

Private Instance Methods

call_callback(res, index) click to toggle source
# File lib/acfs/request/callbacks.rb, line 47
def call_callback(res, index)
  return if index >= callbacks.size

  callbacks[index].call(res, proc {|bres| call_callback bres, index + 1 })
end