diff --git a/src/library/entry.cr b/src/library/entry.cr index d592e43..0a08246 100644 --- a/src/library/entry.cr +++ b/src/library/entry.cr @@ -46,17 +46,19 @@ class Entry file.close end - def build_json(json : JSON::Builder, *, slim = false) - json.object do - {% for str in ["zip_path", "title", "size", "id"] %} + def build_json(*, slim = false) + 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 } - unless slim - json.field "display_name", @book.display_name @title - json.field "cover_url", cover_url - json.field "mtime" { json.number @mtime.to_unix } + json.field "title_id", @book.id + json.field "pages" { json.number @pages } + unless slim + json.field "display_name", @book.display_name @title + json.field "cover_url", cover_url + json.field "mtime" { json.number @mtime.to_unix } + end end end end diff --git a/src/library/library.cr b/src/library/library.cr index 06b1e0f..508a274 100644 --- a/src/library/library.cr +++ b/src/library/library.cr @@ -65,16 +65,15 @@ class Library titles.flat_map &.deep_entries end - def build_json(json : JSON::Builder, *, slim = false, shallow = false) - json.object do - json.field "dir", @dir - json.field "titles" do - json.array do - self.titles.each do |title| - raw = JSON.build do |j| - title.build_json j, slim: slim, shallow: shallow + def build_json(*, slim = false, shallow = false) + 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.build_json(slim: slim, shallow: shallow) end - json.raw raw end end end diff --git a/src/library/title.cr b/src/library/title.cr index 0b91603..957a608 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -65,51 +65,47 @@ class Title alias SortContext = NamedTuple(username: String, opt: SortOptions) - def build_json(json : JSON::Builder, *, slim = false, shallow = false, + def build_json(*, slim = false, shallow = false, sort_context : SortContext? = nil) - json.object do - {% for str in ["dir", "title", "id"] %} + 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 } - unless slim - json.field "display_name", display_name - json.field "cover_url", cover_url - json.field "mtime" { json.number @mtime.to_unix } - end - unless shallow - json.field "titles" do - json.array do - self.titles.each do |title| - raw = JSON.build do |j| - title.build_json j, slim: slim, shallow: shallow + json.field "signature" { json.number @signature } + unless slim + json.field "display_name", display_name + json.field "cover_url", cover_url + json.field "mtime" { json.number @mtime.to_unix } + end + unless shallow + json.field "titles" do + json.array do + self.titles.each do |title| + json.raw title.build_json(slim: slim, shallow: shallow) end - json.raw raw end end - end - json.field "entries" do - json.array do - _entries = if sort_context - sorted_entries sort_context[:username], - sort_context[:opt] - else - @entries - end - _entries.each do |entry| - raw = JSON.build do |j| - entry.build_json j, slim: slim + json.field "entries" do + json.array do + _entries = if sort_context + sorted_entries sort_context[:username], + sort_context[:opt] + else + @entries + end + _entries.each do |entry| + json.raw entry.build_json(slim: slim) end - json.raw raw 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 + 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 diff --git a/src/routes/api.cr b/src/routes/api.cr index 3dc878e..8c66e13 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -158,10 +158,9 @@ struct APIRouter slim = !env.params.query["slim"]?.nil? shallow = !env.params.query["shallow"]?.nil? - json = JSON.build do |j| - title.build_json j, slim: slim, shallow: shallow, - sort_context: {username: username, opt: sort_opt} - end + send_json env, title.build_json(slim: slim, shallow: shallow, + sort_context: {username: username, + opt: sort_opt}) rescue e Logger.error e env.response.status_code = 404 @@ -184,10 +183,7 @@ struct APIRouter slim = !env.params.query["slim"]?.nil? shallow = !env.params.query["shallow"]?.nil? - json = JSON.build do |j| - Library.default.build_json j, slim: slim, shallow: shallow - end - send_json env, json + send_json env, Library.default.build_json(slim: slim, shallow: shallow) end Koa.describe "Triggers a library scan"