Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SlatePlainSerializer
- include Interactor
- def call
- @value = context.json ? JSON.parse(context.json) : context.fail!("No string passed")
- context.text = serialize(@value["document"])
- rescue => error
- context.fail!(
- error: error
- )
- end
- def serialize(node)
- delimiter = "\n"
- if
- node["object"] === "document" ||
- (node["object"] === "block")
- node["nodes"].map { |item|
- serialize(item)
- }.join(delimiter)
- else
- node["text"] ? node["text"] : get_text(node)
- end
- end
- def is_block_list(nodes)
- nodes.all? { |item| item["object"] == "block" }
- end
- def get_text(node)
- children = (node["object"] === "text") ? node["leaves"] : node["nodes"]
- children.reduce("") { |memo, n| memo + n["text"] }
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement