Class: FastJsonTruncator
- Inherits:
-
Object
- Object
- FastJsonTruncator
- Defined in:
- lib/end_point_blank/fast_json_truncator.rb
Constant Summary collapse
- MAX_BYTES =
10000- MAX_DEPTH =
5- MAX_LIST =
20- MAX_STRING =
200- MAX_KEYS =
20
Class Method Summary collapse
- .ensure_limit(json, limit) ⇒ Object
- .prune(value, depth) ⇒ Object
- .truncate(data, limit = MAX_BYTES) ⇒ Object
Class Method Details
.ensure_limit(json, limit) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/end_point_blank/fast_json_truncator.rb', line 43 def self.ensure_limit(json, limit) return json if json.bytesize <= limit truncated = json.byteslice(0, limit - 20) truncated + '...,"truncated":true}' end |
.prune(value, depth) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/end_point_blank/fast_json_truncator.rb', line 16 def self.prune(value, depth) return "[truncated]" if depth > MAX_DEPTH case value when Hash result = {} value.each_with_index do |(k, v), i| break if i >= MAX_KEYS result[k] = prune(v, depth + 1) end result when Array value.first(MAX_LIST).map { |v| prune(v, depth + 1) } when String if value.bytesize > MAX_STRING value.byteslice(0, MAX_STRING) + "..." else value end else value end end |
.truncate(data, limit = MAX_BYTES) ⇒ Object
10 11 12 13 14 |
# File 'lib/end_point_blank/fast_json_truncator.rb', line 10 def self.truncate(data, limit = MAX_BYTES) pruned = prune(data, 0) json = JSON.generate(pruned) ensure_limit(json, limit) end |