Class: Urbit::Node
- Inherits:
-
Object
- Object
- Urbit::Node
- Defined in:
- lib/urbit/node.rb
Instance Attribute Summary collapse
-
#node_json ⇒ Object
Returns the value of attribute node_json.
Class Method Summary collapse
-
.da_to_unix(da) ⇒ Object
Given a bigint representing an urbit date, returns a unix timestamp.
-
.unix_to_da(unix) ⇒ Object
Given a unix timestamp, returns a bigint representing an urbit date.
Instance Method Summary collapse
- #<=>(another_node) ⇒ Object
- #==(another_node) ⇒ Object
- #author ⇒ Object
- #children ⇒ Object
- #contents ⇒ Object
- #datetime_sent ⇒ Object
- #deleted? ⇒ Boolean
- #eql?(another_node) ⇒ Boolean
- #hash ⇒ Object
-
#index ⇒ Object
Answers the memoized @index or calculates it from the raw_index.
-
#initialize(graph:, node_json:) ⇒ Node
constructor
A new instance of Node.
-
#next(count: 1) ⇒ Object
Answers the next count Nodes relative to this Node.
- #parent? ⇒ Boolean
- #persistent? ⇒ Boolean
-
#previous(count: 1) ⇒ Object
Answers the previous count Nodes relative to this Node.
- #raw_index ⇒ Object
-
#time_sent ⇒ Object
This is the time sent as recorded by urbit in unix extended format.
- #to_h ⇒ Object
- #to_pretty_array ⇒ Object
- #to_s ⇒ Object
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_json ⇒ Object
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 |
#author ⇒ Object
57 58 59 |
# File 'lib/urbit/node.rb', line 57 def @post_h["author"] end |
#children ⇒ Object
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 |
#contents ⇒ Object
73 74 75 |
# File 'lib/urbit/node.rb', line 73 def contents @post_h['contents'] end |
#datetime_sent ⇒ Object
77 78 79 |
# File 'lib/urbit/node.rb', line 77 def datetime_sent Time.at(self.time_sent / 1000).to_datetime end |
#deleted? ⇒ 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
44 45 46 |
# File 'lib/urbit/node.rb', line 44 def eql?(another_node) another_node.index == self.index end |
#hash ⇒ Object
53 54 55 |
# File 'lib/urbit/node.rb', line 53 def hash self.index.hash end |
#index ⇒ Object
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
101 102 103 |
# File 'lib/urbit/node.rb', line 101 def parent? !self.children.empty? end |
#persistent? ⇒ 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_index ⇒ Object
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_sent ⇒ Object
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_h ⇒ Object
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., sent: self.datetime_sent, contents: self.contents, is_parent: self.parent?, child_count: self.children.count } end |
#to_pretty_array ⇒ Object
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_s ⇒ Object
140 141 142 |
# File 'lib/urbit/node.rb', line 140 def to_s "a Node(#{self.to_h})" end |