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

Allow param transformation with transproc #30

Merged
merged 1 commit into from
Aug 6, 2019

Conversation

waiting-for-dev
Copy link
Owner

A new :params extension has been added (which in turns removes #params implementation from :url extension).

This new extension adds a Conn#params method which can perform any number of transformations to the request parameters.

When no transformations are given, #params just returns request parameters (both GET and POST) as a hash:

 # http://www.example.com?foo=bar
conn.params #=> { 'foo' => 'bar' }

Further processing can be specified thanks to transproc gem (you need to add it yourself to the Gemfile). All hash transformations in transproc are available:

  # http://www.example.com?foo=bar
conn.params([:deep_symbolize_keys]) #=> { foo: 'bar' }

Extra needed arguments can be provided as an array:

 # http://www.example.com?foo=bar&zoo=zoo
conn.params([:deep_symbolize_keys, [:reject_keys, [:zoo]]) #=> { foo: 'bar' }

Instead of injecting transformations at the moment #params is called, you can configure them to be automatically used.

 # http://www.example.com?foo=bar
conn.
  add_config(:param_transformation, [:deep_symbolize_keys]).
  params #=> { foo: 'bar' }

You can register your own transformation functions:

 # http://www.example.com?foo=bar
fake = ->(_params) { { fake: :params } }
WebPipe::Params::Transf.register(:fake, fake)
conn.params([:fake]) #=> { fake: :params }

Your own transformation functions can depend on the Conn instance at the moment of execution. For that, just place it as the last argument of the function and it will be curried automatically:

 # http://www.example.com?foo=bar
add_name = ->(params, conn) { params.merge(name: conn.fetch(:name)) }
WebPipe::Params::Transf.register(:add_name, add_name)
conn.
  add(:name, 'Joe').
  params([:deep_symbolize_keys, :add_name]) #=> { foo: 'bar', name: 'Joe' }

Inline transformations can also be provided:

 # http://www.example.com?foo=bar
fake = ->(_params) { { fake: :params } }
conn.
  params(fake) #=> { fake: :params }

Besides, router params has also been removed from :url extension and it is now a standalone extension called :router_params.

This extension adds a :router_params transformation to merge router params into Conn#params.

require 'web_pipe'

WebPipe.load_extensions(:router_params)

class MyApp
  include WebPipe

  plug :config
  plug :get_params

  private

  def config(conn)
    conn.add_config(:param_transformation, [:router_params])
  end

  def get_params(conn)
    # http://example.com/users/1/edit
    conn.params #=> { id: 1 }
    conn
  end
end

A new `:params` extension has been added (which in turns removes
`#params` implementation from `:url` extension).

This new extension adds a `Conn#params` method which can perform any
number of transformations to the request parameters.

When no transformations are given, `#params` just returns request
parameters (both GET and POST) as a hash:

```ruby
 # http://www.example.com?foo=bar
conn.params #=> { 'foo' => 'bar' }
```

Further processing can be specified thanks to `transproc` gem (you
need to add it yourself to the Gemfile). All hash transformations
in `transproc` are available:

```ruby
  # http://www.example.com?foo=bar
conn.params([:deep_symbolize_keys]) #=> { foo: 'bar' }
```

Extra needed arguments can be provided as an array:

```ruby
 # http://www.example.com?foo=bar&zoo=zoo
conn.params([:deep_symbolize_keys, [:reject_keys, [:zoo]]) #=> { foo: 'bar' }
```

Instead of injecting transformations at the moment `#params` is
called, you can configure them to be automatically used.

```ruby
 # http://www.example.com?foo=bar
conn.
  add_config(:param_transformation, [:deep_symbolize_keys]).
  params #=> { foo: 'bar' }
```

You can register your own transformation functions:

```ruby
 # http://www.example.com?foo=bar
fake = ->(_params) { { fake: :params } }
WebPipe::Params::Transf.register(:fake, fake)
conn.params([:fake]) #=> { fake: :params }
```

Your own transformation functions can depend on the `Conn`
instance at the moment of execution. For that, just place it as the
last argument of the function and it will be curried automatically:

```ruby
 # http://www.example.com?foo=bar
add_name = ->(params, conn) { params.merge(name: conn.fetch(:name)) }
WebPipe::Params::Transf.register(:add_name, add_name)
conn.
  add(:name, 'Joe').
  params([:deep_symbolize_keys, :add_name]) #=> { foo: 'bar', name: 'Joe' }
```

Inline transformations can also be provided:

```ruby
 # http://www.example.com?foo=bar
fake = ->(_params) { { fake: :params } }
conn.
  params(fake) #=> { fake: :params }
```

Besides, router params has also been removed from `:url` extension and
it is now a standalone extension called `:router_params`.

This extension adds a `:router_params` transformation to merge router
params into `Conn#params`.

```ruby
require 'web_pipe'

WebPipe.load_extensions(:router_params)

class MyApp
  include WebPipe

  plug :config
  plug :get_params

  private

  def config(conn)
    conn.add_config(:param_transformation, [:router_params])
  end

  def get_params(conn)
    # http://example.com/users/1/edit
    conn.params #=> { id: 1 }
    conn
  end
end
```
@waiting-for-dev waiting-for-dev added the enhancement New feature or request label Aug 6, 2019
@waiting-for-dev waiting-for-dev merged commit cddb3b2 into master Aug 6, 2019
@waiting-for-dev waiting-for-dev deleted the params_transproc branch August 6, 2019 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant