mirror of
https://github.com/hkalexling/Mango.git
synced 2025-08-05 04:15:35 -04:00
Simplify #json_build
This commit is contained in:
parent
4b464ed361
commit
4eaf271fa4
@ -46,7 +46,8 @@ class Entry
|
|||||||
file.close
|
file.close
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_json(json : JSON::Builder, *, slim = false)
|
def build_json(*, slim = false)
|
||||||
|
JSON.build do |json|
|
||||||
json.object do
|
json.object do
|
||||||
{% for str in ["zip_path", "title", "size", "id"] %}
|
{% for str in ["zip_path", "title", "size", "id"] %}
|
||||||
json.field {{str}}, @{{str.id}}
|
json.field {{str}}, @{{str.id}}
|
||||||
@ -60,6 +61,7 @@ class Entry
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def display_name
|
def display_name
|
||||||
@book.display_name @title
|
@book.display_name @title
|
||||||
|
@ -65,16 +65,15 @@ class Library
|
|||||||
titles.flat_map &.deep_entries
|
titles.flat_map &.deep_entries
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_json(json : JSON::Builder, *, slim = false, shallow = false)
|
def build_json(*, slim = false, shallow = false)
|
||||||
|
JSON.build do |json|
|
||||||
json.object do
|
json.object do
|
||||||
json.field "dir", @dir
|
json.field "dir", @dir
|
||||||
json.field "titles" do
|
json.field "titles" do
|
||||||
json.array do
|
json.array do
|
||||||
self.titles.each do |title|
|
self.titles.each do |title|
|
||||||
raw = JSON.build do |j|
|
json.raw title.build_json(slim: slim, shallow: shallow)
|
||||||
title.build_json j, slim: slim, shallow: shallow
|
|
||||||
end
|
end
|
||||||
json.raw raw
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -65,8 +65,9 @@ class Title
|
|||||||
|
|
||||||
alias SortContext = NamedTuple(username: String, opt: SortOptions)
|
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)
|
sort_context : SortContext? = nil)
|
||||||
|
JSON.build do |json|
|
||||||
json.object do
|
json.object do
|
||||||
{% for str in ["dir", "title", "id"] %}
|
{% for str in ["dir", "title", "id"] %}
|
||||||
json.field {{str}}, @{{str.id}}
|
json.field {{str}}, @{{str.id}}
|
||||||
@ -81,10 +82,7 @@ class Title
|
|||||||
json.field "titles" do
|
json.field "titles" do
|
||||||
json.array do
|
json.array do
|
||||||
self.titles.each do |title|
|
self.titles.each do |title|
|
||||||
raw = JSON.build do |j|
|
json.raw title.build_json(slim: slim, shallow: shallow)
|
||||||
title.build_json j, slim: slim, shallow: shallow
|
|
||||||
end
|
|
||||||
json.raw raw
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -97,10 +95,7 @@ class Title
|
|||||||
@entries
|
@entries
|
||||||
end
|
end
|
||||||
_entries.each do |entry|
|
_entries.each do |entry|
|
||||||
raw = JSON.build do |j|
|
json.raw entry.build_json(slim: slim)
|
||||||
entry.build_json j, slim: slim
|
|
||||||
end
|
|
||||||
json.raw raw
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -117,6 +112,7 @@ class Title
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def titles
|
def titles
|
||||||
@title_ids.map { |tid| Library.default.get_title! tid }
|
@title_ids.map { |tid| Library.default.get_title! tid }
|
||||||
|
@ -158,10 +158,9 @@ struct APIRouter
|
|||||||
slim = !env.params.query["slim"]?.nil?
|
slim = !env.params.query["slim"]?.nil?
|
||||||
shallow = !env.params.query["shallow"]?.nil?
|
shallow = !env.params.query["shallow"]?.nil?
|
||||||
|
|
||||||
json = JSON.build do |j|
|
send_json env, title.build_json(slim: slim, shallow: shallow,
|
||||||
title.build_json j, slim: slim, shallow: shallow,
|
sort_context: {username: username,
|
||||||
sort_context: {username: username, opt: sort_opt}
|
opt: sort_opt})
|
||||||
end
|
|
||||||
rescue e
|
rescue e
|
||||||
Logger.error e
|
Logger.error e
|
||||||
env.response.status_code = 404
|
env.response.status_code = 404
|
||||||
@ -184,10 +183,7 @@ struct APIRouter
|
|||||||
slim = !env.params.query["slim"]?.nil?
|
slim = !env.params.query["slim"]?.nil?
|
||||||
shallow = !env.params.query["shallow"]?.nil?
|
shallow = !env.params.query["shallow"]?.nil?
|
||||||
|
|
||||||
json = JSON.build do |j|
|
send_json env, Library.default.build_json(slim: slim, shallow: shallow)
|
||||||
Library.default.build_json j, slim: slim, shallow: shallow
|
|
||||||
end
|
|
||||||
send_json env, json
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Koa.describe "Triggers a library scan"
|
Koa.describe "Triggers a library scan"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user