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 optional functionality to pre process and extract parameters. #188

Merged
merged 8 commits into from
Feb 21, 2014

Conversation

mtparet
Copy link
Contributor

@mtparet mtparet commented Jan 24, 2014

Description

The goal is to extract and pre process parameters of the request.

For example Rails, by default, transforms empty array to nil value, for some people it could be better to transform it again to an empty array.

Or if we want to support a custom parameter type, like enumeration (comma separated values), this could be used to transform automatically the string parameter, representing an enumeration, to an array.

Usage

To use it, set processing_value configuration variable to true.

Apipie.configure do |config|
  config.process_values = true
end

In your controller action, use values variable instead of params.

To implement a custom pre processing, the idea is to write a process_value function in the validator:
function in your validator:

   def process_value(value)
    value ? value.split(',') : []
   end

as

In the same time, I added an option in parameter description: as.
The idea is to add the possibility to have a mapping/separation between names we expose to the external world and the names we use internally.

example:

param :client, Hash, as: :user do
  param :default_language, String, as: :locale
  param :name, String
end

not processed params:

params : { 'client': {'default_language': 'en', 'name': 'toto'} }

processed params (available through api_params:

api_params: {user:{locale: 'en', name: 'toto'}}

What do you think about this functionality in Apipie ?

@iNecas
Copy link
Member

iNecas commented Jan 24, 2014

Interesting idea! It could also probably used as way to prevent multi_assignment issues, accepting only the documented params. I'm not sure about @values being the best name for it. Not saying I have better name :) Maybe something like @api_params?

@@ -186,6 +187,8 @@ def _apipie_define_validators(description)

old_method = instance_method(description.method)


# @todo we should use before_filter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'm not sure we didn't go for it from the start. Maybe there was some reason, if there is, I bet it will be rediscovered while trying converting that into filter.

@mtparet
Copy link
Contributor Author

mtparet commented Jan 25, 2014

Thanks! Indeed we are using it also for multi_assignment issues. values is a history legacy from a previous try from scratch. I agree api_params could be a better name.

Almost all the modification I proposed in these PRs are (or will be) used in production.

@mtparet
Copy link
Contributor Author

mtparet commented Jan 27, 2014

I added a description about internal_name and I renamed values to api_params

@mtparet
Copy link
Contributor Author

mtparet commented Feb 10, 2014

I changed internal_name for as

@api_params = {}
if @hash_params && value
@hash_params.each do |k, p|
@api_params[p.as] = p.process_value(value[k]) if value.has_key?(k)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the instance param for @api_params is probably unnecessary, what about:

  if @hash_params && value
    return @hash_params.each_with_object({}) do |(key, param), api_params|
      if value.has_key?(key)
        api_params[param.as] = param.process_value(value[key])
      end
    end
  end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never used each_with_object before. It seems better with it, thanks !

@iNecas
Copy link
Member

iNecas commented Feb 21, 2014

Very nice PR, merging now

@iNecas
Copy link
Member

iNecas commented Feb 21, 2014

sry, one more thing: could you rebase against the latest master, probably some conflict in README

@mtparet
Copy link
Contributor Author

mtparet commented Feb 21, 2014

Just rebased against master

@iNecas
Copy link
Member

iNecas commented Feb 21, 2014

Thank you!

iNecas added a commit that referenced this pull request Feb 21, 2014
Add optional functionality to pre process and extract parameters.
@iNecas iNecas merged commit c82bd58 into Apipie:master Feb 21, 2014
@iNecas
Copy link
Member

iNecas commented Mar 3, 2014

Released in 0.1.0. Thank you for your help!

@nlenepveu nlenepveu deleted the process_values branch April 23, 2014 16:33
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.

2 participants