Commit 1b83b35b7bf66cdb6fe97f62e625c1f495fc87fe
1 parent
1a453475
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
refactoring api
Showing
5 changed files
with
98 additions
and
53 deletions
Show diff stats
lib/api/api.rb
lib/api/entities.rb
1 | 1 | module API |
2 | 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 | 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 | 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 | 53 | end |
31 | 54 | |
32 | 55 | class Comment < Grape::Entity |
33 | 56 | root 'comments', 'comment' |
34 | 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 | 60 | end |
48 | 61 | |
62 | + | |
49 | 63 | class User < Grape::Entity |
50 | 64 | root 'users', 'user' |
51 | 65 | expose :login |
66 | + expose :person, :using => Profile | |
52 | 67 | end |
53 | 68 | |
54 | 69 | class UserLogin < User | ... | ... |
lib/api/helpers.rb
... | ... | @@ -32,13 +32,8 @@ module API |
32 | 32 | end |
33 | 33 | |
34 | 34 | def period(from_date, until_date) |
35 | - if from_date.nil? | |
36 | - begin_period = Time.at(0).to_datetime | |
37 | - end_period = until_date.nil? ? DateTime.now : until_date | |
38 | - else | |
39 | - begin_period = from_date | |
40 | - end_period = DateTime.now | |
41 | - end | |
35 | + begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date | |
36 | + end_period = until_date.nil? ? DateTime.now : until_date | |
42 | 37 | |
43 | 38 | begin_period...end_period |
44 | 39 | end |
... | ... | @@ -63,6 +58,18 @@ module API |
63 | 58 | conditions |
64 | 59 | end |
65 | 60 | |
61 | + | |
62 | + def select_filtered_collection_of(object, method, params) | |
63 | + conditions = make_conditions_with_parameter(params) | |
64 | + | |
65 | + if params[:reference_id] | |
66 | + 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") | |
67 | + else | |
68 | + objects = object.send(method).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | |
69 | + end | |
70 | + objects | |
71 | + end | |
72 | + | |
66 | 73 | #FIXME see if its needed |
67 | 74 | # def paginate(relation) |
68 | 75 | # per_page = params[:per_page].to_i | ... | ... |
lib/api/v1/articles.rb
... | ... | @@ -20,13 +20,7 @@ module API |
20 | 20 | # } |
21 | 21 | get do |
22 | 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 | |
23 | + articles = select_filtered_collection_of(environment, 'articles', params) | |
30 | 24 | present articles, :with => Entities::Article |
31 | 25 | end |
32 | 26 | |
... | ... | @@ -36,8 +30,6 @@ module API |
36 | 30 | end |
37 | 31 | |
38 | 32 | 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 | 33 | |
42 | 34 | conditions = make_conditions_with_parameter(params) |
43 | 35 | if params[:reference_id] |
... | ... | @@ -54,6 +46,35 @@ module API |
54 | 46 | |
55 | 47 | |
56 | 48 | end |
49 | + | |
50 | + resource :communities do | |
51 | + segment '/:community_id' do | |
52 | + resource :articles do | |
53 | + get do | |
54 | + community = environment.communities.find(params[:community_id]) | |
55 | + articles = select_filtered_collection_of(community, 'articles', params) | |
56 | + present articles, :with => Entities::Article | |
57 | + end | |
58 | + | |
59 | + get '/:id' do | |
60 | + community = environment.communities.find(params[:community_id]) | |
61 | + present community.articles.find(params[:id]), :with => Entities::Article | |
62 | + end | |
63 | + | |
64 | + # Example Request: | |
65 | + # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body | |
66 | + post do | |
67 | + community = environment.communities.find(params[:community_id]) | |
68 | + article = community.articles.build(params[:article].merge(:last_changed_by => current_person)) | |
69 | + article.type= params[:type].nil? ? 'TinyMceArticle' : params[:type] | |
70 | + article.save | |
71 | + present article, :with => Entities::Article | |
72 | + end | |
73 | + | |
74 | + end | |
75 | + end | |
76 | + | |
77 | + end | |
57 | 78 | |
58 | 79 | end |
59 | 80 | end | ... | ... |
lib/api/v1/users.rb
... | ... | @@ -2,18 +2,17 @@ module API |
2 | 2 | module V1 |
3 | 3 | class Users < Grape::API |
4 | 4 | |
5 | + before { detect_stuff_by_domain } | |
5 | 6 | before { authenticate! } |
6 | 7 | |
7 | 8 | resource :users do |
8 | 9 | |
9 | - #FIXME make the pagination | |
10 | - #FIXME put it on environment context | |
11 | 10 | get do |
12 | - present User.all, :with => Entities::User | |
11 | + present environment.users, :with => Entities::User | |
13 | 12 | end |
14 | 13 | |
15 | 14 | get ":id" do |
16 | - present User.find(params[:id]), :with => Entities::User | |
15 | + present environment.users.find(params[:id]), :with => Entities::User | |
17 | 16 | end |
18 | 17 | |
19 | 18 | end | ... | ... |