class Urbit::Graph

Attributes

host_ship_name[R]
name[R]
ship[R]

Public Class Methods

new(ship:, graph_name:, host_ship_name:) click to toggle source
# File lib/urbit/graph.rb, line 9
def initialize(ship:, graph_name:, host_ship_name:)
  @ship           = ship
  @name           = graph_name
  @host_ship_name = host_ship_name
  @nodes          = SortedSet.new
end

Public Instance Methods

add_node(node:) click to toggle source
# File lib/urbit/graph.rb, line 16
def add_node(node:)
  @nodes << node
end
host_ship() click to toggle source
# File lib/urbit/graph.rb, line 20
def host_ship
  "~#{@host_ship_name}"
end
newer_sibling_nodes(node:, count:) click to toggle source

Answers the {count} newer sibling nodes relative to the passed {node}.

# File lib/urbit/graph.rb, line 75
def newer_sibling_nodes(node:, count:)
  self.fetch_sibling_nodes(node, :newer, count)[0..(count - 1)]
end
newest_nodes(count: 10) click to toggle source
# File lib/urbit/graph.rb, line 56
def newest_nodes(count: 10)
  count = 1 if count < 1
  return self.fetch_newest_nodes(count) if @nodes.empty? || @nodes.count < count
  self.nodes.reverse[0..(count - 1)]
end
node(index:) click to toggle source

Finds a single node in this graph by its index. The index here should be the atom representation (as returned by Node#index).

# File lib/urbit/graph.rb, line 36
def node(index:)
  self.fetch_node(index).first
end
nodes() click to toggle source

Answers an array with all of this Graph's currently attached Nodes, recursively inluding all of the Node's children.

# File lib/urbit/graph.rb, line 44
def nodes
  self.fetch_all_nodes if @nodes.empty?
  @all_n = []
  @nodes.each do |n|
    @all_n << n
    n.children.each do |c|
      @all_n << c
    end
  end
  @all_n
end
older_sibling_nodes(node:, count:) click to toggle source

Answers the {count} older sibling nodes relative to the passed {node}.

# File lib/urbit/graph.rb, line 82
def older_sibling_nodes(node:, count:)
  self.fetch_sibling_nodes(node, :older, count)[0..(count - 1)]
end
oldest_nodes(count: 10) click to toggle source
# File lib/urbit/graph.rb, line 62
def oldest_nodes(count: 10)
  count = 1 if count < 1
  return self.fetch_oldest_nodes(count) if @nodes.empty? || @nodes.count < count
  self.nodes[0..(count - 1)]
end
resource() click to toggle source
# File lib/urbit/graph.rb, line 68
def resource
  "#{self.host_ship}/#{self.name}"
end
to_s() click to toggle source

the canonical printed representation of a Graph

# File lib/urbit/graph.rb, line 88
def to_s
  "a Graph(#{self.resource})"
end

Private Instance Methods

fetch_all_nodes() click to toggle source
# File lib/urbit/graph.rb, line 94
def fetch_all_nodes
  self.fetch_nodes("#{self.graph_resource}/",
                   AddGraphParser,
                   "add-graph")
end
fetch_newest_nodes(count) click to toggle source
# File lib/urbit/graph.rb, line 100
def fetch_newest_nodes(count)
  self.fetch_nodes("#{self.graph_resource}/node/siblings/newest/kith/#{count}/",
                   AddNodesParser,
                   "add-nodes")
end
fetch_node(index_atom) click to toggle source
# File lib/urbit/graph.rb, line 106
def fetch_node(index_atom)
  self.fetch_nodes("#{self.graph_resource}/node/index/kith/#{index_atom}/",
                   AddNodesParser,
                   "add-nodes")
end
fetch_nodes(endpoint, parser, node) click to toggle source

Answers an array of Nodes that were fetched or an empty array if nothing found.

# File lib/urbit/graph.rb, line 127
def fetch_nodes(endpoint, parser, node)
  r = self.ship.scry(app: 'graph-store', path: endpoint)
  if (200 == r[:status])
    body = JSON.parse(r[:body])
    if (p = parser.new(for_graph: self, with_json: body["graph-update"][node]))
      return p.add_nodes
    end
  end
  []
end
fetch_oldest_nodes(count) click to toggle source
# File lib/urbit/graph.rb, line 112
def fetch_oldest_nodes(count)
  self.fetch_nodes("#{self.graph_resource}/node/siblings/oldest/kith/#{count}/",
                   AddNodesParser,
                   "add-nodes")
end
fetch_sibling_nodes(node, direction, count) click to toggle source
# File lib/urbit/graph.rb, line 118
def fetch_sibling_nodes(node, direction, count)
  self.fetch_nodes("#{self.graph_resource}/node/siblings/#{direction}/kith/#{count}/#{node.index}/",
                   AddNodesParser,
                   "add-nodes")
end
graph_resource() click to toggle source
# File lib/urbit/graph.rb, line 138
def graph_resource
  "/graph/#{self.resource}"
end