Class: Urbit::Channel

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

Direct Known Subclasses

ChatChannel

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#keyObject (readonly)

Returns the value of attribute key.



12
13
14
# File 'lib/urbit/channel.rb', line 12

def key
  @key
end

#messagesObject

Returns the value of attribute messages.



11
12
13
# File 'lib/urbit/channel.rb', line 11

def messages
  @messages
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/urbit/channel.rb', line 12

def name
  @name
end

#receiverObject (readonly)

Returns the value of attribute receiver.



12
13
14
# File 'lib/urbit/channel.rb', line 12

def receiver
  @receiver
end

#shipObject (readonly)

Returns the value of attribute ship.



12
13
14
# File 'lib/urbit/channel.rb', line 12

def ship
  @ship
end

Instance Method Details

#closeObject



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

Returns:

  • (Boolean)


28
29
30
# File 'lib/urbit/channel.rb', line 28

def closed?
  !@is_open
end

#open?Boolean

Returns:

  • (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: message)))
  @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:)
  message.id = self.sent_messages.size + 1
  @messages << message
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: message)
  resp = message.transmit
  resp.reason_phrase == "ok"
end

#sent_messagesObject



58
59
60
# File 'lib/urbit/channel.rb', line 58

def sent_messages
  @messages
end

#statusObject



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

Returns:

  • (Boolean)


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

def subscribed?
  @is_open
end

#to_sObject



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

#urlObject



85
86
87
# File 'lib/urbit/channel.rb', line 85

def url
  "#{self.ship.url}/~/channel/#{self.key}"
end