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