Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. class SlatePlainSerializer
  2. include Interactor
  3.  
  4. def call
  5. @value = context.json ? JSON.parse(context.json) : context.fail!("No string passed")
  6. context.text = serialize(@value["document"])
  7. rescue => error
  8. context.fail!(
  9. error: error
  10. )
  11. end
  12.  
  13.  
  14. def serialize(node)
  15. delimiter = "\n"
  16.  
  17. if
  18. node["object"] === "document" ||
  19. (node["object"] === "block")
  20.  
  21. node["nodes"].map { |item|
  22. serialize(item)
  23. }.join(delimiter)
  24. else
  25. node["text"] ? node["text"] : get_text(node)
  26. end
  27. end
  28.  
  29. def is_block_list(nodes)
  30. nodes.all? { |item| item["object"] == "block" }
  31. end
  32.  
  33.  
  34. def get_text(node)
  35. children = (node["object"] === "text") ? node["leaves"] : node["nodes"]
  36. children.reduce("") { |memo, n| memo + n["text"] }
  37. end
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement