Class: Urbit::Channel
- Inherits:
-
Object
- Object
- Urbit::Channel
- Defined in:
- lib/urbit/channel.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#receiver ⇒ Object
readonly
Returns the value of attribute receiver.
-
#ship ⇒ Object
readonly
Returns the value of attribute ship.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(ship:, name:) ⇒ Channel
constructor
A new instance of Channel.
- #open? ⇒ Boolean
-
#poke(app:, mark:, message:) ⇒ Object
Poke an app with a message using a mark.
- #queue(message:) ⇒ Object
-
#send(message:) ⇒ Object
Answers true if message was successfully sent.
- #sent_messages ⇒ Object
- #status ⇒ Object
-
#subscribe(app:, path:) ⇒ Object
Subscribe to an app at a path.
- #subscribed? ⇒ Boolean
- #to_s ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(ship:, name:) ⇒ Channel
Returns a new instance of Channel.
14 15 16 17 18 19 20 21 |
# 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 |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
12 13 14 |
# File 'lib/urbit/channel.rb', line 12 def key @key end |
#messages ⇒ Object
Returns the value of attribute messages.
11 12 13 |
# File 'lib/urbit/channel.rb', line 11 def @messages end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/urbit/channel.rb', line 12 def name @name end |
#receiver ⇒ Object (readonly)
Returns the value of attribute receiver.
12 13 14 |
# File 'lib/urbit/channel.rb', line 12 def receiver @receiver end |
#ship ⇒ Object (readonly)
Returns the value of attribute ship.
12 13 14 |
# File 'lib/urbit/channel.rb', line 12 def ship @ship end |
Instance Method Details
#close ⇒ Object
23 24 25 26 |
# File 'lib/urbit/channel.rb', line 23 def close m = Urbit::CloseMessage.new(channel: self) @is_open = !self.send(message: m) end |
#closed? ⇒ Boolean
28 29 30 |
# File 'lib/urbit/channel.rb', line 28 def closed? !@is_open end |
#open? ⇒ Boolean
32 33 34 |
# File 'lib/urbit/channel.rb', line 32 def open? @is_open end |
#poke(app:, mark:, message:) ⇒ Object
Poke an app with a message using a mark. The message must be a Ruby Hash, not a String.
40 41 42 43 44 |
# File 'lib/urbit/channel.rb', line 40 def poke(app:, mark:, message:) @is_open = self.send(message: (Urbit::PokeMessage.new(channel: self, app: app, mark: mark, a_message_hash: ))) @receiver = Urbit::Receiver.new(channel: self) self end |
#queue(message:) ⇒ Object
46 47 48 49 |
# File 'lib/urbit/channel.rb', line 46 def queue(message:) .id = self..size + 1 @messages << end |
#send(message:) ⇒ Object
Answers true if message was successfully sent.
52 53 54 55 56 |
# File 'lib/urbit/channel.rb', line 52 def send(message:) self.queue(message: ) resp = .transmit resp.reason_phrase == "ok" end |
#sent_messages ⇒ Object
58 59 60 |
# File 'lib/urbit/channel.rb', line 58 def @messages end |
#status ⇒ Object
62 63 64 |
# File 'lib/urbit/channel.rb', line 62 def status self.open? ? "Open" : "Closed" end |
#subscribe(app:, path:) ⇒ Object
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?
70 71 72 73 74 75 |
# File 'lib/urbit/channel.rb', line 70 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? ⇒ Boolean
77 78 79 |
# File 'lib/urbit/channel.rb', line 77 def subscribed? @is_open end |
#to_s ⇒ Object
81 82 83 |
# File 'lib/urbit/channel.rb', line 81 def to_s "a Channel (#{self.status}) on #{self.ship.name}(name: '#{self.name}', key: '#{self.key}')" end |
#url ⇒ Object
85 86 87 |
# File 'lib/urbit/channel.rb', line 85 def url "#{self.ship.url}/~/channel/#{self.key}" end |