Class: Aspen::Renderers::JsonRenderer

Inherits:
AbstractRenderer show all
Defined in:
lib/aspen/renderers/json_renderer.rb

Instance Attribute Summary

Attributes inherited from AbstractRenderer

#environment, #statements

Instance Method Summary collapse

Methods inherited from AbstractRenderer

#initialize

Constructor Details

This class inherits a constructor from AbstractRenderer

Instance Method Details

#nodesObject



12
13
14
15
16
17
18
19
# File 'lib/aspen/renderers/json_renderer.rb', line 12

def nodes
  statements.flat_map(&:nodes).map do |node|
    node.attributes.merge({
      id: node.nickname,
      label: node.label
    })
  end
end

#relationshipsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aspen/renderers/json_renderer.rb', line 21

def relationships
  statements.map.with_index do |st, id|
    # @todo: Add in Custom Statements
    if st.is_a? Aspen::CustomStatement
      next # NO OP
    else
      {
        id: "e#{id}",
        source: st.origin.nickname,
        target: st.target.nickname,
        label: st.edge.label,
        reciprocal: st.edge.reciprocal?
      }
    end
  end.compact
end

#renderObject



5
6
7
8
9
10
# File 'lib/aspen/renderers/json_renderer.rb', line 5

def render
  {
    nodes: nodes,
    edges: relationships
  }.to_json
end