Sha256: a1f6e0c94bedd2283d06bd6b1922b7589b132f4f2518b5ccd3d76ca26b41ee58

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

#
# Abstraction over WebMock to reduce duplication
#
# @author Mikael Henriksson <mikael@zoolutions.se>
# @since 0.1.0
#
module StubRequests
  #
  # Class Callback contains information about a subscription
  #
  # @author Mikael Henriksson <mikael@zoolutions.se>
  # @since 0.1.3
  #
  class Callback
    include Property
    #
    # @!attribute [rw] service_id
    #   @return [Symbol] the id of a service
    property :service_id, type: Symbol
    #
    # @!attribute [rw] endpoint_id
    #   @return [Symbol] the id of an endpoint
    property :endpoint_id, type: Symbol
    #
    # @!attribute [rw] verb
    #   @return [Symbol] the HTTP verb/method
    property :verb, type: Symbol, default: :any
    #
    # @!attribute [rw] callback
    #   @return [Proc] a proc to callback on notify
    property :callback, type: Proc

    #
    # Initialize a new Callback
    #
    # @param [Symbol] service_id the id of a service
    # @param [Symbol] endpoint_id the id of an endpoint
    # @param [Symbol] verb the HTTP verb/method
    # @param [Proc] callback a proc to callback on notify
    #
    def initialize(service_id, endpoint_id, verb, callback)
      self.service_id  = service_id
      self.endpoint_id = endpoint_id
      self.verb        = verb
      self.callback    = callback
    end

    def call(request_stub)
      case arity
      when 0
        callback.call
      when 1
        callback.call(request_stub)
      else
        raise InvalidCallback, "The callback for a callback can either take 0 or 1 arguments (was #{arity})"
      end
    end

    def arity
      callback.arity
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stub_requests-0.1.9 lib/stub_requests/callback.rb
stub_requests-0.1.8 lib/stub_requests/callback.rb
stub_requests-0.1.7 lib/stub_requests/callback.rb
stub_requests-0.1.6 lib/stub_requests/callback.rb
stub_requests-0.1.5 lib/stub_requests/callback.rb