fix #1078 and document it on the REST_API page

This commit is contained in:
Reinier Balt 2011-07-09 18:47:30 +02:00
parent ed2154b94b
commit 174becee81
3 changed files with 67 additions and 23 deletions

View file

@ -26,7 +26,7 @@ class TodosController < ApplicationController
respond_to do |format|
format.html &render_todos_html
format.m &render_todos_mobile
format.xml { render :xml => @todos.to_xml( :except => :user_id ) }
format.xml { render :xml => @todos.to_xml( *to_xml_params ) }
format.rss &render_rss_feed
format.atom &render_atom_feed
format.text &render_text_feed
@ -230,7 +230,7 @@ class TodosController < ApplicationController
@return_path=cookies[:mobile_url] ? cookies[:mobile_url] : mobile_path
render :action => 'show'
end
format.xml { render :xml => @todo.to_xml( :root => 'todo', :except => :user_id, :include => [:tags] ) }
format.xml { render :xml => @todo.to_xml( *to_xml_params ) }
end
end
@ -305,7 +305,7 @@ class TodosController < ApplicationController
end
render
end
format.xml { render :xml => @todo.to_xml( :except => :user_id ) }
format.xml { render :xml => @todo.to_xml( *to_xml_params ) }
format.html do
if @saved
# TODO: I think this will work, but can't figure out how to test it
@ -325,7 +325,7 @@ class TodosController < ApplicationController
@saved = true # cannot determine error
respond_to do |format|
format.js
format.xml { render :xml => @todo.to_xml( :except => :user_id ) }
format.xml { render :xml => @todo.to_xml( *to_xml_params ) }
format.html { redirect_to request.referrer}
end
end
@ -345,7 +345,7 @@ class TodosController < ApplicationController
respond_to do |format|
format.js { render :action => :update }
format.xml { render :xml => @todo.to_xml( :except => :user_id ) }
format.xml { render :xml => @todo.to_xml( *to_xml_params ) }
end
end
@ -385,7 +385,7 @@ class TodosController < ApplicationController
@status_message = t('todos.added_new_project') + ' / ' + @status_message if @new_project_created
@status_message = t('todos.added_new_context') + ' / ' + @status_message if @new_context_created
}
format.xml { render :xml => @todo.to_xml( :except => :user_id ) }
format.xml { render :xml => @todo.to_xml( *to_xml_params ) }
format.m do
if @saved
if cookies[:mobile_url]
@ -473,6 +473,11 @@ class TodosController < ApplicationController
@done_this_week = get_done_this_week(completed_todos)
@done_this_month = get_done_this_month(completed_todos)
@count = @done_today.size + @done_this_week.size + @done_this_month.size
respond_to do |format|
format.html
format.xml { render :xml => completed_todos.to_xml( *to_xml_params ) }
end
end
def all_done
@ -497,7 +502,7 @@ class TodosController < ApplicationController
respond_to do |format|
format.html
format.m { render :action => 'mobile_list_deferred' }
format.xml { render :xml => @not_done_todos.to_xml( :except => :user_id ) }
format.xml { render :xml => @not_done_todos.to_xml( *to_xml_params ) }
end
end
@ -687,6 +692,19 @@ class TodosController < ApplicationController
@due_all = current_user.todos.not_completed.are_due.find(:all, :order => "due")
render :action => 'calendar', :layout => false, :content_type => Mime::ICS
}
format.xml {
@due_all = current_user.todos.not_completed.are_due.find(:all, :order => "due")
render :xml => @due_all.to_xml( *to_xml_params )
}
end
end
def list_hidden
@hidden = current_user.todos.hidden
respond_to do |format|
format.xml {
render :xml => @hidden.to_xml( *to_xml_params )
}
end
end
@ -754,6 +772,14 @@ class TodosController < ApplicationController
private
def to_xml_params
if params[:limit_fields] == 'index'
return [:only => [:id, :created_at, :updated_at, :completed_at] ]
else
return [:except => :user_id, :include => [:tags] ]
end
end
def get_todo_from_params
# TODO: this was a :append_before but was removed to tune performance per
# method. Reconsider re-enabling it

View file

