Commit 29ef17c701e3b1945efe1a158bc4d43add18779e

Authored by Francisco Júnior
2 parents 14564ee3 a21569c4

Merge branch 'api' of gitlab.com:participa/noosfero into api

lib/noosfero/api/entities.rb
@@ -2,12 +2,16 @@ module Noosfero @@ -2,12 +2,16 @@ module Noosfero
2 module API 2 module API
3 module Entities 3 module Entities
4 4
5 - Grape::Entity.format_with :timestamp do |date| 5 + Entity.format_with :timestamp do |date|
6 date.strftime('%Y/%m/%d %H:%M:%S') if date 6 date.strftime('%Y/%m/%d %H:%M:%S') if date
7 end 7 end
8 8
9 - class Image < Grape::Entity 9 + class Image < Entity
10 root 'images', 'image' 10 root 'images', 'image'
  11 +
  12 + expose :url do |image, options|
  13 + image.public_filename
  14 + end
11 15
12 expose :icon_url do |image, options| 16 expose :icon_url do |image, options|
13 image.public_filename(:icon) 17 image.public_filename(:icon)
@@ -26,7 +30,7 @@ module Noosfero @@ -26,7 +30,7 @@ module Noosfero
26 end 30 end
27 end 31 end
28 32
29 - class Profile < Grape::Entity 33 + class Profile < Entity
30 expose :identifier, :name, :id 34 expose :identifier, :name, :id
31 expose :created_at, :format_with => :timestamp 35 expose :created_at, :format_with => :timestamp
32 expose :image, :using => Image 36 expose :image, :using => Image
@@ -43,13 +47,13 @@ module Noosfero @@ -43,13 +47,13 @@ module Noosfero
43 expose :description 47 expose :description
44 end 48 end
45 49
46 - class Category < Grape::Entity 50 + class Category < Entity
47 root 'categories', 'category' 51 root 'categories', 'category'
48 expose :name, :id, :slug 52 expose :name, :id, :slug
49 expose :image, :using => Image 53 expose :image, :using => Image
50 end 54 end
51 55
52 - class ArticleChild < Grape::Entity 56 + class ArticleChild < Entity
53 root 'articles', 'article' 57 root 'articles', 'article'
54 expose :id, :body, :abstract 58 expose :id, :body, :abstract
55 expose :created_at, :format_with => :timestamp 59 expose :created_at, :format_with => :timestamp
@@ -59,8 +63,8 @@ module Noosfero @@ -59,8 +63,8 @@ module Noosfero
59 expose :categories, :using => Category 63 expose :categories, :using => Category
60 expose :image, :using => Image 64 expose :image, :using => Image
61 end 65 end
62 -  
63 - class Article < Grape::Entity 66 +
  67 + class Article < Entity
