Commit 339fa73032bad31d82a94778523a0d698bd36a6b
Committed by
Rodrigo Souto
1 parent
edaa7736
Exists in
api_tasks
and in
4 other branches
adding initial infrastruture for noosfero api
Showing
4 changed files
with
88 additions
and
0 deletions
Show diff stats
config.ru
... | ... | @@ -2,3 +2,20 @@ |
2 | 2 | |
3 | 3 | require ::File.expand_path('../config/environment', __FILE__) |
4 | 4 | run Noosfero::Application |
5 | +require "config/environment" | |
6 | + | |
7 | + | |
8 | +#use Rails::Rack::LogTailer | |
9 | +#use Rails::Rack::Static | |
10 | +#run ActionController::Dispatcher.new | |
11 | + | |
12 | +rails_app = Rack::Builder.new do | |
13 | + use Rails::Rack::LogTailer | |
14 | + use Rails::Rack::Static | |
15 | + run ActionController::Dispatcher.new | |
16 | +end | |
17 | + | |
18 | +run Rack::Cascade.new([ | |
19 | + API::API, | |
20 | + rails_app | |
21 | +]) | ... | ... |
... | ... | @@ -0,0 +1,18 @@ |
1 | +require 'grape' | |
2 | + | |
3 | +module API | |
4 | + class API < Grape::API | |
5 | + version 'v1' | |
6 | + prefix "api" | |
7 | + format :json | |
8 | + content_type :txt, "text/plain" | |
9 | + | |
10 | + mount V1::Articles | |
11 | + mount V1::Comments | |
12 | + | |
13 | +# helpers APIHelpers | |
14 | + | |
15 | +# require Articles | |
16 | + | |
17 | + end | |
18 | +end | ... | ... |
... | ... | @@ -0,0 +1,34 @@ |
1 | +module API | |
2 | + module V1 | |
3 | + class Articles < Grape::API | |
4 | + | |
5 | + resource :articles do | |
6 | + | |
7 | + get do | |
8 | + first_update = DateTime.parse(params[:first_update]) if params[:first_update] | |
9 | + last_update = DateTime.parse(params[:last_update]) if params[:last_update] | |
10 | + | |
11 | + if first_update.nil? | |
12 | + begin_date = Article.first.created_at | |
13 | + end_date = last_update.nil? ? DateTime.now : last_update | |
14 | + else | |
15 | + begin_date = first_update | |
16 | + end_date = DateTime.now | |
17 | + end | |
18 | + | |
19 | + limit = params[:limit].to_i | |
20 | + limit = 20 if limit == 0 | |
21 | + conditions = {} | |
22 | + conditions[:type] = params[:content_type] if params[:content_type] #FIXME validate type | |
23 | + conditions[:created_at] = begin_date...end_date | |
24 | + Article.find(:all, :conditions => conditions, :offset => (first_update.nil? ? 0 : 1), :limit => limit, :order => "created_at DESC") | |
25 | + end | |
26 | + | |
27 | + get ':id' do | |
28 | + Article.find(params[:id]) | |
29 | + end | |
30 | + end | |
31 | + | |
32 | + end | |
33 | + end | |
34 | +end | ... | ... |
... | ... | @@ -0,0 +1,19 @@ |
1 | +module API | |
2 | + module V1 | |
3 | + class Comments < Grape::API | |
4 | + | |
5 | + resource :articles do | |
6 | + | |
7 | + get ":id/comments" do | |
8 | + Article.find(params[:id]).comments | |
9 | + end | |
10 | + | |
11 | + get ":id/comments/:comment_id" do | |
12 | + Article.find(params[:id]).comments.find(params[:comment_id]) | |
13 | + end | |
14 | + | |
15 | + end | |
16 | + | |
17 | + end | |
18 | + end | |
19 | +end | ... | ... |