Skip to content

Response shortcuts #2

@aldesantis

Description

@aldesantis

Responding right now is very cumbersome in custom operations, e.g.

def respond!(options)
  options['result.response'] = Pragma::Operation::Response::Ok.new(
    entity: options['model'],
  ).decorate_with(Decorator::Instance)
end

We can simplify success and failure responses like this:

# Responds and returns true (for regular responses)
respond(options, :ok, 
  entity: options['model'] # optional (default is nil),
  headers: {}, # optional
  decorator: Decorator::Instance, # optional
)

# Responds and returns false (for errors)
respond!(options, :not_found, 
  entity: Pragma::Operation::Error.new(
    error_type: :not_found,
    error_message: 'The requested resource could not be found.',
  ), 
  headers: {}, 
  decorator: Pragma::Decorator::Error,
)

# Shortcut for erroring (all arguments optional)
fail!(options, :not_found, 
  error_type: :not_found,
  error_message: 'The requested resource could not be found.',
  headers: {},
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions