Class: APIClientBuilder::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/api_client_builder/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, response_handler, body = {}) ⇒ Request

a pagination strategy

Parameters:

  • type (Symbol)

    defines the object type to be processed

  • response_handler (Hash)

    a customizable set of options

  • body (Hash) (defaults to: {})

    a customizable set of options

Options Hash (response_handler):

  • the (ResponseHandler)

    response handler. Usually

Options Hash (body):

  • the (Hash)

    body of the response from the source



11
12
13
14
15
16
# File 'lib/api_client_builder/request.rb', line 11

def initialize(type, response_handler, body = {})
  @type = type
  @response_handler = response_handler
  @body = body
  @error_handlers_collection = []
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/api_client_builder/request.rb', line 5

def body
  @body
end

#error_handlers_collectionObject (readonly)

Returns the value of attribute error_handlers_collection.



5
6
7
# File 'lib/api_client_builder/request.rb', line 5

def error_handlers_collection
  @error_handlers_collection
end

#response_handlerObject (readonly)

Returns the value of attribute response_handler.



5
6
7
# File 'lib/api_client_builder/request.rb', line 5

def response_handler
  @response_handler
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/api_client_builder/request.rb', line 5

def type
  @type
end

Instance Method Details

#error_handlersArray<Block>

Yields the collection of error handlers that have been populated via the on_error interface. If none are defined, this will provide a default error handler that will provide context about the error and also how to define a new error handler.

Returns:

  • (Array<Block>)

    the error handlers collection



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/api_client_builder/request.rb', line 24

def error_handlers
  if error_handlers_collection.empty?
    on_error do |page, _handler|
      raise DefaultPageError, <<~MESSAGE
        Default error for bad response. If you want to handle this error use #on_error
        on the response in your api consumer. Error Code: #{page.status_code}.
      MESSAGE
    end
  end
  error_handlers_collection
end

#on_error(&block) ⇒ Object

Used to define custom error handling on this response. The error handlers will be called if there is not a success

Parameters:

  • block (Lambda)

    the error handling block to be stored in the error_handlers list



41
42
43
# File 'lib/api_client_builder/request.rb', line 41

def on_error(&block)
  @error_handlers_collection << block
end