Class: Urbit::GraphParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/urbit/parser.rb

Direct Known Subclasses

AddGraphParser, AddNodesParser, RemoveGraphParser

Instance Method Summary collapse

Constructor Details

#initialize(for_graph:, with_json:) ⇒ GraphParser

Returns a new instance of GraphParser.



12
13
14
15
# File 'lib/urbit/parser.rb', line 12

def initialize(for_graph:, with_json:)
  super(with_json: with_json)
  @g = for_graph
end

Instance Method Details

#add_nodesObject

Parses the embedded json and adds any found nodes to the graph. Answers an array of nodes.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/urbit/parser.rb', line 21

def add_nodes
  added_nodes = []
  # Make sure we are adding to the correct graph...
  if (@g.resource == self.resource)
    self.nodes_hash.each do |k, v|
      added_nodes << (n = Urbit::Node.new(graph: @g, node_json: v))
      @g.add_node(node: n)
    end
  end
  added_nodes
end

#resourceObject



33
34
35
# File 'lib/urbit/parser.rb', line 33

def resource
  "~#{self.resource_hash["ship"]}/#{self.resource_hash["name"]}"
end

#resource_hashObject



37
38
39
# File 'lib/urbit/parser.rb', line 37

def resource_hash
  @j["resource"]
end