Class: Treaty::Action::Response::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/action/response/factory.rb

Overview

Factory for creating response attribute collections.

Purpose

Captures response attribute definitions from treaty DSL and provides access to the resulting attribute collection. Supports both inline block syntax and Entity class references.

Difference from Request::Factory

Response::Factory stores HTTP status code along with attributes. This allows treaty to validate responses against expected status.

Usage

Created internally by:

  • Versions::Factory (when response STATUS do ... end is called)

Consumed by:

  • Response::Validator (to validate service output)
  • Info::Builder (to build response schema information)
  • Versions::Execution::Base (to set response status)

Definition Modes

Inline Block Mode

response 201 do
object :post do
  string :id
  string :title
end
end

Entity Class Mode

response 201, Posts::Create::ResponseEntity

Example

factory = Response::Factory.new(201)
factory.status  # => 201
factory.object :post do
factory.string :id
end
factory.collection_of_attributes  # => Collection with post attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status) ⇒ Factory

Creates new response factory with HTTP status

Parameters:

  • status (Integer)

    HTTP status code (200, 201, 404, etc.)



59
60
61
# File 'lib/treaty/action/response/factory.rb', line 59

def initialize(status)
  @status = status
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(type, *helpers, **options, &block) ⇒ void

This method returns an undefined value.

Forwards DSL method calls to internal Entity class

Creates an anonymous Entity class on first call, then forwards all DSL methods (string, integer, object, etc.) to it.

Parameters:

  • type (Symbol)

    Attribute type (method name)

  • helpers (Array)

    Helper symbols and arguments

  • options (Hash)

    Attribute options

  • block (Proc)

    Block for nested attributes



95
96
97
98
99
# File 'lib/treaty/action/response/factory.rb', line 95

def method_missing(type, *helpers, **options, &block)
  @entity_class ||= Class.new(Entity)

  @entity_class.public_send(type, *helpers, **options, &block)
end

Instance Attribute Details

#statusInteger (readonly)

Returns HTTP status code for this response.

Returns:

  • (Integer)

    HTTP status code for this response



54
55
56
# File 'lib/treaty/action/response/factory.rb', line 54

def status
  @status
end

Instance Method Details

#collection_of_attributesTreaty::Entity::Attribute::Collection

Returns the collection of defined attributes

Returns:



79
80
81
82
83
# File 'lib/treaty/action/response/factory.rb', line 79

def collection_of_attributes
  return Treaty::Entity::Attribute::Collection.new if @entity_class.nil?

  @entity_class.collection_of_attributes
end

#respond_to_missing?(name) ⇒ Boolean

Checks if method should be handled by method_missing

Parameters:

  • name (Symbol)

    Method name

Returns:

  • (Boolean)


105
106
107
# File 'lib/treaty/action/response/factory.rb', line 105

def respond_to_missing?(name, *)
  super
end

#use_entity(entity_class) ⇒ void

This method returns an undefined value.

Registers an Entity class for response schema

Use this to reference a pre-defined Entity class instead of inline attribute definitions.

Parameters:

  • entity_class (Class)

    Must be Treaty::Entity::Base subclass

Raises:



71
72
73
74
# File 'lib/treaty/action/response/factory.rb', line 71

def use_entity(entity_class)
  validate_entity_class!(entity_class)
  @entity_class = entity_class
end