Slim option in library and title APIs

This commit is contained in:
Alex Ling 2021-04-25 12:39:08 +00:00
parent 7f0c256fe6
commit cd8944ed2d
4 changed files with 80 additions and 4 deletions

View File

@ -46,6 +46,18 @@ class Entry
file.close file.close
end end
def to_slim_json : String
JSON.build do |json|
json.object do
{% for str in ["zip_path", "title", "size", "id"] %}
json.field {{str}}, @{{str.id}}
{% end %}
json.field "title_id", @book.id
json.field "pages" { json.number @pages }
end
end
end
def to_json(json : JSON::Builder) def to_json(json : JSON::Builder)
json.object do json.object do
{% for str in ["zip_path", "title", "size", "id"] %} {% for str in ["zip_path", "title", "size", "id"] %}

View File

@ -85,6 +85,21 @@ class Library
titles + titles.flat_map &.deep_titles titles + titles.flat_map &.deep_titles
end end
def to_slim_json : String
JSON.build do |json|
json.object do
json.field "dir", @dir
json.field "titles" do
json.array do
self.titles.each do |title|
json.raw title.to_slim_json
end
end
end
end
end
end
def to_json(json : JSON::Builder) def to_json(json : JSON::Builder)
json.object do json.object do
json.field "dir", @dir json.field "dir", @dir

View File

@ -57,6 +57,41 @@ class Title
end end
end end
def to_slim_json : String
JSON.build do |json|
json.object do
{% for str in ["dir", "title", "id"] %}
json.field {{str}}, @{{str.id}}
{% end %}
json.field "signature" { json.number @signature }
json.field "titles" do
json.array do
self.titles.each do |title|
json.raw title.to_slim_json
end
end
end
json.field "entries" do
json.array do
@entries.each do |entry|
json.raw entry.to_slim_json
end
end
end
json.field "parents" do
json.array do
self.parents.each do |title|
json.object do
json.field "title", title.title
json.field "id", title.id
end
end
end
end
end
end
end
def to_json(json : JSON::Builder) def to_json(json : JSON::Builder)
json.object do json.object do
{% for str in ["dir", "title", "id"] %} {% for str in ["dir", "title", "id"] %}

View File

@ -126,8 +126,11 @@ struct APIRouter
end end
end end
Koa.describe "Returns the book with title `tid`" Koa.describe "Returns the book with title `tid`", <<-MD
Supply the `tid` query parameter to strip away "display_name", "cover_url", and "mtime" from the returned object to speed up the loading time
MD
Koa.path "tid", desc: "Title ID" Koa.path "tid", desc: "Title ID"
Koa.query "slim"
Koa.response 200, schema: "title" Koa.response 200, schema: "title"
Koa.response 404, "Title not found" Koa.response 404, "Title not found"
Koa.tag "library" Koa.tag "library"
@ -137,7 +140,11 @@ struct APIRouter
title = Library.default.get_title tid title = Library.default.get_title tid
raise "Title ID `#{tid}` not found" if title.nil? raise "Title ID `#{tid}` not found" if title.nil?
send_json env, title.to_json if env.params.query["slim"]?
send_json env, title.to_slim_json
else
send_json env, title.to_json
end
rescue e rescue e
Logger.error e Logger.error e
env.response.status_code = 404 env.response.status_code = 404
@ -145,14 +152,21 @@ struct APIRouter
end end
end end
Koa.describe "Returns the entire library with all titles and entries" Koa.describe "Returns the entire library with all titles and entries", <<-MD
Supply the `tid` query parameter to strip away "display_name", "cover_url", and "mtime" from the returned object to speed up the loading time
MD
Koa.query "slim"
Koa.response 200, schema: { Koa.response 200, schema: {
"dir" => String, "dir" => String,
"titles" => ["title"], "titles" => ["title"],
} }
Koa.tag "library" Koa.tag "library"
get "/api/library" do |env| get "/api/library" do |env|
send_json env, Library.default.to_json if env.params.query["slim"]?
send_json env, Library.default.to_slim_json
else
send_json env, Library.default.to_json
end
end end
Koa.describe "Triggers a library scan" Koa.describe "Triggers a library scan"