Commit 62b5dade20f847b39a51183011f316e4914412ef
Committed by
Rodrigo Souto
1 parent
691617be
Exists in
master
and in
21 other branches
refactoring api
Showing
5 changed files
with
98 additions
and
47 deletions
Show diff stats
lib/api/api.rb
@@ -13,6 +13,9 @@ module API | @@ -13,6 +13,9 @@ module API | ||
13 | mount V1::Articles | 13 | mount V1::Articles |
14 | mount V1::Comments | 14 | mount V1::Comments |
15 | mount V1::Users | 15 | mount V1::Users |
16 | + mount V1::Communities | ||
17 | + mount V1::People | ||
18 | + mount V1::Enterprises | ||
16 | mount Session | 19 | mount Session |
17 | 20 | ||
18 | end | 21 | end |
lib/api/entities.rb
1 | module API | 1 | module API |
2 | module Entities | 2 | module Entities |
3 | - class Article < Grape::Entity | ||
4 | - root 'articles', 'article' | ||
5 | - expose :id, :body, :created_at | ||
6 | - expose :title, :documentation => {:type => "String", :desc => "Title of the article"} | ||
7 | 3 | ||
8 | - expose :author do |article, options| | ||
9 | - { | ||
10 | - :id => article.author_id, | ||
11 | - :name => article.author_name, | ||
12 | - :icon_url => article.author_custom_image(:icon), | ||
13 | - :minor_url => article.author_custom_image(:minor), | ||
14 | - :portrait_url => article.author_custom_image(:portrait), | ||
15 | - :thumb_url => article.author_custom_image(:thumb), | ||
16 | - } | 4 | + class Image < Grape::Entity |
5 | + | ||
6 | + expose :icon_url do |image, options| | ||
7 | + image.public_filename(:icon) | ||
8 | + end | ||
9 | + | ||
10 | + expose :minor_url do |image, options| | ||
11 | + image.public_filename(:minor) | ||
17 | end | 12 | end |
18 | 13 | ||
19 | - expose :profile do |article, options| | ||
20 | - { | ||
21 | - :id => article.profile.id, | ||
22 | - :name => article.profile.name, | ||
23 | - :icon_url => article.profile.profile_custom_image(:icon), | ||
24 | - :minor_url => article.profile.profile_custom_image(:minor), | ||
25 | - :portrait_url => article.profile.profile_custom_image(:portrait), | ||
26 | - :thumb_url => article.profile.profile_custom_image(:thumb), | ||
27 | - } | 14 | + expose :portrait_url do |image, options| |
15 | + image.public_filename(:portrait) | ||
28 | end | 16 | end |
29 | 17 | ||
18 | + expose :thumb_url do |image, options| | ||
19 | + image.public_filename(:thumb) | ||
20 | + end | ||
21 | + end | ||
22 | + | ||
23 | + class Profile < Grape::Entity | ||
24 | + expose :identifier, :name, :created_at, :id | ||
25 | + expose :image, :using => Image | ||
26 | + end | ||
27 | + | ||
28 | + class Person < Profile;end; | ||
29 | + class Enterprise < Profile;end; | ||
30 | + class Community < Profile | ||
31 | + expose :description | ||
32 | + end | ||
33 | + | ||
34 | + class Category < Grape::Entity | ||
35 | + expose :name, :id, :slug | ||
36 | + expose :image, :using => Image | ||
37 | + end | ||
38 | + | ||
39 | + | ||
40 | + class Article < Grape::Entity | ||
41 | + root 'articles', 'article' | ||
42 | + expose :id, :body, :created_at | ||
43 | + expose :title, :documentation => {:type => "String", :desc => "Title of the article"} | ||
44 | + expose :author, :using => Profile | ||
45 | + expose :profile, :using => Profile | ||
46 | + 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 | + | ||
30 | end | 53 | end |
31 | 54 | ||
32 | class Comment < Grape::Entity | 55 | class Comment < Grape::Entity |
33 | root 'comments', 'comment' | 56 | root 'comments', 'comment' |
34 | expose :body, :title, :created_at, :id | 57 | expose :body, :title, :created_at, :id |
35 | 58 | ||
36 | - expose :author do |comment, options| | ||
37 | - { | ||
38 | - :id => comment.author_id, | ||
39 | - :name => comment.author_name, | ||
40 | - :icon_url => comment.author_custom_image(:icon), | ||
41 | - :minor_url => comment.author_custom_image(:minor), | ||
42 | - :portrait_url => comment.author_custom_image(:portrait), | ||
43 | - :thumb_url => comment.author_custom_image(:thumb), | ||
44 | - } | ||
45 | - end | ||
46 | - | 59 | + expose :author, :using => Profile |
47 | end | 60 | end |
48 | 61 | ||
62 | + | ||
49 | class User < Grape::Entity | 63 | class User < Grape::Entity |
50 | root 'users', 'user' | 64 | root 'users', 'user' |
51 | expose :login | 65 | expose :login |
66 | + expose :person, :using => Profile | ||
52 | end | 67 | end |
53 | 68 | ||
54 | class UserLogin < User | 69 | class UserLogin < User |
lib/api/helpers.rb
@@ -59,6 +59,18 @@ module API | @@ -59,6 +59,18 @@ module API | ||
59 | conditions | 59 | conditions |
60 | end | 60 | end |
61 | 61 | ||
62 | + | ||
63 | + def select_filtered_collection_of(object, method, params) | ||
64 | + conditions = make_conditions_with_parameter(params) | ||
65 | + | ||
66 | + if params[:reference_id] | ||
67 | + objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
68 | + else | ||
69 | + objects = object.send(method).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
70 | + end | ||
71 | + objects | ||
72 | + end | ||
73 | + | ||
62 | #FIXME see if its needed | 74 | #FIXME see if its needed |
63 | # def paginate(relation) | 75 | # def paginate(relation) |
64 | # per_page = params[:per_page].to_i | 76 | # per_page = params[:per_page].to_i |
lib/api/v1/articles.rb
@@ -19,15 +19,8 @@ module API | @@ -19,15 +19,8 @@ module API | ||
19 | # :params => API::Entities::Article.documentation | 19 | # :params => API::Entities::Article.documentation |
20 | # } | 20 | # } |
21 | get do | 21 | get do |
22 | - | ||
23 | - conditions = make_conditions_with_parameter(params) | ||
24 | - | ||
25 | - if params[:reference_id] | ||
26 | - articles = environment.articles.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
27 | - else | ||
28 | - articles = environment.articles.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
29 | - end | ||
30 | - present articles, :with => Entities::Article | 22 | + articles = select_filtered_collection_of(environment, 'articles', params) |
23 | + present articles, :with => Entities::Article | ||
31 | end | 24 | end |
32 | 25 | ||
33 | desc "Return the article id" | 26 | desc "Return the article id" |
@@ -36,8 +29,6 @@ module API | @@ -36,8 +29,6 @@ module API | ||
36 | end | 29 | end |
37 | 30 | ||
38 | get ':id/children' do | 31 | get ':id/children' do |
39 | - from_date = DateTime.parse(params[:from]) if params[:from] | ||
40 | - until_date = DateTime.parse(params[:until]) if params[:until] | ||
41 | 32 | ||
42 | conditions = make_conditions_with_parameter(params) | 33 | conditions = make_conditions_with_parameter(params) |
43 | if params[:reference_id] | 34 | if params[:reference_id] |
@@ -55,6 +46,35 @@ module API | @@ -55,6 +46,35 @@ module API | ||
55 | 46 | ||
56 | end | 47 | end |
57 | 48 | ||
49 | + resource :communities do | ||
50 | + segment '/:community_id' do | ||
51 | + resource :articles do | ||
52 | + get do | ||
53 | + community = environment.communities.find(params[:community_id]) | ||
54 | + articles = select_filtered_collection_of(community, 'articles', params) | ||
55 | + present articles, :with => Entities::Article | ||
56 | + end | ||
57 | + | ||
58 | + get '/:id' do | ||
59 | + community = environment.communities.find(params[:community_id]) | ||
60 | + present community.articles.find(params[:id]), :with => Entities::Article | ||
61 | + end | ||
62 | + | ||
63 | + # Example Request: | ||
64 | + # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body | ||
65 | + post do | ||
66 | + community = environment.communities.find(params[:community_id]) | ||
67 | + article = community.articles.build(params[:article].merge(:last_changed_by => current_person)) | ||
68 | + article.type= params[:type].nil? ? 'TinyMceArticle' : params[:type] | ||
69 | + article.save | ||
70 | + present article, :with => Entities::Article | ||
71 | + end | ||
72 | + | ||
73 | + end | ||
74 | + end | ||
75 | + | ||
76 | + end | ||
77 | + | ||
58 | end | 78 | end |
59 | end | 79 | end |
60 | end | 80 | end |
lib/api/v1/users.rb
1 | module API | 1 | module API |
2 | module V1 | 2 | module V1 |
3 | class Users < Grape::API | 3 | class Users < Grape::API |
4 | - | 4 | + |
5 | + before { detect_stuff_by_domain } | ||
5 | before { authenticate! } | 6 | before { authenticate! } |
6 | 7 | ||
7 | resource :users do | 8 | resource :users do |
@@ -9,11 +10,11 @@ module API | @@ -9,11 +10,11 @@ module API | ||
9 | #FIXME make the pagination | 10 | #FIXME make the pagination |
10 | #FIXME put it on environment context | 11 | #FIXME put it on environment context |
11 | get do | 12 | get do |
12 | - present User.all, :with => Entities::User | 13 | + present environment.users, :with => Entities::User |
13 | end | 14 | end |
14 | 15 | ||
15 | get ":id" do | 16 | get ":id" do |
16 | - present User.find(params[:id]), :with => Entities::User | 17 | + present environment.users.find(params[:id]), :with => Entities::User |
17 | end | 18 | end |
18 | 19 | ||
19 | end | 20 | end |