Commit 29ef17c701e3b1945efe1a158bc4d43add18779e
Exists in
staging
and in
4 other branches
Merge branch 'api' of gitlab.com:participa/noosfero into api
Showing
4 changed files
with
49 additions
and
20 deletions
Show diff stats
lib/noosfero/api/entities.rb
... | ... | @@ -2,12 +2,16 @@ module Noosfero |
2 | 2 | module API |
3 | 3 | module Entities |
4 | 4 | |
5 | - Grape::Entity.format_with :timestamp do |date| | |
5 | + Entity.format_with :timestamp do |date| | |
6 | 6 | date.strftime('%Y/%m/%d %H:%M:%S') if date |
7 | 7 | end |
8 | 8 | |
9 | - class Image < Grape::Entity | |
9 | + class Image < Entity | |
10 | 10 | root 'images', 'image' |
11 | + | |
12 | + expose :url do |image, options| | |
13 | + image.public_filename | |
14 | + end | |
11 | 15 | |
12 | 16 | expose :icon_url do |image, options| |
13 | 17 | image.public_filename(:icon) |
... | ... | @@ -26,7 +30,7 @@ module Noosfero |
26 | 30 | end |
27 | 31 | end |
28 | 32 | |
29 | - class Profile < Grape::Entity | |
33 | + class Profile < Entity | |
30 | 34 | expose :identifier, :name, :id |
31 | 35 | expose :created_at, :format_with => :timestamp |
32 | 36 | expose :image, :using => Image |
... | ... | @@ -43,13 +47,13 @@ module Noosfero |
43 | 47 | expose :description |
44 | 48 | end |
45 | 49 | |
46 | - class Category < Grape::Entity | |
50 | + class Category < Entity | |
47 | 51 | root 'categories', 'category' |
48 | 52 | expose :name, :id, :slug |
49 | 53 | expose :image, :using => Image |
50 | 54 | end |
51 | 55 | |
52 | - class ArticleChild < Grape::Entity | |
56 | + class ArticleChild < Entity | |
53 | 57 | root 'articles', 'article' |
54 | 58 | expose :id, :body, :abstract |
55 | 59 | expose :created_at, :format_with => :timestamp |
... | ... | @@ -59,8 +63,8 @@ module Noosfero |
59 | 63 | expose :categories, :using => Category |
60 | 64 | expose :image, :using => Image |
61 | 65 | end |
62 | - | |
63 | - class Article < Grape::Entity | |
66 | + | |
67 | + class Article < Entity | |
64 | 68 | root 'articles', 'article' |
65 | 69 | expose :id, :body, :abstract |
66 | 70 | expose :created_at, :format_with => :timestamp |
... | ... | @@ -69,12 +73,12 @@ module Noosfero |
69 | 73 | expose :profile, :using => Profile |
70 | 74 | expose :categories, :using => Category |
71 | 75 | # FIXME: create a method that overrides expose and include conditions for return attributes |
72 | - expose :parent, :using => Article, :if => lambda { |article, options| options[:fields].blank? || options[:fields].include?(:parent) } | |
76 | + expose :parent, :using => Article | |
73 | 77 | expose :children, :using => ArticleChild |
74 | 78 | expose :image, :using => Image |
75 | 79 | end |
76 | 80 | |
77 | - class Comment < Grape::Entity | |
81 | + class Comment < Entity | |
78 | 82 | root 'comments', 'comment' |
79 | 83 | expose :body, :title, :id |
80 | 84 | expose :created_at, :format_with => :timestamp |
... | ... | @@ -82,7 +86,7 @@ module Noosfero |
82 | 86 | end |
83 | 87 | |
84 | 88 | |
85 | - class User < Grape::Entity | |
89 | + class User < Entity | |
86 | 90 | root 'users', 'user' |
87 | 91 | expose :id |
88 | 92 | expose :login | ... | ... |
... | ... | @@ -0,0 +1,17 @@ |
1 | +class Noosfero::API::Entity < Grape::Entity | |
2 | + | |
3 | + def self.fields_condition(fields) | |
4 | + lambda do |object, options| | |
5 | + return true if options[:fields].blank? | |
6 | + fields.map { |field| options[:fields].include?(field.to_s)}.grep(true).present? | |
7 | + end | |
8 | + end | |
9 | + | |
10 | + def self.expose(*args, &block) | |
11 | + hash = args.last.is_a?(Hash) ? args.pop : {} | |
12 | + hash.merge!({:if => fields_condition(args)}) if hash[:if].blank? | |
13 | + args << hash | |
14 | + super | |
15 | + end | |
16 | + | |
17 | +end | ... | ... |
lib/noosfero/api/v1/articles.rb
... | ... | @@ -20,25 +20,25 @@ module Noosfero |
20 | 20 | get do |
21 | 21 | articles = select_filtered_collection_of(environment, 'articles', params) |
22 | 22 | articles = articles.display_filter(current_person, nil) |
23 | - present articles, :with => Entities::Article | |
23 | + present articles, :with => Entities::Article, :fields => params[:fields] | |
24 | 24 | end |
25 | 25 | |
26 | 26 | desc "Return the article id" |
27 | 27 | get ':id' do |
28 | 28 | article = find_article(environment.articles, params[:id]) |
29 | - present article, :with => Entities::Article | |
29 | + present article, :with => Entities::Article, :fields => params[:fields] | |
30 | 30 | end |
31 | 31 | |
32 | 32 | get ':id/children' do |
33 | 33 | article = find_article(environment.articles, params[:id]) |
34 | 34 | articles = select_filtered_collection_of(article, 'children', params) |
35 | 35 | articles = articles.display_filter(current_person, nil) |
36 | - present articles, :with => Entities::Article, :fields => [:id, :name, :abstract, :author] | |
36 | + present articles, :with => Entities::Article, :fields => params[:fields] | |
37 | 37 | end |
38 | 38 | |
39 | 39 | get ':id/children/:child_id' do |
40 | 40 | article = find_article(environment.articles, params[:id]) |
41 | - present find_article(article.children, params[:child_id]), :with => Entities::Article | |
41 | + present find_article(article.children, params[:child_id]), :with => Entities::Article, :fields => params[:fields] | |
42 | 42 | end |
43 | 43 | |
44 | 44 | # Example Request: |
... | ... | @@ -75,13 +75,13 @@ module Noosfero |
75 | 75 | community = environment.communities.find(params[:community_id]) |
76 | 76 | articles = select_filtered_collection_of(community, 'articles', params) |
77 | 77 | articles = articles.display_filter(current_person, community) |
78 | - present articles, :with => Entities::Article | |
78 | + present articles, :with => Entities::Article, :fields => params[:fields] | |
79 | 79 | end |
80 | 80 | |
81 | 81 | get ':id' do |
82 | 82 | community = environment.communities.find(params[:community_id]) |
83 | 83 | article = find_article(community.articles, params[:id]) |
84 | - present article, :with => Entities::Article | |
84 | + present article, :with => Entities::Article, :fields => params[:fields] | |
85 | 85 | end |
86 | 86 | |
87 | 87 | # Example Request: |
... | ... | @@ -116,13 +116,13 @@ module Noosfero |
116 | 116 | person = environment.people.find(params[:person_id]) |
117 | 117 | articles = select_filtered_collection_of(person, 'articles', params) |
118 | 118 | articles = articles.display_filter(current_person, person) |
119 | - present articles, :with => Entities::Article | |
119 | + present articles, :with => Entities::Article, :fields => params[:fields] | |
120 | 120 | end |
121 | 121 | |
122 | 122 | get ':id' do |
123 | 123 | person = environment.people.find(params[:person_id]) |
124 | 124 | article = find_article(person.articles, params[:id]) |
125 | - present article, :with => Entities::Article | |
125 | + present article, :with => Entities::Article, :fields => params[:fields] | |
126 | 126 | end |
127 | 127 | |
128 | 128 | post do |
... | ... | @@ -155,13 +155,13 @@ module Noosfero |
155 | 155 | enterprise = environment.enterprises.find(params[:enterprise_id]) |
156 | 156 | articles = select_filtered_collection_of(enterprise, 'articles', params) |
157 | 157 | articles = articles.display_filter(current_person, enterprise) |
158 | - present articles, :with => Entities::Article | |
158 | + present articles, :with => Entities::Article, :fields => params[:fields] | |
159 | 159 | end |
160 | 160 | |
161 | 161 | get ':id' do |
162 | 162 | enterprise = environment.enterprises.find(params[:enterprise_id]) |
163 | 163 | article = find_article(enterprise.articles, params[:id]) |
164 | - present article, :with => Entities::Article | |
164 | + present article, :with => Entities::Article, :fields => params[:fields] | |
165 | 165 | end |
166 | 166 | |
167 | 167 | post do | ... | ... |
test/unit/api/articles_test.rb
... | ... | @@ -442,5 +442,13 @@ class ArticlesTest < ActiveSupport::TestCase |
442 | 442 | assert_equal user.person, Article.last.last_changed_by |
443 | 443 | end |
444 | 444 | |
445 | + should 'list article children with partial fields' do | |
446 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | |
447 | + child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") | |
448 | + params[:fields] = [:title] | |
449 | + get "/api/v1/articles/#{article.id}/children?#{params.to_query}" | |
450 | + json = JSON.parse(last_response.body) | |
451 | + assert_equal ['title'], json['articles'].first.keys | |
452 | + end | |
445 | 453 | |
446 | 454 | end | ... | ... |