class Urbit::Channel
Attributes
key[R]
messages[RW]
name[R]
receiver[R]
ship[R]
Public Class Methods
new(ship:, name:)
click to toggle source
# File lib/urbit/channel.rb, line 14 def initialize(ship:, name:) @ship = ship @key = "#{Time.now.to_i}#{SecureRandom.hex(3)}" @messages = [] @name = name @receiver = nil @is_open = false end
Public Instance Methods
close()
click to toggle source
# File lib/urbit/channel.rb, line 23 def close # puts "closing #{name}" m = Urbit::CloseMessage.new(channel: self) @is_open = !self.send(message: m) end
closed?()
click to toggle source
# File lib/urbit/channel.rb, line 29 def closed? !@is_open end
open?()
click to toggle source
# File lib/urbit/channel.rb, line 33 def open? @is_open end
poke(app:, mark:, message:)
click to toggle source
One way to open a channel by “poking” an urbit app with a mark and a (json) message. A typical example of this is poking the 'hood' app using the mark 'helm-hi' to start a DM chat.
# File lib/urbit/channel.rb, line 41 def poke(app:, mark:, message:) @is_open = self.send(message: (Urbit::PokeMessage.new(channel: self, app: app, mark: mark, a_string: message))) @receiver = Urbit::Receiver.new(channel: self) self end
queue(message:)
click to toggle source
# File lib/urbit/channel.rb, line 47 def queue(message:) message.id = self.sent_messages.size + 1 @messages << message end
send(message:)
click to toggle source
Answers true if message was successfully sent.
# File lib/urbit/channel.rb, line 53 def send(message:) self.queue(message: message) resp = message.transmit resp.reason_phrase == "ok" end
sent_messages()
click to toggle source
# File lib/urbit/channel.rb, line 59 def sent_messages @messages end
status()
click to toggle source
# File lib/urbit/channel.rb, line 63 def status self.open? ? "Open" : "Closed" end
subscribe(app:, path:)
click to toggle source
Subscribe to an app at a path. Returns a Receiver
which will begin to get back a stream of facts… which is a… Dictionary? Encyclopedia?
# File lib/urbit/channel.rb, line 71 def subscribe(app:, path:) m = Urbit::SubscribeMessage.new(channel: self, app: app, path: path) @is_open = self.send(message: m) @receiver = Urbit::Receiver.new(channel: self) self end
subscribed?()
click to toggle source
# File lib/urbit/channel.rb, line 78 def subscribed? @is_open end
to_s()
click to toggle source
# File lib/urbit/channel.rb, line 82 def to_s "a Channel (#{self.status}) on #{self.ship.name}(name: '#{self.name}', key: '#{self.key}')" end
url()
click to toggle source
# File lib/urbit/channel.rb, line 86 def url "http://localhost:8080/~/channel/#{self.key}" end