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

Add not_found extension #46

Merged
merged 1 commit into from
Sep 19, 2021
Merged

Conversation

waiting-for-dev
Copy link
Owner

This extension helps to build a not-found response in a single method
invocation. The WebPipe::Conn#not_found method will:

  • Set 404 as response status.
  • Set 'Not found' as the response body, or instead run a step configured in
    :not_found_body_step config key.
  • Halt the connection struct.
require 'web_pipe'
require 'web_pipe/plugs/config'

WebPipe.load_extensions(:params, :not_found)

class ShowItem
  include 'web_pipe'

  plug :config, WebPipe::Plugs::Config.(
    not_found_body_step: ->(conn) { conn.set_response_body('Nothing') }
  )

  plug :fetch_item do |conn|
    conn.add(:item, Item[params['id']])
  end

  plug :check_item do |conn|
    if conn.fetch(:item)
      conn
    else
      conn.not_found
    end
  end

  plug :render do |conn|
    conn.set_response_body(conn.fetch(:item).name)
  end
end

This extension helps to build a not-found response in a single method
invocation. The `WebPipe::Conn#not_found` method will:

- Set 404 as response status.
- Set 'Not found' as the response body, or instead run a step configured in
`:not_found_body_step` config key.
- Halt the connection struct.

```ruby
require 'web_pipe'
require 'web_pipe/plugs/config'

WebPipe.load_extensions(:params, :not_found)

class ShowItem
  include 'web_pipe'

  plug :config, WebPipe::Plugs::Config.(
    not_found_body_step: ->(conn) { conn.set_response_body('Nothing') }
  )

  plug :fetch_item do |conn|
    conn.add(:item, Item[params['id']])
  end

  plug :check_item do |conn|
    if conn.fetch(:item)
      conn
    else
      conn.not_found
    end
  end

  plug :render do |conn|
    conn.set_response_body(conn.fetch(:item).name)
  end
end
```
@waiting-for-dev waiting-for-dev merged commit b0e04e6 into master Sep 19, 2021
@waiting-for-dev waiting-for-dev deleted the waiting-for-dev/not_found branch September 19, 2021 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant