diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 45d0b28..eea067e 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -2,6 +2,7 @@ module API module Entities class Image < Grape::Entity + root 'images', 'image' expose :icon_url do |image, options| image.public_filename(:icon) @@ -28,10 +29,12 @@ module API class Person < Profile;end; class Enterprise < Profile;end; class Community < Profile + root 'communities', 'community' expose :description end class Category < Grape::Entity + root 'categories', 'category' expose :name, :id, :slug expose :image, :using => Image end @@ -44,12 +47,6 @@ module API expose :author, :using => Profile expose :profile, :using => Profile expose :categories, :using => Category -#, :if => lambda { |instance, options| raise params.inspect } -# do |instance, options| -# # examine available environment keys with `p options[:env].keys` -# options[:user] -# end - end class Comment < Grape::Entity diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 845e386..e92aebf 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -163,6 +163,9 @@ module API error!({'message' => message, :code => status}, status) end + def render_api_errors!(messages) + render_api_error!(messages.join(','), 400) + end protected def detect_stuff_by_domain diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb index 50d592b..b114d0b 100644 --- a/lib/api/v1/articles.rb +++ b/lib/api/v1/articles.rb @@ -66,7 +66,9 @@ module API community = environment.communities.find(params[:community_id]) article = community.articles.build(params[:article].merge(:last_changed_by => current_person)) article.type= params[:type].nil? ? 'TinyMceArticle' : params[:type] - article.save + if !article.save + render_api_errors!(article.errors.full_messages) + end present article, :with => Entities::Article end diff --git a/lib/api/v1/communities.rb b/lib/api/v1/communities.rb index 1f51ffb..1332397 100644 --- a/lib/api/v1/communities.rb +++ b/lib/api/v1/communities.rb @@ -16,27 +16,24 @@ module API # Example Request: # GET /communities?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 # GET /communities?reference_id=10&limit=10&oldest -# desc 'Articles.', { -# :params => API::Entities::Article.documentation -# } get do - conditions = make_conditions_with_parameter(params) - - if params[:reference_id] - communities = environment.communities.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - else - communities = environment.communities.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - end + communities = select_filtered_collection_of(current_person, 'communities', params) present communities, :with => Entities::Community end - - desc "Return the article id" + + #FIXME See only public communities + get '/all' do + communities = select_filtered_collection_of(environment, 'communities', params) + present communities, :with => Entities::Community + end + get ':id' do - present environment.communities.find(params[:id]), :with => Entities::Community + community = environment.communities.find(params[:id]) + present community, :with => Entities::Community end end - + end end end diff --git a/lib/api/v1/enterprises.rb b/lib/api/v1/enterprises.rb index fc8615e..6116f8c 100644 --- a/lib/api/v1/enterprises.rb +++ b/lib/api/v1/enterprises.rb @@ -16,17 +16,8 @@ module API # Example Request: # GET /enterprises?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 # GET /enterprises?reference_id=10&limit=10&oldest -# desc 'Articles.', { -# :params => API::Entities::Article.documentation -# } get do - conditions = make_conditions_with_parameter(params) - - if params[:reference_id] - enterprises = environment.enterprises.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - else - enterprises = environment.enterprises.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - end + enterprises = select_filtered_collection_of(environment, 'enterprises', params) present enterprises, :with => Entities::Enterprise end diff --git a/lib/api/v1/people.rb b/lib/api/v1/people.rb index b6e6837..fd10dca 100644 --- a/lib/api/v1/people.rb +++ b/lib/api/v1/people.rb @@ -16,48 +16,14 @@ module API # Example Request: # GET /people?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 # GET /people?reference_id=10&limit=10&oldest -# desc 'Articles.', { -# :params => API::Entities::Article.documentation -# } get do - conditions = make_conditions_with_parameter(params) - - if params[:reference_id] - people = environment.people.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - else - people = environment.people.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - end + people = select_filtered_collection_of(environment, 'people', params) present people, :with => Entities::Person end - - segment '/:person_id' do - - desc "Return the person information" - get do - present environment.people.find(params[:person_id]), :with => Entities::Person - end - - resource '/communities' do - desc "Return all communities of person" - get do - person = environment.people.find(params[:person_id]) - conditions = make_conditions_with_parameter(params) - - if params[:reference_id] - communities = person.communities.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - else - communities = person.communities.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - end - present communities, :with => Entities::Community - end - - desc "Return all communities of person" - get '/:id' do - person = environment.people.find(params[:person_id]) - present person.communities.find(params[:id]), :with => Entities::Community - end - end + desc "Return the person information" + get '/:id' do + present environment.people.find(params[:id]), :with => Entities::Person end end -- libgit2 0.21.2