@ -56,6 +56,10 @@
<ul>
<li>/todos.xml</li>
<li>/todos/<code>ID</code>.xml</li>
<li>/tickler.xml</li>
<li>/done.xml</li>
<li>/hidden.xml</li>
<li>/calendar.xml</li>
<li>/contexts.xml</li>
<li>/contexts/<code>ID</code>.xml</li>
<li>/contexts/<code>ID</code>/todos.xml</li>
@ -64,6 +68,17 @@
<li>/projects/<code>ID</code>/todos.xml</li>
</ul>
<p>For the todo resources (todos, tickler, done, hidden and calendar) you can limit the returned
field to <code>ID, created_at, modified_at, completed_at</code> by adding the parameter
<code>limit_fields</code> and setting it to <code>index</code>. For example:</p>
<pre>
<code>
$ curl -u username:p4ssw0rd -H "Content-Type: text/xml" \
<%= home_url %>tickler.xml?limit_fields=index
</code>
</pre>
<h2>Writing to the API</h2>
<p>The API provides mechanisms for adding, updating and deleting resources using the HTTP methods <code>PUT</code>, <code>POST</code> and <code>DELETE</code> in combination with the content.</p>
@ -173,17 +188,17 @@ $ curl -u username:p4ssw0rd -H "Content-Type: text/xml" \
<pre>
<code>
$ script/console
$ script/console
Loading development environment (Rails 1.2.4)
&gt;&gt; class Context &lt; ActiveResource::Base; end
=&gt; nil
&gt;&gt; Context.site = "<%= home_url %>"
=&gt; "<%= home_url %>"
&gt;&gt; Context.site.user = "username"
=&gt; "username"
=&gt; "<%= home_url %>"
&gt;&gt; Context.site.user = "username"
=&gt; "username"
&gt;&gt; Context.site.password = CGI.escape "p4ssw0rd"
=&gt; "p4ssw0rd"
&gt;&gt; Context.site.password = CGI.escape "p4ssw0rd"
=&gt; "p4ssw0rd"
&gt;&gt; Context.find :first
=&gt; #&lt;Context:0x262396c @prefix_options={}, @attributes={...}&gt;
&gt;&gt; &gt;&gt; Context.find :all
@ -201,7 +216,7 @@ $ SITE="http://username:p4ssw0rd@<%= request.host_with_port %>" irb \
irb(main):001:0&gt; inbox = Tracks::Context.find :first
irb(main):002:0&gt; inbox.name
=&gt; "@inbox"
=&gt; "@inbox"
irb(main):003:0&gt;
</code>
</pre>

View file

@ -3,7 +3,7 @@ ActionController::Routing::Routes.draw do |map|
:member => {:change_password => :get, :update_password => :post,
:change_auth_type => :get, :update_auth_type => :post, :complete => :get,
:refresh_token => :post }
map.with_options :controller => :users do |users|
users.signup 'signup', :action => "new"
end
@ -12,11 +12,11 @@ ActionController::Routing::Routes.draw do |map|
contexts.resources :todos, :name_prefix => "context_"
end
map.resources :projects, :collection => {:order => :post, :alphabetize => :post, :actionize => :post, :done => :get},
map.resources :projects, :collection => {:order => :post, :alphabetize => :post, :actionize => :post, :done => :get},
:member => {:done_todos => :get, :all_done_todos => :get} do |projects|
projects.resources :todos, :name_prefix => "project_"
end
map.resources :notes
map.resources :todos,
@ -28,7 +28,7 @@ ActionController::Routing::Routes.draw do |map|
todos.home '', :action => "index"
todos.tickler 'tickler.:format', :action => "list_deferred"
todos.mobile_tickler 'tickler.m', :action => "list_deferred", :format => 'm'
# This route works for tags with dots like /todos/tag/version1.5
# please note that this pattern consumes everything after /todos/tag
# so /todos/tag/version1.5.xml will result in :name => 'version1.5.xml'
@ -41,10 +41,13 @@ ActionController::Routing::Routes.draw do |map|
todos.tags 'tags.autocomplete', :action => "tags", :format => 'autocomplete'
todos.auto_complete_for_predecessor 'auto_complete_for_predecessor', :action => 'auto_complete_for_predecessor'
todos.calendar 'calendar.ics', :action => "calendar", :format => 'ics'
todos.calendar 'calendar.xml', :action => "calendar", :format => 'xml'
todos.calendar 'calendar', :action => "calendar"
todos.hidden 'hidden.xml', :action => "list_hidden", :format => 'xml'
todos.mobile 'mobile', :action => "index", :format => 'm'
todos.mobile_abbrev 'm', :action => "index", :format => 'm'
todos.mobile_abbrev_new 'm/new', :action => "new", :format => 'm'
@ -74,7 +77,7 @@ ActionController::Routing::Routes.draw do |map|
fl.mobile_feeds 'feeds.m', :action => 'index', :format => 'm'
fl.feeds 'feeds', :action => 'index'
end
map.with_options :controller => :integrations do |i|
i.integrations 'integrations', :action => 'index'
i.rest_api_docs 'integrations/rest_api', :action => "rest_api"
@ -83,12 +86,12 @@ ActionController::Routing::Routes.draw do |map|
end
map.preferences 'preferences', :controller => 'preferences', :action => 'index'
map.with_options :controller => :stats do |stats|
stats.stats 'stats', :action => 'index'
stats.done_overview 'done', :action => 'done'
end
map.search 'search', :controller => 'search', :action => 'index'
map.data 'data', :controller => 'data', :action => 'index'