Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response shortcuts #2

Open
aldesantis opened this issue Feb 6, 2018 · 1 comment
Open

Response shortcuts #2

aldesantis opened this issue Feb 6, 2018 · 1 comment
Labels

Comments

@aldesantis
Copy link
Member

aldesantis commented Feb 6, 2018

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: {},
)
@aldesantis aldesantis changed the title Simplify responding Response shortcuts Feb 6, 2018
@aldesantis
Copy link
Member Author

Some work was started around this in #4 but I don't think it was going in the right direction, as the helpers introduced too much complexity. Still not sure what a good implementation might look like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant