Class: Textus::Format::Json
- Inherits:
-
Base
- Object
- Base
- Textus::Format::Json
show all
- Defined in:
- lib/textus/format/json.rb
Class Method Summary
collapse
Methods inherited from Base
enforce_name_match!, format_name, rewrite_name, validate_raw_entry!
Class Method Details
.data_to_payload(data) ⇒ Object
75
76
77
78
|
# File 'lib/textus/format/json.rb', line 75
def self.data_to_payload(data)
data = data.transform_keys(&:to_s) if data.is_a?(Hash)
{ meta: data["_meta"] || {}, body: nil, content: data["content"] || data }
end
|
.extensions ⇒ Object
42
|
# File 'lib/textus/format/json.rb', line 42
def self.extensions = [".json"]
|
.nested_glob ⇒ Object
44
|
# File 'lib/textus/format/json.rb', line 44
def self.nested_glob = "**/*.json"
|
.parse(raw, path: nil) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/textus/format/json.rb', line 6
def self.parse(raw, path: nil)
raw = raw.dup.force_encoding(Encoding::UTF_8)
raise BadFrontmatter.new(path, "entry is not valid UTF-8") unless raw.valid_encoding?
begin
parsed = ::JSON.parse(raw)
rescue ::JSON::ParserError => e
raise BadFrontmatter.new(path, "JSON parse failed: #{e.message}")
end
raise BadFrontmatter.new(path, "JSON top-level must be an object") unless parsed.is_a?(Hash)
meta = parsed["_meta"]
fm = meta.is_a?(Hash) ? meta : {}
content_without_meta = parsed.except("_meta")
{ "_meta" => fm, "body" => raw, "content" => content_without_meta }
end
|
.serialize(meta:, body:, content: nil) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/textus/format/json.rb', line 23
def self.serialize(meta:, body:, content: nil)
if content.is_a?(Hash)
on_disk = meta && !meta.empty? ? { "_meta" => meta }.merge(content) : content
out = ::JSON.pretty_generate(on_disk)
out += "\n" unless out.end_with?("\n")
out
elsif body && !body.to_s.empty?
b = body.to_s
b += "\n" unless b.end_with?("\n")
b
else
raise UsageError.new("json serialize requires :content or :body")
end
end
|
.serialize_for_put(meta:, body:, content:, path:) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/textus/format/json.rb', line 46
def self.serialize_for_put(meta:, body:, content:, path:)
raise UsageError.new("put for json requires content: or body:") if content.nil? && (body.nil? || body.to_s.empty?)
if content.nil?
begin
parsed = parse(body.to_s, path: path)
rescue BadFrontmatter => e
raise BadContent.new(path, "bad_content: #{e.message}")
end
[body.to_s, parsed["_meta"], body.to_s, parsed["content"]]
else
bytes = serialize(meta: meta, body: "", content: content)
[bytes, meta, bytes, content]
end
end
|
.validate_against(schema, parsed) ⇒ Object
38
39
40
|
# File 'lib/textus/format/json.rb', line 38
def self.validate_against(schema, parsed)
schema.validate!(parsed["content"] || {})
end
|
.validate_path_extension(path, nested) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/textus/format/json.rb', line 62
def self.validate_path_extension(path, nested)
ext = File.extname(path)
if nested
return if ext == ""
raise UsageError.new("nested json path must not have an extension")
end
return if ext == ".json"
raise UsageError.new("json format requires '.json' path (got #{ext.inspect})")
end
|