Commit 57472ade72239609203050429ab310eac2914a80

Authored by Evandro Jr
1 parent d51e10ba

Adds API support for categories

params[:categories_ids]

/api/v1/communities/64/articles?from=2013-04-04-14:41:43&until=2015-06-11-14:41:43&limit=10&categories_ids[]=7&categories_ids[]=8&private_token=a97b6a5cae2c4c54e4ae18dde1829a49
Showing 1 changed file with 43 additions and 38 deletions   Show diff stats
lib/noosfero/api/helpers.rb
... ... @@ -2,7 +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 + ALLOWED_PARAMETERS = [:parent_id, :from, :until, :content_type]
6 6  
7 7 def current_user
8 8 private_token = (params[PRIVATE_TOKEN_PARAM] || headers['Private-Token']).to_s
... ... @@ -10,41 +10,41 @@ module Noosfero
10 10 @current_user = nil if !@current_user.nil? && @current_user.private_token_expired?
11 11 @current_user
12 12 end
13   -
  13 +
14 14 def current_person
15 15 current_user.person unless current_user.nil?
16 16 end
17   -
  17 +
18 18 def logout
19 19 @current_user = nil
20 20 end
21   -
  21 +
22 22 def environment
23 23 @environment
24 24 end
25   -
  25 +
26 26 def limit
27 27 limit = params[:limit].to_i
28 28 limit = default_limit if limit <= 0
29 29 limit
30 30 end
31   -
  31 +
32 32 def period(from_date, until_date)
33 33 return nil if from_date.nil? && until_date.nil?
34   -
  34 +
35 35 begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date
36 36 end_period = until_date.nil? ? DateTime.now : until_date
37   -
  37 +
38 38 begin_period..end_period
39 39 end
40   -
  40 +
41 41 def parse_content_type(content_type)
42 42 return nil if content_type.blank?
43 43 content_type.split(',').map do |content_type|
44 44 content_type.camelcase
45 45 end
46 46 end
47   -
  47 +
48 48 def find_article(articles, id)
49 49 article = articles.find(id)
50 50 article.display_to?(current_user.person) ? article : forbidden!
... ... @@ -60,12 +60,12 @@ module Noosfero
60 60 conditions = {}
61 61 from_date = DateTime.parse(parsed_params.delete(:from)) if parsed_params[:from]
62 62 until_date = DateTime.parse(parsed_params.delete(:until)) if parsed_params[:until]
63   -
  63 +
64 64 conditions[:type] = parse_content_type(parsed_params.delete(:content_type)) unless parsed_params[:content_type].nil?
65   -
  65 +
66 66 conditions[:created_at] = period(from_date, until_date) if from_date || until_date
67 67 conditions.merge!(parsed_params)
68   -
  68 +
69 69 conditions
70 70 end
71 71  
... ... @@ -82,13 +82,18 @@ module Noosfero
82 82 else
83 83 objects = object.send(method).where(conditions).limit(limit).order(order)
84 84 end
  85 +
  86 + if params[:categories_ids]
  87 + objects = objects.joins(:categories).where('category_id in (?)', params[:categories_ids])
  88 + end
  89 +
85 90 objects
86 91 end
87   -
  92 +
88 93 def authenticate!
89 94 unauthorized! unless current_user
90 95 end
91   -
  96 +
92 97 # Checks the occurrences of uniqueness of attributes, each attribute must be present in the params hash
93 98 # or a Bad Request error is invoked.
94 99 #
... ... @@ -99,7 +104,7 @@ module Noosfero
99 104 cant_be_saved_request!(key) if obj.send("find_by_#{key.to_s}", params[key])
100 105 end
101 106 end
102   -
  107 +
103 108 def attributes_for_keys(keys)
104 109 attrs = {}
105 110 keys.each do |key|
... ... @@ -110,9 +115,9 @@ module Noosfero
110 115  
111 116 def verify_recaptcha_v2(remote_ip, g_recaptcha_response, private_key, api_recaptcha_verify_uri)
112 117 verify_hash = {
113   - "secret" => private_key,
114   - "remoteip" => remote_ip,
115   - "response" => g_recaptcha_response
  118 + "secret" => private_key,
  119 + "remoteip" => remote_ip,
  120 + "response" => g_recaptcha_response
116 121 }
117 122 uri = URI(api_recaptcha_verify_uri)
118 123 https = Net::HTTP.new(uri.host, uri.port)
... ... @@ -121,42 +126,42 @@ module Noosfero
121 126 request.set_form_data(verify_hash)
122 127 JSON.parse(https.request(request).body)
123 128 end
124   -
  129 +
125 130 ##########################################
126 131 # error helpers #
127 132 ##########################################
128   -
  133 +
129 134 def forbidden!
130 135 render_api_error!('403 Forbidden', 403)
131 136 end
132   -
  137 +
133 138 def cant_be_saved_request!(attribute)
134 139 message = _("(Invalid request) #{attribute} can't be saved")
135 140 render_api_error!(message, 400)
136 141 end
137   -
  142 +
138 143 def bad_request!(attribute)
139 144 message = _("(Bad request) #{attribute} not given")
140 145 render_api_error!(message, 400)
141 146 end
142   -
  147 +
143 148 def something_wrong!
144 149 message = _("Something wrong happened")
145 150 render_api_error!(message, 400)
146 151 end
147   -
  152 +
148 153 def unauthorized!
149 154 render_api_error!(_('Unauthorized'), 401)
150 155 end
151   -
  156 +
152 157 def not_allowed!
153 158 render_api_error!(_('Method Not Allowed'), 405)
154 159 end
155   -
  160 +
156 161 def render_api_error!(message, status)
157 162 error!({'message' => message, :code => status}, status)
158 163 end
159   -
  164 +
160 165 def render_api_errors!(messages)
161 166 render_api_error!(messages.join(','), 400)
162 167 end
... ... @@ -169,7 +174,7 @@ module Noosfero
169 174 def setup_multitenancy
170 175 Noosfero::MultiTenancy.setup!(request.host)
171 176 end
172   -
  177 +
173 178 def detect_stuff_by_domain
174 179 @domain = Domain.find_by_name(request.host)
175 180 if @domain.nil?
... ... @@ -182,35 +187,35 @@ module Noosfero
182 187 @environment = @domain.environment
183 188 end
184 189 end
185   -
  190 +
186 191 private
187 192  
188   - def parser_params(params)
  193 + def parser_params(params)
189 194 parsed_params = {}
190   - params.map do |k,v|
  195 + params.map do |k,v|
191 196 parsed_params[k.to_sym] = v if ALLOWED_PARAMETERS.include?(k.to_sym)
192 197 end
193   - parsed_params
  198 + parsed_params
194 199 end
195   -
  200 +
196 201 def default_limit
197 202 20
198 203 end
199   -
  204 +
200 205 def parse_content_type(content_type)
201 206 return nil if content_type.blank?
202 207 content_type.split(',').map do |content_type|
203 208 content_type.camelcase
204 209 end
205 210 end
206   -
  211 +
207 212 def period(from_date, until_date)
208 213 begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date
209 214 end_period = until_date.nil? ? DateTime.now : until_date
210   -
  215 +
211 216 begin_period..end_period
212 217 end
213   -
  218 +
214 219 end
215 220 end
216 221 end
... ...