Commit fa0a9ba753ebd4998f06075456711826b0a8be07

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent da3f08f8

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 2 module API
3 3 module APIHelpers
4 4 PRIVATE_TOKEN_PARAM = :private_token
  5 + ALLOWED_PARAMETERS = ['parent_id', 'from', 'until', 'content_type']
5 6  
6 7 def logger
7 8 @logger ||= Logger.new(File.join(Rails.root, 'log', "#{ENV['RAILS_ENV']}_api.log"))
... ... @@ -52,15 +53,17 @@ module Noosfero
52 53 article = articles.find(id)
53 54 article.display_to?(current_user.person) ? article : forbidden!
54 55 end
55   -
  56 +
56 57 def make_conditions_with_parameter(params = {})
  58 + parsed_params = parser_params(params)
57 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 65 conditions[:created_at] = period(from_date, until_date) if from_date || until_date
  66 + conditions.merge!(parsed_params)
64 67  
65 68 conditions
66 69 end
... ... @@ -165,6 +168,10 @@ module Noosfero
165 168 end
166 169  
167 170 private
  171 +
  172 + def parser_params(params)
  173 + params.select{|k,v| ALLOWED_PARAMETERS.include?(k)}
  174 + end
168 175  
169 176 def default_limit
170 177 20
... ...