Commit fa0a9ba753ebd4998f06075456711826b0a8be07
Committed by
Rodrigo Souto
1 parent
da3f08f8
Exists in
master
and in
29 other branches
API: adding more parameters as filter in conditions
Showing
1 changed file
with
11 additions
and
4 deletions
Show diff stats
lib/noosfero/api/helpers.rb
| @@ -2,6 +2,7 @@ module Noosfero | @@ -2,6 +2,7 @@ module Noosfero | ||
| 2 | module API | 2 | module API |
| 3 | module APIHelpers | 3 | module APIHelpers |
| 4 | PRIVATE_TOKEN_PARAM = :private_token | 4 | PRIVATE_TOKEN_PARAM = :private_token |
| 5 | + ALLOWED_PARAMETERS = ['parent_id', 'from', 'until', 'content_type'] | ||
| 5 | 6 | ||
| 6 | def logger | 7 | def logger |
| 7 | @logger ||= Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_api.log")) | 8 | @logger ||= Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_api.log")) |
| @@ -52,15 +53,17 @@ module Noosfero | @@ -52,15 +53,17 @@ module Noosfero | ||
| 52 | article = articles.find(id) | 53 | article = articles.find(id) |
| 53 | article.display_to?(current_user.person) ? article : forbidden! | 54 | article.display_to?(current_user.person) ? article : forbidden! |
| 54 | end | 55 | end |
| 55 | - | 56 | + |
| 56 | def make_conditions_with_parameter(params = {}) | 57 | def make_conditions_with_parameter(params = {}) |
| 58 | + parsed_params = parser_params(params) | ||
| 57 | conditions = {} | 59 | conditions = {} |
| 58 | - from_date = DateTime.parse(params[:from]) if params[:from] | ||
| 59 | - until_date = DateTime.parse(params[:until]) if params[:until] | 60 | + from_date = DateTime.parse(parsed_params.delete('from')) if parsed_params['from'] |
| 61 | + until_date = DateTime.parse(parsed_params.delete('until')) if parsed_params['until'] | ||
| 60 | 62 | ||
| 61 | - conditions[:type] = parse_content_type(params[:content_type]) unless params[:content_type].nil? | 63 | + conditions[:type] = parse_content_type(parsed_params.delete('content_type')) unless parsed_params['content_type'].nil? |
| 62 | 64 | ||
| 63 | conditions[:created_at] = period(from_date, until_date) if from_date || until_date | 65 | conditions[:created_at] = period(from_date, until_date) if from_date || until_date |
| 66 | + conditions.merge!(parsed_params) | ||
| 64 | 67 | ||
| 65 | conditions | 68 | conditions |
| 66 | end | 69 | end |
| @@ -165,6 +168,10 @@ module Noosfero | @@ -165,6 +168,10 @@ module Noosfero | ||
| 165 | end | 168 | end |
| 166 | 169 | ||
| 167 | private | 170 | private |
| 171 | + | ||
| 172 | + def parser_params(params) | ||
| 173 | + params.select{|k,v| ALLOWED_PARAMETERS.include?(k)} | ||
| 174 | + end | ||
| 168 | 175 | ||
| 169 | def default_limit | 176 | def default_limit |
| 170 | 20 | 177 | 20 |