Permalink
Browse files

Provide a page with examples on how to access api

  • Loading branch information...
1 parent 193faaf commit 2f2031544a70092595abbcaa04863c627f4e3494 @chischaschos committed Feb 26, 2014
View
@@ -10,7 +10,6 @@
## Pending tasks
- Unify logs format
-- Provide curl examples to interact with API
- Verify passed access token authenticity per client
- Review response status codes
- Create a real password creation strategy
@@ -12,6 +12,7 @@ class App.TodoRouter extends Backbone.Router
'' : 'authenticate'
'todos' : 'todos'
'sign-out' : 'signOut'
+ 'api-doc' : 'apiDocumentation'
authenticate: ->
@switchView(new App.AuthenticationView)
@@ -28,3 +29,6 @@ class App.TodoRouter extends Backbone.Router
todoRouter.navigate('', trigger: true, replace: true)
success: (model, response, options) ->
todoRouter.navigate('', trigger: true, replace: true)
+
+ apiDocumentation: ->
+ @switchView(new App.ApiDocumentationView)
@@ -0,0 +1,50 @@
+<div id='menu' class='grid-item one_whole'>
+ <ul>
+ <li>
+ <a href='#todos' id='todos'>back</a>
+ </li>
+ <li>
+ <a href='#sign-out' id='sign-out'>sign out</a>
+ </li>
+ </ul>
+</div>
+
+<div class='grid-item one_whole'>
+ <ul>
+ <li>
+ <p>Create a user</p>
+ <code>
+ curl localhost:9393/api/users -H'Content-Type: application/json' -d'{"user":{"email":"user@someqhere.com","password":"123test123"}}'
+ </code>
+ </li>
+
+ <li>
+ <p>Create a session</p>
+ <code>
+ curl localhost:9393/api/session -H'Content-Type: application/json' -d'{"user":{"email":"paco1@paco.com","password":"123test123"}}'
+ </code>
+ </li>
+
+ <li>
+ <p>Create a session</p>
+ <code>
+ curl localhost:9393/api/session -H'Content-Type: application/json' -d'{"user":{"email":"paco1@paco.com","password":"123test123"}}'
+ </code>
+ </li>
+
+ <li>
+ <p>Get your items</p>
+ <code>
+ curl localhost:9393/api/list_item -HCookie:access_token=<span name='access_token'></span>
+ </code>
+ </li>
+
+ <li>
+ <p>Create an item</p>
+ <code>
+ curl localhost:9393/api/list_item -H'Cookie:access_token=<span name='access_token'></span>' -H'Content-Type: application/json' -d'{"list_item":{"description":"buy some","priority":"4","completed":true,"due_date":"200-12-12"}}'
+ </code>
+ </li>
+ </ul>
+
+</div>
@@ -4,7 +4,10 @@
<a href='#add-todo' id='add-todo'>add</a>
</li>
<li>
- <a href='#sign-out' id='sign out'>sign out</a>
+ <a href='#sign-out' id='sign-out'>sign out</a>
+ </li>
+ <li>
+ <a href='#api-doc' id=''>api docs</a>
</li>
</ul>
</div>
@@ -0,0 +1,11 @@
+@App ||= {}
+
+class App.ApiDocumentationView extends App.BaseView
+ template: JST['todo/templates/api_documentation']
+
+ render: ->
+ @$el.html(@template(access_token: 123))
+ (new App.SessionModel).fetch
+ success: (model, response, options) =>
+ @$el.find("span[name='access_token']").html model.get('access_token')
+ @
View
@@ -40,21 +40,8 @@ class Api < Application
end
end
- post '/api/session', auth_required: false do
- session = Services::SessionCreator.new(params[:user])
-
- if session.valid?
- cookie_params = {
- value: session.access_token,
- httponly: true
- }
- response.set_cookie 'access_token', cookie_params
- {}.to_json
-
- else
- status 404
- session.h_errors.to_json
- end
+ get '/api/session', auth_required: true do
+ @session.to_json
end
post '/api/session', auth_required: false do
@@ -63,10 +50,10 @@ class Api < Application
if session.valid?
cookie_params = {
value: session.access_token,
- httponly: true,
- secure: true
+ httponly: true
}
response.set_cookie 'access_token', cookie_params
+ {}.to_json
else
status 404
@@ -15,6 +15,10 @@ class Session
session.access_token = SecureRandom.uuid
end
+ def to_json
+ { access_token: self.access_token }.to_json
+ end
+
def h_errors
{ errors: self.errors.to_hash }
end
View
@@ -54,4 +54,23 @@
end
end
end
+
+ context 'when retrieving the existing session' do
+ let!(:session) { Todo::Services::SessionCreator.new(params) }
+
+ it 'should success' do
+ expect(session.valid?).to be_true
+
+ set_cookie "access_token=#{session.access_token}"
+
+ get "/api/session"
+
+ expect(last_response).to be_json
+ expect(last_response).not_to have_cookie 'access_token'
+ expect(last_response.body).to have_json_path 'access_token'
+ expect(last_response.status).to eq 200
+
+ end
+
+ end
end

0 comments on commit 2f20315

Please sign in to comment.