Sha256: aea6ad2357245649fa142fb3f04fa0fdef8789383e5c8f15ffac4c33c1291833

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module Acfs
  class Request
    # Module containing callback handling for Requests.
    # Current the only callback type is `on_complete`:
    #
    #   request = Request.new 'URL'
    #   request.on_complete { |response| ... }
    #
    module Callbacks
      # 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.
      #
      def on_complete(&block)
        callbacks.insert 0, block if block
        self
      end

      # Return array of all callbacks.
      #
      # @return [ Array<Block> ] All callbacks.
      #
      def callbacks
        @callbacks ||= []
      end

      # Trigger all callback for given response.
      #
      # @return [ Acfs::Request ] The request itself.
      #
      def complete!(response)
        call_callback response, 0
        self
      end

      private

      def call_callback(res, index)
        return if index >= callbacks.size

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acfs-2.0.0 lib/acfs/request/callbacks.rb
acfs-1.7.0 lib/acfs/request/callbacks.rb