Commit 3d17bbb1a88d56c95ab7f1669e49c5989e9308b5
Committed by
Rodrigo Souto
1 parent
62b5dade
Exists in
master
and in
21 other branches
refactoring api
Showing
6 changed files
with
25 additions
and
69 deletions
Show diff stats
lib/api/entities.rb
@@ -2,6 +2,7 @@ module API | @@ -2,6 +2,7 @@ module API | ||
2 | module Entities | 2 | module Entities |
3 | 3 | ||
4 | class Image < Grape::Entity | 4 | class Image < Grape::Entity |
5 | + root 'images', 'image' | ||
5 | 6 | ||
6 | expose :icon_url do |image, options| | 7 | expose :icon_url do |image, options| |
7 | image.public_filename(:icon) | 8 | image.public_filename(:icon) |
@@ -28,10 +29,12 @@ module API | @@ -28,10 +29,12 @@ module API | ||
28 | class Person < Profile;end; | 29 | class Person < Profile;end; |
29 | class Enterprise < Profile;end; | 30 | class Enterprise < Profile;end; |
30 | class Community < Profile | 31 | class Community < Profile |
32 | + root 'communities', 'community' | ||
31 | expose :description | 33 | expose :description |
32 | end | 34 | end |
33 | 35 | ||
34 | class Category < Grape::Entity | 36 | class Category < Grape::Entity |
37 | + root 'categories', 'category' | ||
35 | expose :name, :id, :slug | 38 | expose :name, :id, :slug |
36 | expose :image, :using => Image | 39 | expose :image, :using => Image |
37 | end | 40 | end |
@@ -44,12 +47,6 @@ module API | @@ -44,12 +47,6 @@ module API | ||
44 | expose :author, :using => Profile | 47 | expose :author, :using => Profile |
45 | expose :profile, :using => Profile | 48 | expose :profile, :using => Profile |
46 | expose :categories, :using => Category | 49 | expose :categories, :using => Category |
47 | -#, :if => lambda { |instance, options| raise params.inspect } | ||
48 | -# do |instance, options| | ||
49 | -# # examine available environment keys with `p options[:env].keys` | ||
50 | -# options[:user] | ||
51 | -# end | ||
52 | - | ||
53 | end | 50 | end |
54 | 51 | ||
55 | class Comment < Grape::Entity | 52 | class Comment < Grape::Entity |
lib/api/helpers.rb
@@ -163,6 +163,9 @@ module API | @@ -163,6 +163,9 @@ module API | ||
163 | error!({'message' => message, :code => status}, status) | 163 | error!({'message' => message, :code => status}, status) |
164 | end | 164 | end |
165 | 165 | ||
166 | + def render_api_errors!(messages) | ||
167 | + render_api_error!(messages.join(','), 400) | ||
168 | + end | ||
166 | protected | 169 | protected |
167 | 170 | ||
168 | def detect_stuff_by_domain | 171 | def detect_stuff_by_domain |
lib/api/v1/articles.rb
@@ -66,7 +66,9 @@ module API | @@ -66,7 +66,9 @@ module API | ||
66 | community = environment.communities.find(params[:community_id]) | 66 | community = environment.communities.find(params[:community_id]) |
67 | article = community.articles.build(params[:article].merge(:last_changed_by => current_person)) | 67 | article = community.articles.build(params[:article].merge(:last_changed_by => current_person)) |
68 | article.type= params[:type].nil? ? 'TinyMceArticle' : params[:type] | 68 | article.type= params[:type].nil? ? 'TinyMceArticle' : params[:type] |
69 | - article.save | 69 | + if !article.save |
70 | + render_api_errors!(article.errors.full_messages) | ||
71 | + end | ||
70 | present article, :with => Entities::Article | 72 | present article, :with => Entities::Article |
71 | end | 73 | end |
72 | 74 |
lib/api/v1/communities.rb
@@ -16,27 +16,24 @@ module API | @@ -16,27 +16,24 @@ module API | ||
16 | # Example Request: | 16 | # Example Request: |
17 | # GET /communities?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 | 17 | # GET /communities?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 |
18 | # GET /communities?reference_id=10&limit=10&oldest | 18 | # GET /communities?reference_id=10&limit=10&oldest |
19 | -# desc 'Articles.', { | ||
20 | -# :params => API::Entities::Article.documentation | ||
21 | -# } | ||
22 | get do | 19 | get do |
23 | - conditions = make_conditions_with_parameter(params) | ||
24 | - | ||
25 | - if params[:reference_id] | ||
26 | - communities = environment.communities.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
27 | - else | ||
28 | - communities = environment.communities.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
29 | - end | 20 | + communities = select_filtered_collection_of(current_person, 'communities', params) |
30 | present communities, :with => Entities::Community | 21 | present communities, :with => Entities::Community |
31 | end | 22 | end |
32 | - | ||
33 | - desc "Return the article id" | 23 | + |
24 | + #FIXME See only public communities | ||
25 | + get '/all' do | ||
26 | + communities = select_filtered_collection_of(environment, 'communities', params) | ||
27 | + present communities, :with => Entities::Community | ||
28 | + end | ||
29 | + | ||
34 | get ':id' do | 30 | get ':id' do |
35 | - present environment.communities.find(params[:id]), :with => Entities::Community | 31 | + community = environment.communities.find(params[:id]) |
32 | + present community, :with => Entities::Community | ||
36 | end | 33 | end |
37 | 34 | ||
38 | end | 35 | end |
39 | - | 36 | + |
40 | end | 37 | end |
41 | end | 38 | end |
42 | end | 39 | end |
lib/api/v1/enterprises.rb
@@ -16,17 +16,8 @@ module API | @@ -16,17 +16,8 @@ module API | ||
16 | # Example Request: | 16 | # Example Request: |
17 | # GET /enterprises?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 | 17 | # GET /enterprises?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 |
18 | # GET /enterprises?reference_id=10&limit=10&oldest | 18 | # GET /enterprises?reference_id=10&limit=10&oldest |
19 | -# desc 'Articles.', { | ||
20 | -# :params => API::Entities::Article.documentation | ||
21 | -# } | ||
22 | get do | 19 | get do |
23 | - conditions = make_conditions_with_parameter(params) | ||
24 | - | ||
25 | - if params[:reference_id] | ||
26 | - enterprises = environment.enterprises.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
27 | - else | ||
28 | - enterprises = environment.enterprises.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
29 | - end | 20 | + enterprises = select_filtered_collection_of(environment, 'enterprises', params) |
30 | present enterprises, :with => Entities::Enterprise | 21 | present enterprises, :with => Entities::Enterprise |
31 | end | 22 | end |
32 | 23 |
lib/api/v1/people.rb
@@ -16,48 +16,14 @@ module API | @@ -16,48 +16,14 @@ module API | ||
16 | # Example Request: | 16 | # Example Request: |
17 | # GET /people?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 | 17 | # GET /people?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10 |
18 | # GET /people?reference_id=10&limit=10&oldest | 18 | # GET /people?reference_id=10&limit=10&oldest |
19 | -# desc 'Articles.', { | ||
20 | -# :params => API::Entities::Article.documentation | ||
21 | -# } | ||
22 | get do | 19 | get do |
23 | - conditions = make_conditions_with_parameter(params) | ||
24 | - | ||
25 | - if params[:reference_id] | ||
26 | - people = environment.people.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
27 | - else | ||
28 | - people = environment.people.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
29 | - end | 20 | + people = select_filtered_collection_of(environment, 'people', params) |
30 | present people, :with => Entities::Person | 21 | present people, :with => Entities::Person |
31 | end | 22 | end |
32 | - | ||
33 | 23 | ||
34 | - segment '/:person_id' do | ||
35 | - | ||
36 | - desc "Return the person information" | ||
37 | - get do | ||
38 | - present environment.people.find(params[:person_id]), :with => Entities::Person | ||
39 | - end | ||
40 | - | ||
41 | - resource '/communities' do | ||
42 | - desc "Return all communities of person" | ||
43 | - get do | ||
44 | - person = environment.people.find(params[:person_id]) | ||
45 | - conditions = make_conditions_with_parameter(params) | ||
46 | - | ||
47 | - if params[:reference_id] | ||
48 | - communities = person.communities.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
49 | - else | ||
50 | - communities = person.communities.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
51 | - end | ||
52 | - present communities, :with => Entities::Community | ||
53 | - end | ||
54 | - | ||
55 | - desc "Return all communities of person" | ||
56 | - get '/:id' do | ||
57 | - person = environment.people.find(params[:person_id]) | ||
58 | - present person.communities.find(params[:id]), :with => Entities::Community | ||
59 | - end | ||
60 | - end | 24 | + desc "Return the person information" |
25 | + get '/:id' do | ||
26 | + present environment.people.find(params[:id]), :with => Entities::Person | ||
61 | end | 27 | end |
62 | 28 | ||
63 | end | 29 | end |