64 root 'articles', 'article' 68 root 'articles', 'article'
65 expose :id, :body, :abstract 69 expose :id, :body, :abstract
66 expose :created_at, :format_with => :timestamp 70 expose :created_at, :format_with => :timestamp
@@ -69,12 +73,12 @@ module Noosfero @@ -69,12 +73,12 @@ module Noosfero
69 expose :profile, :using => Profile 73 expose :profile, :using => Profile
70 expose :categories, :using => Category 74 expose :categories, :using => Category
71 # FIXME: create a method that overrides expose and include conditions for return attributes 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 expose :children, :using => ArticleChild 77 expose :children, :using => ArticleChild
74 expose :image, :using => Image 78 expose :image, :using => Image
75 end 79 end
76 80
77 - class Comment < Grape::Entity 81 + class Comment < Entity
78 root 'comments', 'comment' 82 root 'comments', 'comment'
79 expose :body, :title, :id 83 expose :body, :title, :id
80 expose :created_at, :format_with => :timestamp 84 expose :created_at, :format_with => :timestamp
@@ -82,7 +86,7 @@ module Noosfero @@ -82,7 +86,7 @@ module Noosfero
82 end 86 end
83 87
84 88
85 - class User < Grape::Entity 89 + class User < Entity
86 root 'users', 'user' 90 root 'users', 'user'
87 expose :id 91 expose :id
88 expose :login 92 expose :login
lib/noosfero/api/entity.rb 0 → 100644
@@ -0,0 +1,17 @@ @@ -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,25 +20,25 @@ module Noosfero
20 get do 20 get do
21 articles = select_filtered_collection_of(environment, 'articles', params) 21 articles = select_filtered_collection_of(environment, 'articles', params)
22 articles = articles.display_filter(current_person, nil) 22 articles = articles.display_filter(current_person, nil)
23 - present articles, :with => Entities::Article 23 + present articles, :with => Entities::Article, :fields => params[:fields]
24 end 24 end
25 25
26 desc "Return the article id" 26 desc "Return the article id"
27 get ':id' do 27 get ':id' do
28 article = find_article(environment.articles, params[:id]) 28 article = find_article(environment.articles, params[:id])
29 - present article, :with => Entities::Article 29 + present article, :with => Entities::Article, :fields => params[:fields]
30 end 30 end
31 31
32 get ':id/children' do 32 get ':id/children' do
33 article = find_article(environment.articles, params[:id]) 33 article = find_article(environment.articles, params[:id])
34 articles = select_filtered_collection_of(article, 'children', params) 34 articles = select_filtered_collection_of(article, 'children', params)
35 articles = articles.display_filter(current_person, nil) 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 end 37 end
38 38
39 get ':id/children/:child_id' do 39 get ':id/children/:child_id' do
40 article = find_article(environment.articles, params[:id]) 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 end 42 end
43 43
44 # Example Request: 44 # Example Request:
@@ -75,13 +75,13 @@ module Noosfero @@ -75,13 +75,13 @@ module Noosfero
75 community = environment.communities.find(params[:community_id]) 75 community = environment.communities.find(params[:community_id])
76 articles = select_filtered_collection_of(community, 'articles', params) 76 articles = select_filtered_collection_of(community, 'articles', params)
77 articles = articles.display_filter(current_person, community) 77 articles = articles.display_filter(current_person, community)
78 - present articles, :with => Entities::Article 78 + present articles, :with => Entities::Article, :fields => params[:fields]
79 end 79 end
80 80
81 get ':id' do 81 get ':id' do
82 community = environment.communities.find(params[:community_id]) 82 community = environment.communities.find(params[:community_id])
83 article = find_article(community.articles, params[:id]) 83 article = find_article(community.articles, params[:id])
84 - present article, :with => Entities::Article 84 + present article, :with => Entities::Article, :fields => params[:fields]
85 end 85 end
86 86
87 # Example Request: 87 # Example Request:
@@ -116,13 +116,13 @@ module Noosfero @@ -116,13 +116,13 @@ module Noosfero
116 person = environment.people.find(params[:person_id]) 116 person = environment.people.find(params[:person_id])
117 articles = select_filtered_collection_of(person, 'articles', params) 117 articles = select_filtered_collection_of(person, 'articles', params)
118 articles = articles.display_filter(current_person, person) 118 articles = articles.display_filter(current_person, person)
119 - present articles, :with => Entities::Article 119 + present articles, :with => Entities::Article, :fields => params[:fields]
120 end 120 end
121 121
122 get ':id' do 122 get ':id' do
123 person = environment.people.find(params[:person_id]) 123 person = environment.people.find(params[:person_id])
124 article = find_article(person.articles, params[:id]) 124 article = find_article(person.articles, params[:id])
125 - present article, :with => Entities::Article 125 + present article, :with => Entities::Article, :fields => params[:fields]
126 end 126 end
127 127
128 post do 128 post do
@@ -155,13 +155,13 @@ module Noosfero @@ -155,13 +155,13 @@ module Noosfero
155 enterprise = environment.enterprises.find(params[:enterprise_id]) 155 enterprise = environment.enterprises.find(params[:enterprise_id])
156 articles = select_filtered_collection_of(enterprise, 'articles', params) 156 articles = select_filtered_collection_of(enterprise, 'articles', params)
157 articles = articles.display_filter(current_person, enterprise) 157 articles = articles.display_filter(current_person, enterprise)
158 - present articles, :with => Entities::Article 158 + present articles, :with => Entities::Article, :fields => params[:fields]
159 end 159 end
160 160
161 get ':id' do 161 get ':id' do
162 enterprise = environment.enterprises.find(params[:enterprise_id]) 162 enterprise = environment.enterprises.find(params[:enterprise_id])
163 article = find_article(enterprise.articles, params[:id]) 163 article = find_article(enterprise.articles, params[:id])
164 - present article, :with => Entities::Article 164 + present article, :with => Entities::Article, :fields => params[:fields]
165 end 165 end
166 166
167 post do 167 post do
test/unit/api/articles_test.rb
@@ -442,5 +442,13 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -442,5 +442,13 @@ class ArticlesTest &lt; ActiveSupport::TestCase
442 assert_equal user.person, Article.last.last_changed_by 442 assert_equal user.person, Article.last.last_changed_by
443 end 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 end 454 end