Commit 2ecd965861a08b94eaf562b1abd5263800817ca4
Exists in
staging
and in
4 other branches
Merge branch 'noosfero_grape_api' into rails3_api
Conflicts: lib/api/helpers.rb lib/api/v1/articles.rb lib/api/v1/users.rb
Showing
9 changed files
with
218 additions
and
53 deletions
Show diff stats
app/models/profile.rb
| @@ -89,6 +89,16 @@ class Profile < ActiveRecord::Base | @@ -89,6 +89,16 @@ class Profile < ActiveRecord::Base | ||
| 89 | scope :templates, {:conditions => {:is_template => true}} | 89 | scope :templates, {:conditions => {:is_template => true}} |
| 90 | scope :no_templates, {:conditions => {:is_template => false}} | 90 | scope :no_templates, {:conditions => {:is_template => false}} |
| 91 | 91 | ||
| 92 | + #FIXME make this test | ||
| 93 | + scope :newer_than, lambda { |reference_id| | ||
| 94 | + {:conditions => ["profiles.id > #{reference_id}"]} | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + #FIXME make this test | ||
| 98 | + scope :older_than, lambda { |reference_id| | ||
| 99 | + {:conditions => ["profiles.id < #{reference_id}"]} | ||
| 100 | + } | ||
| 101 | + | ||
| 92 | def members | 102 | def members |
| 93 | scopes = plugins.dispatch_scopes(:organization_members, self) | 103 | scopes = plugins.dispatch_scopes(:organization_members, self) |
| 94 | scopes << Person.members_of(self) | 104 | scopes << Person.members_of(self) |
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 | + root 'images', 'image' | ||
| 6 | + | ||
| 7 | + expose :icon_url do |image, options| | ||
| 8 | + image.public_filename(:icon) | ||
| 9 | + end | ||
| 10 | + | ||
| 11 | + expose :minor_url do |image, options| | ||
| 12 | + image.public_filename(:minor) | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + expose :portrait_url do |image, options| | ||
| 16 | + image.public_filename(:portrait) | ||
| 17 | end | 17 | end |
| 18 | 18 | ||
| 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 | - } | 19 | + expose :thumb_url do |image, options| |
| 20 | + image.public_filename(:thumb) | ||
| 28 | end | 21 | end |
| 22 | + end | ||
| 23 | + | ||
| 24 | + class Profile < Grape::Entity | ||
| 25 | + expose :identifier, :name, :created_at, :id | ||
| 26 | + expose :image, :using => Image | ||
| 27 | + end | ||
| 29 | 28 | ||
| 29 | + class Person < Profile;end; | ||
| 30 | + class Enterprise < Profile;end; | ||
| 31 | + class Community < Profile | ||
| 32 | + root 'communities', 'community' | ||
| 33 | + expose :description | ||
| 34 | + end | ||
| 35 | + | ||
| 36 | + class Category < Grape::Entity | ||
| 37 | + root 'categories', 'category' | ||
| 38 | + expose :name, :id, :slug | ||
| 39 | + expose :image, :using => Image | ||
| 40 | + end | ||
| 41 | + | ||
| 42 | + | ||
| 43 | + class Article < Grape::Entity | ||
| 44 | + root 'articles', 'article' | ||
| 45 | + expose :id, :body, :created_at | ||
| 46 | + expose :title, :documentation => {:type => "String", :desc => "Title of the article"} | ||
| 47 | + expose :author, :using => Profile | ||
| 48 | + expose :profile, :using => Profile | ||
| 49 | + expose :categories, :using => Category | ||
| 30 | end | 50 | end |
| 31 | 51 | ||
| 32 | class Comment < Grape::Entity | 52 | class Comment < Grape::Entity |
| 33 | root 'comments', 'comment' | 53 | root 'comments', 'comment' |
| 34 | expose :body, :title, :created_at, :id | 54 | expose :body, :title, :created_at, :id |
| 35 | 55 | ||
| 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 | - | 56 | + expose :author, :using => Profile |
| 47 | end | 57 | end |
| 48 | 58 | ||
| 59 | + | ||
| 49 | class User < Grape::Entity | 60 | class User < Grape::Entity |
| 50 | root 'users', 'user' | 61 | root 'users', 'user' |
| 51 | expose :login | 62 | expose :login |
| 63 | + expose :person, :using => Profile | ||
| 52 | end | 64 | end |
| 53 | 65 | ||
| 54 | class UserLogin < User | 66 | class UserLogin < User |
lib/api/helpers.rb
| @@ -32,12 +32,10 @@ module API | @@ -32,12 +32,10 @@ module API | ||
| 32 | end | 32 | end |
| 33 | 33 | ||
| 34 | def period(from_date, until_date) | 34 | def period(from_date, until_date) |
| 35 | - return nil if from_date.nil? && until_date.nil? | ||
| 36 | - | ||
| 37 | begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date | 35 | begin_period = from_date.nil? ? Time.at(0).to_datetime : from_date |
| 38 | end_period = until_date.nil? ? DateTime.now : until_date | 36 | end_period = until_date.nil? ? DateTime.now : until_date |
| 39 | 37 | ||
| 40 | - begin_period..end_period | 38 | + begin_period...end_period |
| 41 | end | 39 | end |
| 42 | 40 | ||
| 43 | def parse_content_type(content_type) | 41 | def parse_content_type(content_type) |
| @@ -59,6 +57,18 @@ module API | @@ -59,6 +57,18 @@ module API | ||
| 59 | conditions | 57 | conditions |
| 60 | end | 58 | end |
| 61 | 59 | ||
| 60 | + | ||
| 61 | + def select_filtered_collection_of(object, method, params) | ||
| 62 | + conditions = make_conditions_with_parameter(params) | ||
| 63 | + | ||
| 64 | + if params[:reference_id] | ||
| 65 | + 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") | ||
| 66 | + else | ||
| 67 | + objects = object.send(method).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") | ||
| 68 | + end | ||
| 69 | + objects | ||
| 70 | + end | ||
| 71 | + | ||
| 62 | #FIXME see if its needed | 72 | #FIXME see if its needed |
| 63 | # def paginate(relation) | 73 | # def paginate(relation) |
| 64 | # per_page = params[:per_page].to_i | 74 | # per_page = params[:per_page].to_i |
| @@ -151,6 +161,9 @@ module API | @@ -151,6 +161,9 @@ module API | ||
| 151 | error!({'message' => message, :code => status}, status) | 161 | error!({'message' => message, :code => status}, status) |
| 152 | end | 162 | end |
| 153 | 163 | ||
| 164 | + def render_api_errors!(messages) | ||
| 165 | + render_api_error!(messages.join(','), 400) | ||
| 166 | + end | ||
| 154 | protected | 167 | protected |
| 155 | 168 | ||
| 156 | def detect_stuff_by_domain | 169 | def detect_stuff_by_domain |
lib/api/v1/articles.rb
| @@ -19,15 +19,9 @@ module API | @@ -19,15 +19,9 @@ 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 | + |
| 23 | + articles = select_filtered_collection_of(environment, 'articles', params) | ||
| 24 | + present articles, :with => Entities::Article | ||
| 31 | end | 25 | end |
| 32 | 26 | ||
| 33 | desc "Return the article id" | 27 | desc "Return the article id" |
| @@ -36,8 +30,6 @@ module API | @@ -36,8 +30,6 @@ module API | ||
| 36 | end | 30 | end |
| 37 | 31 | ||
| 38 | get ':id/children' do | 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 | conditions = make_conditions_with_parameter(params) | 34 | conditions = make_conditions_with_parameter(params) |
| 43 | if params[:reference_id] | 35 | if params[:reference_id] |
| @@ -55,6 +47,37 @@ module API | @@ -55,6 +47,37 @@ module API | ||
| 55 | 47 | ||
| 56 | end | 48 | end |
| 57 | 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 | + if !article.save | ||
| 71 | + render_api_errors!(article.errors.full_messages) | ||
| 72 | + end | ||
| 73 | + present article, :with => Entities::Article | ||
| 74 | + end | ||
| 75 | + | ||
| 76 | + end | ||
| 77 | + end | ||
| 78 | + | ||
| 79 | + end | ||
| 80 | + | ||
| 58 | end | 81 | end |
| 59 | end | 82 | end |
| 60 | end | 83 | end |
| @@ -0,0 +1,39 @@ | @@ -0,0 +1,39 @@ | ||
| 1 | +module API | ||
| 2 | + module V1 | ||
| 3 | + class Communities < Grape::API | ||
| 4 | + before { detect_stuff_by_domain } | ||
| 5 | + before { authenticate! } | ||
| 6 | + | ||
| 7 | + resource :communities do | ||
| 8 | + | ||
| 9 | + # Collect comments from articles | ||
| 10 | + # | ||
| 11 | + # Parameters: | ||
| 12 | + # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created | ||
| 13 | + # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected | ||
| 14 | + # limit - amount of comments returned. The default value is 20 | ||
| 15 | + # | ||
| 16 | + # Example Request: | ||
| 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 | ||
| 19 | + get do | ||
| 20 | + communities = select_filtered_collection_of(current_person, 'communities', params) | ||
| 21 | + present communities, :with => Entities::Community | ||
| 22 | + end | ||
| 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 | + | ||
| 30 | + get ':id' do | ||
| 31 | + community = environment.communities.find(params[:id]) | ||
| 32 | + present community, :with => Entities::Community | ||
| 33 | + end | ||
| 34 | + | ||
| 35 | + end | ||
| 36 | + | ||
| 37 | + end | ||
| 38 | + end | ||
| 39 | +end |
| @@ -0,0 +1,33 @@ | @@ -0,0 +1,33 @@ | ||
| 1 | +module API | ||
| 2 | + module V1 | ||
| 3 | + class Enterprises < Grape::API | ||
| 4 | + before { detect_stuff_by_domain } | ||
| 5 | + before { authenticate! } | ||
| 6 | + | ||
| 7 | + resource :enterprises do | ||
| 8 | + | ||
| 9 | + # Collect comments from articles | ||
| 10 | + # | ||
| 11 | + # Parameters: | ||
| 12 | + # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created | ||
| 13 | + # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected | ||
| 14 | + # limit - amount of comments returned. The default value is 20 | ||
| 15 | + # | ||
| 16 | + # Example Request: | ||
| 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 | ||
| 19 | + get do | ||
| 20 | + enterprises = select_filtered_collection_of(environment, 'enterprises', params) | ||
| 21 | + present enterprises, :with => Entities::Enterprise | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + desc "Return the article id" | ||
| 25 | + get ':id' do | ||
| 26 | + present environment.enterprises.find(params[:id]), :with => Entities::Enterprise | ||
| 27 | + end | ||
| 28 | + | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | + end | ||
| 32 | + end | ||
| 33 | +end |
| @@ -0,0 +1,33 @@ | @@ -0,0 +1,33 @@ | ||
| 1 | +module API | ||
| 2 | + module V1 | ||
| 3 | + class People < Grape::API | ||
| 4 | + before { detect_stuff_by_domain } | ||
| 5 | + before { authenticate! } | ||
| 6 | + | ||
| 7 | + resource :people do | ||
| 8 | + | ||
| 9 | + # Collect comments from articles | ||
| 10 | + # | ||
| 11 | + # Parameters: | ||
| 12 | + # from - date where the search will begin. If nothing is passed the default date will be the date of the first article created | ||
| 13 | + # oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected | ||
| 14 | + # limit - amount of comments returned. The default value is 20 | ||
| 15 | + # | ||
| 16 | + # Example Request: | ||
| 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 | ||
| 19 | + get do | ||
| 20 | + people = select_filtered_collection_of(environment, 'people', params) | ||
| 21 | + present people, :with => Entities::Person | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + desc "Return the person information" | ||
| 25 | + get '/:id' do | ||
| 26 | + present environment.people.find(params[:id]), :with => Entities::Person | ||
| 27 | + end | ||
| 28 | + | ||
| 29 | + end | ||
| 30 | + | ||
| 31 | + end | ||
| 32 | + end | ||
| 33 | +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 |
| 8 | - | ||
| 9 | - #FIXME make the pagination | ||
| 10 | - #FIXME put it on environment context | 9 | + |
| 11 | get do | 10 | get do |
| 12 | - present User.all, :with => Entities::User | 11 | + present environment.users, :with => Entities::User |
| 13 | end | 12 | end |
| 14 | 13 | ||
| 15 | get ":id" do | 14 | get ":id" do |
| 16 | - present User.find(params[:id]), :with => Entities::User | 15 | + present environment.users.find(params[:id]), :with => Entities::User |
| 17 | end | 16 | end |
| 18 | 17 | ||
| 19 | end | 18 | end |