diff --git a/lib/api/api.rb b/lib/api/api.rb index 8f7ea97..cb4535a 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -13,6 +13,9 @@ module API mount V1::Articles mount V1::Comments mount V1::Users + mount V1::Communities + mount V1::People + mount V1::Enterprises mount Session end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 1750d50..45d0b28 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -1,54 +1,69 @@ module API module Entities - class Article < Grape::Entity - root 'articles', 'article' - expose :id, :body, :created_at - expose :title, :documentation => {:type => "String", :desc => "Title of the article"} - expose :author do |article, options| - { - :id => article.author_id, - :name => article.author_name, - :icon_url => article.author_custom_image(:icon), - :minor_url => article.author_custom_image(:minor), - :portrait_url => article.author_custom_image(:portrait), - :thumb_url => article.author_custom_image(:thumb), - } + class Image < Grape::Entity + + expose :icon_url do |image, options| + image.public_filename(:icon) + end + + expose :minor_url do |image, options| + image.public_filename(:minor) end - expose :profile do |article, options| - { - :id => article.profile.id, - :name => article.profile.name, - :icon_url => article.profile.profile_custom_image(:icon), - :minor_url => article.profile.profile_custom_image(:minor), - :portrait_url => article.profile.profile_custom_image(:portrait), - :thumb_url => article.profile.profile_custom_image(:thumb), - } + expose :portrait_url do |image, options| + image.public_filename(:portrait) end + expose :thumb_url do |image, options| + image.public_filename(:thumb) + end + end + + class Profile < Grape::Entity + expose :identifier, :name, :created_at, :id + expose :image, :using => Image + end + + class Person < Profile;end; + class Enterprise < Profile;end; + class Community < Profile + expose :description + end + + class Category < Grape::Entity + expose :name, :id, :slug + expose :image, :using => Image + end + + + class Article < Grape::Entity + root 'articles', 'article' + expose :id, :body, :created_at + expose :title, :documentation => {:type => "String", :desc => "Title of the article"} + 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 root 'comments', 'comment' expose :body, :title, :created_at, :id - expose :author do |comment, options| - { - :id => comment.author_id, - :name => comment.author_name, - :icon_url => comment.author_custom_image(:icon), - :minor_url => comment.author_custom_image(:minor), - :portrait_url => comment.author_custom_image(:portrait), - :thumb_url => comment.author_custom_image(:thumb), - } - end - + expose :author, :using => Profile end + class User < Grape::Entity root 'users', 'user' expose :login + expose :person, :using => Profile end class UserLogin < User diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 126a442..845e386 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -59,6 +59,18 @@ module API conditions end + + def select_filtered_collection_of(object, method, params) + conditions = make_conditions_with_parameter(params) + + if params[:reference_id] + 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") + else + objects = object.send(method).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") + end + objects + end + #FIXME see if its needed # def paginate(relation) # per_page = params[:per_page].to_i diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb index cb1b0db..50d592b 100644 --- a/lib/api/v1/articles.rb +++ b/lib/api/v1/articles.rb @@ -19,15 +19,8 @@ module API # :params => API::Entities::Article.documentation # } get do - - conditions = make_conditions_with_parameter(params) - - if params[:reference_id] - articles = environment.articles.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - else - articles = environment.articles.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") - end - present articles, :with => Entities::Article + articles = select_filtered_collection_of(environment, 'articles', params) + present articles, :with => Entities::Article end desc "Return the article id" @@ -36,8 +29,6 @@ module API end get ':id/children' do - from_date = DateTime.parse(params[:from]) if params[:from] - until_date = DateTime.parse(params[:until]) if params[:until] conditions = make_conditions_with_parameter(params) if params[:reference_id] @@ -55,6 +46,35 @@ module API end + resource :communities do + segment '/:community_id' do + resource :articles do + get do + community = environment.communities.find(params[:community_id]) + articles = select_filtered_collection_of(community, 'articles', params) + present articles, :with => Entities::Article + end + + get '/:id' do + community = environment.communities.find(params[:community_id]) + present community.articles.find(params[:id]), :with => Entities::Article + end + + # Example Request: + # POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body + post do + 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 + present article, :with => Entities::Article + end + + end + end + + end + end end end diff --git a/lib/api/v1/users.rb b/lib/api/v1/users.rb index 6113b7e..32a7e49 100644 --- a/lib/api/v1/users.rb +++ b/lib/api/v1/users.rb @@ -1,7 +1,8 @@ module API module V1 class Users < Grape::API - + + before { detect_stuff_by_domain } before { authenticate! } resource :users do @@ -9,11 +10,11 @@ module API #FIXME make the pagination #FIXME put it on environment context get do - present User.all, :with => Entities::User + present environment.users, :with => Entities::User end get ":id" do - present User.find(params[:id]), :with => Entities::User + present environment.users.find(params[:id]), :with => Entities::User end end -- libgit2 0.21.2