api docs started, minor refactoring

This commit is contained in:
Paul Liverman III 2018-04-24 22:02:41 -07:00
parent 4bace26750
commit 40c7fce965
6 changed files with 162 additions and 47 deletions

View File

@ -24,8 +24,6 @@ class Simplex extends Application
super err --, trace
[index: "/"]: =>
@title = "Simplex Task Manager"
if @user
@keys = APIKeys\select "WHERE user_id = ? ORDER BY id ASC", @user.id
@tasks = Tasks\select "WHERE user_id = ? ORDER BY id ASC", @user.id
@ -36,10 +34,7 @@ class Simplex extends Application
return render: "index.logged_in"
else
@html ->
a href: @url_for("user_login"), "log in"
text " | "
a href: @url_for("user_new"), "new user"
return render: "index.logged_out"
[console: "/console"]: =>
if @user and @user.admin

View File

@ -79,32 +79,6 @@ class API extends Application
else
abort 500, "Error deleting task."
[random: "/random"]: api_request =>
assert_valid @params, {
{"count", exists: true, is_integer: true, optional: true, "Count is not an integer."}
{"done", exists: true, one_of: {true, false}, optional: true, "Done is not a boolean."}
}
@params.count or= 1
@params.done = false if @params.done == nil
abort 501, "Not implemented."
-- possibly need to store how many items each user has and use a different strategy for users with low amounts vs high amounts
-- key = get_key(@)
--
-- local tasks
-- if @params.done
-- offset = random Tasks\count "user_id = ? AND done = ? ORDER BY id ASC", key.user_id, @params.done
-- tasks = Tasks\select "WHERE user_id = ? AND done = ? ORDER BY id ASC OFFSET ? LIMIT 1", key.user_id, @params.done, offset
-- else
-- offset = random Tasks\count "user_id = ? ORDER BY id ASC", key.user_id
-- tasks = Tasks\select "WHERE user_id = ? ORDER BY id ASC OFFSET ? LIMIT 1", key.user_id, offset
--
-- if tasks and #tasks == 1
-- return json: { success: true, task: tasks[1] }
-- else
-- abort!
[list: "/list"]: api_request =>
assert_valid @params, {
{"count", exists: true, is_integer: true, optional: true, "Count is not an integer."}
@ -131,6 +105,32 @@ class API extends Application
-- returns page in case it returned a different page than you asked for! (in the case you ask for a page beyond the end)
return json: { success: true, page: @params.page, :tasks }
[random: "/random"]: api_request =>
assert_valid @params, {
{"count", exists: true, is_integer: true, optional: true, "Count is not an integer."}
{"done", exists: true, one_of: {true, false}, optional: true, "Done is not a boolean."}
}
@params.count or= 1
@params.done = false if @params.done == nil
abort 501, "Not implemented."
-- possibly need to store how many items each user has and use a different strategy for users with low amounts vs high amounts
-- key = get_key(@)
--
-- local tasks
-- if @params.done
-- offset = random Tasks\count "user_id = ? AND done = ? ORDER BY id ASC", key.user_id, @params.done
-- tasks = Tasks\select "WHERE user_id = ? AND done = ? ORDER BY id ASC OFFSET ? LIMIT 1", key.user_id, @params.done, offset
-- else
-- offset = random Tasks\count "user_id = ? ORDER BY id ASC", key.user_id
-- tasks = Tasks\select "WHERE user_id = ? ORDER BY id ASC OFFSET ? LIMIT 1", key.user_id, offset
--
-- if tasks and #tasks == 1
-- return json: { success: true, task: tasks[1] }
-- else
-- abort!
[new_key: "/key/new"]: api_request =>
api_key, err = APIKeys\create(@user)
abort 500, err unless api_key

View File

@ -10,17 +10,5 @@ class Docs extends Application
a href: @url_for("docs_v1"), "click here"
[v1: "/v1"]: =>
@html ->
p "ToDo. Write this."
-- /new { content: "string" }
-- /get { id: # } or content
-- /do { id: # } or content
-- /undo { id: # } or content
-- /delete { id: # } or content
-- /random { count: #, done: bool } (both args optional, defaults count 1, done false)
-- /list { count: #, done: bool, page: #, order: asc/desc } (if done not specified, returns all,
-- default count is 50, default page is 1, default order is latest first
-- /key/new
-- /key/delete { id: #, key: "str" }
@title = "Simplex API v1"
return render: "docs.v1"

View File

@ -9,7 +9,7 @@ class extends Widget
head ->
meta charset: "utf-8"
meta name: "viewport", content: "width=device-width, initial-scale=1"
title(@title or "Simplex")
title(@title or "Simplex Task Manager")
-- Milligram CSS framework
link rel: "stylesheet", href: "https://fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic"
link rel: "stylesheet", href: "https://cdn.rawgit.com/necolas/normalize.css/master/normalize.css"

120
views/docs/v1.moon Normal file
View File

@ -0,0 +1,120 @@
import Widget from require "lapis.html"
class Docs_1 extends Widget
content: =>
task_list = ->
ul ->
li ->
a href: "#new", "/new"
text " { content: string }"
li ->
a href: "#get", "/get"
text " { id: integer, content: string }"
li ->
a href: "#do", "/do"
text " { id: integer, content: string }"
li ->
a href: "#undo", "/undo"
text " { id: integer, content: string }"
li ->
a href: "#delete", "/delete"
text " { id: integer, content: string }"
li ->
a href: "#list", "/list"
text " { count: integer, done: boolean, page: integer, order: 'asc'/'desc' }"
li ->
a href: "#random", "/random"
text " { count: integer, done: boolean }"
style -> raw "h3 { font-family: monospace; }"
ol style: "font-family: monospace;", ->
li ->
a href: "#auth", "Authorization"
li ->
a href: "#tasks", "Tasks"
task_list!
li ->
a href: "#keys", "API Keys"
ul ->
li ->
a href: "#new-key", "/key/new"
li ->
a href: "#delete-key", "/key/delete"
text " { id: integer, key: string }"
a name: "auth"
h2 "Authorization"
p "Authorization is done using a session cookie on the web interface, or an API key sent along with API requests."
p ->
text "API keys can be sent in an Authorization header or as an "
code "api_key"
text " field in JSON."
blockquote ->
code "Authorization: JDJiJDEyJFRPaG0wOW16VXhoUTd3dElB"
blockquote ->
code '{ "api_key": "JDJiJDEyJFRPaG0wOW16VXhoUTd3dElB" }'
p ->
text "Grab an API key from "
a href: @url_for("index"), "the web interface"
text " to get started. You can also delete existing API keys there."
a name: "tasks"
h2 "Tasks"
p ->
text "Version 1 of this API is extremely simple. POST "
code '{ "content": "this is a todo item" }'
text " with valid authorization to "
code "/new"
text " to add tasks. The next four endpoints ("
code "/get"
text ","
code "/do"
text ","
code "/undo"
text ","
code "/delete"
text ") all use the same input to complete their operations (an "
code "id"
text " integer or "
code "content"
text " boolean)."
p ->
code "/list"
text " and "
code "/random"
text " are a little more complex, and explained further below."
task_list!
a name: "new"
h3 "/new"
a name: "get"
a name: "do"
a name: "undo"
a name: "delete"
a name: "list"
a name: "random"
a name: "keys"
h2 "API Keys"
a name: "new-key"
a name: "delete-key"
-- /list { count: #, done: bool, page: #, order: asc/desc } (if done not specified, returns all,
-- default count is 50, default page is 1, default order is latest first
-- /random { count: #, done: bool } (both args optional, defaults count 1, done false)

View File

@ -0,0 +1,12 @@
import Widget from require "lapis.html"
class LoggedOut extends Widget
content: =>
p "This introduction will be written soon(TM)."
p ->
text "For now, forward any questions or ideas to "
a href: "https://twitter.com/Guard13007", "@Guard13007"
text " on Twitter."
p ->
text "Or email me: "
a href: "mailto:paul.liverman.iii@gmail.com", "paul.liverman.iii@gmail.com"