Class: Urbit::Node

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph:, node_json:) ⇒ Node

Returns a new instance of Node.



6
7
8
9
10
11
12
# File 'lib/urbit/node.rb', line 6

def initialize(graph:, node_json:)
  @graph      = graph
  @post_h     = node_json['post']
  @children_h = node_json['children']
  @persistent = false
  @index      = nil
end

Instance Attribute Details

#node_jsonObject

Returns the value of attribute node_json.



5
6
7
# File 'lib/urbit/node.rb', line 5

def node_json
  @node_json
end

Class Method Details

.da_to_unix(da) ⇒ Object

Given a bigint representing an urbit date, returns a unix timestamp.



17
18
19
20
21
22
23
24
# File 'lib/urbit/node.rb', line 17

def self.da_to_unix(da)
  # ported from urbit lib.ts which in turn was ported from +time:enjs:format in hoon.hoon
  da_second = 18446744073709551616
  da_unix_epoch = 170141184475152167957503069145530368000
  offset = da_second / 2000
  epoch_adjusted = offset + (da - da_unix_epoch)
  return (epoch_adjusted * 1000) / da_second
end

.unix_to_da(unix) ⇒ Object

Given a unix timestamp, returns a bigint representing an urbit date



29
30
31
32
33
34
# File 'lib/urbit/node.rb', line 29

def self.unix_to_da(unix)
  da_second = 18446744073709551616
  da_unix_epoch = 170141184475152167957503069145530368000
  time_since_epoch =  (unix * da_second) / 1000
  return da_unix_epoch + time_since_epoch;
end

Instance Method Details

#<=>(another_node) ⇒ Object



40
41
42
# File 'lib/urbit/node.rb', line 40

def <=>(another_node)
  self.time_sent <=> another_node.time_sent
end

#==(another_node) ⇒ Object



36
37
38
# File 'lib/urbit/node.rb', line 36

def ==(another_node)
  another_node.index == self.index
end

#authorObject



57
58
59
# File 'lib/urbit/node.rb', line 57

def author
  @post_h["author"]
end

#childrenObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/urbit/node.rb', line 61

def children
  @children = SortedSet.new
  if @children_h
    @children_h.each do |k, v|
      @children << (n = Urbit::Node.new(graph: @graph, node_json: v))
      # Recursively fetch all the children's children until we reach the bottom...
      n.children.each {|c| @children << c} if !n.children.empty?
    end
  end
  @children
end

#contentsObject



73
74
75
# File 'lib/urbit/node.rb', line 73

def contents
  @post_h['contents']
end

#datetime_sentObject



77
78
79
# File 'lib/urbit/node.rb', line 77

def datetime_sent
  Time.at(self.time_sent / 1000).to_datetime
end

#deleted?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/urbit/node.rb', line 48

def deleted?
  # This is a "deleted" node. Not sure what to do yet, but for now don't create a Node.
  @post_h["index"].nil?
end

#eql?(another_node) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/urbit/node.rb', line 44

def eql?(another_node)
  another_node.index == self.index
end

#hashObject



53
54
55
# File 'lib/urbit/node.rb', line 53

def hash
  self.index.hash
end

#indexObject

Answers the memoized @index or calculates it from the raw_index.



88
89
90
91
# File 'lib/urbit/node.rb', line 88

def index
  return @index if @index
  @index = self.index_to_atom
end

#next(count: 1) ⇒ Object

Answers the next count Nodes relative to this Node. Defaults to the next Node if no count is passed.



97
98
99
# File 'lib/urbit/node.rb', line 97

def next(count: 1)
  @graph.newer_sibling_nodes(node: self, count: count)
end

#parent?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/urbit/node.rb', line 101

def parent?
  !self.children.empty?
end

#persistent?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/urbit/node.rb', line 81

def persistent?
  @persistent
end

#previous(count: 1) ⇒ Object

Answers the previous count Nodes relative to this Node. Defaults to the next Node if no count is passed.



109
110
111
# File 'lib/urbit/node.rb', line 109

def previous(count: 1)
  @graph.older_sibling_nodes(node: self, count: count)
end

#raw_indexObject



113
114
115
116
# File 'lib/urbit/node.rb', line 113

def raw_index
  return @post_h["index"].delete_prefix('/') unless self.deleted?
  (Node.unix_to_da(Time.now.to_i)).to_s
end

#time_sentObject

This is the time sent as recorded by urbit in unix extended format.



121
122
123
# File 'lib/urbit/node.rb', line 121

def time_sent
  @post_h['time-sent']
end

#to_hObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/urbit/node.rb', line 125

def to_h
  {
    index: self.index,
    author: self.author,
    sent: self.datetime_sent,
    contents: self.contents,
    is_parent: self.parent?,
    child_count: self.children.count
  }
end

#to_pretty_arrayObject



136
137
138
# File 'lib/urbit/node.rb', line 136

def to_pretty_array
  self.to_h.each.map {|k, v| "#{k}#{(' ' * (12 - k.length))}#{v}"}
end

#to_sObject



140
141
142
# File 'lib/urbit/node.rb', line 140

def to_s
  "a Node(#{self.to_h})"
end