Commit 7c4b4a95b827d24a5bbb43df707e6d0765f32ad1

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent e493f05a

refactoring api

lib/api/api.rb
... ... @@ -13,6 +13,9 @@ module API
13 13 mount V1::Articles
14 14 mount V1::Comments
15 15 mount V1::Users
  16 + mount V1::Communities
  17 + mount V1::People
  18 + mount V1::Enterprises
16 19 mount Session
17 20  
18 21 end
... ...
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
... ... @@ -59,6 +59,18 @@ module API
59 59 conditions
60 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 74 #FIXME see if its needed
63 75 # def paginate(relation)
64 76 # per_page = params[:per_page].to_i
... ...
lib/api/v1/articles.rb
... ... @@ -19,15 +19,8 @@ module API
19 19 # :params => API::Entities::Article.documentation
20 20 # }
21 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 24 end
32 25  
33 26 desc "Return the article id"
... ... @@ -36,8 +29,6 @@ module API
36 29 end
37 30  
38 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 33 conditions = make_conditions_with_parameter(params)
43 34 if params[:reference_id]
... ... @@ -55,6 +46,35 @@ module API
55 46  
56 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 78 end
59 79 end
60 80 end
... ...
lib/api/v1/users.rb
1 1 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
... ... @@ -9,11 +10,11 @@ module API
9 10 #FIXME make the pagination
10 11 #FIXME put it on environment context
11 12 get do
12   - present User.all, :with => Entities::User
  13 + present environment.users, :with => Entities::User
13 14 end
14 15  
15 16 get ":id" do
16   - present User.find(params[:id]), :with => Entities::User
  17 + present environment.users.find(params[:id]), :with => Entities::User
17 18 end
18 19  
19 20 end
... ...