Commit a21569c41e766c97b4e3b0a407cda89fb0f59fec
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'api' of gitlab.com:participa/noosfero into api
Showing
5 changed files
with
46 additions
and
21 deletions
Show diff stats
Gemfile
| ... | ... | @@ -19,7 +19,7 @@ gem 'gettext', '~> 2.2.1', :require => false |
| 19 | 19 | gem 'locale', '~> 2.0.5' |
| 20 | 20 | gem 'whenever', :require => false |
| 21 | 21 | gem 'eita-jrails', '>= 0.9.5', :require => 'jrails' |
| 22 | -gem 'grape', '~> 0.8.0' | |
| 22 | +gem 'grape', '~> 0.11.0' | |
| 23 | 23 | gem 'grape-entity' |
| 24 | 24 | gem 'grape-swagger' |
| 25 | 25 | gem 'rack-cors' | ... | ... |
lib/noosfero/api/entities.rb
| ... | ... | @@ -2,11 +2,11 @@ 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 | 11 | |
| 12 | 12 | expose :url do |image, options| |
| ... | ... | @@ -30,7 +30,7 @@ module Noosfero |
| 30 | 30 | end |
| 31 | 31 | end |
| 32 | 32 | |
| 33 | - class Profile < Grape::Entity | |
| 33 | + class Profile < Entity | |
| 34 | 34 | expose :identifier, :name, :id |
| 35 | 35 | expose :created_at, :format_with => :timestamp |
| 36 | 36 | expose :image, :using => Image |
| ... | ... | @@ -47,13 +47,13 @@ module Noosfero |
| 47 | 47 | expose :description |
| 48 | 48 | end |
| 49 | 49 | |
| 50 | - class Category < Grape::Entity | |
| 50 | + class Category < Entity | |
| 51 | 51 | root 'categories', 'category' |
| 52 | 52 | expose :name, :id, :slug |
| 53 | 53 | expose :image, :using => Image |
| 54 | 54 | end |
| 55 | 55 | |
| 56 | - class ArticleChild < Grape::Entity | |
| 56 | + class ArticleChild < Entity | |
| 57 | 57 | root 'articles', 'article' |
| 58 | 58 | expose :id, :body, :abstract |
| 59 | 59 | expose :created_at, :format_with => :timestamp |
| ... | ... | @@ -63,8 +63,8 @@ module Noosfero |
| 63 | 63 | expose :categories, :using => Category |
| 64 | 64 | expose :image, :using => Image |
| 65 | 65 | end |
| 66 | - | |
| 67 | - class Article < Grape::Entity | |
| 66 | + | |
| 67 | + class Article < Entity | |
| 68 | 68 | root 'articles', 'article' |
| 69 | 69 | expose :id, :body, :abstract |
| 70 | 70 | expose :created_at, :format_with => :timestamp |
| ... | ... | @@ -73,12 +73,12 @@ module Noosfero |
| 73 | 73 | expose :profile, :using => Profile |
| 74 | 74 | expose :categories, :using => Category |
| 75 | 75 | # FIXME: create a method that overrides expose and include conditions for return attributes |
| 76 | - expose :parent, :using => Article, :if => lambda { |article, options| options[:fields].blank? || options[:fields].include?(:parent) } | |
| 76 | + expose :parent, :using => Article | |
| 77 | 77 | expose :children, :using => ArticleChild |
| 78 | 78 | expose :image, :using => Image |
| 79 | 79 | end |
| 80 | 80 | |
| 81 | - class Comment < Grape::Entity | |
| 81 | + class Comment < Entity | |
| 82 | 82 | root 'comments', 'comment' |
| 83 | 83 | expose :body, :title, :id |
| 84 | 84 | expose :created_at, :format_with => :timestamp |
| ... | ... | @@ -86,7 +86,7 @@ module Noosfero |
| 86 | 86 | end |
| 87 | 87 | |
| 88 | 88 | |
| 89 | - class User < Grape::Entity | |
| 89 | + class User < Entity | |
| 90 | 90 | root 'users', 'user' |
| 91 | 91 | expose :id |
| 92 | 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 | ... | ... |