Class: Urbit::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel:, app: nil, mark: nil, contents: nil) ⇒ Message

Returns a new instance of Message.



8
9
10
11
12
13
14
# File 'lib/urbit/message.rb', line 8

def initialize(channel:, app: nil, mark: nil, contents: nil)
  @app      = app
  @channel  = channel
  @contents = contents
  @id       = 0
  @mark     = mark
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/urbit/message.rb', line 6

def app
  @app
end

#channelObject (readonly)

Returns the value of attribute channel.



6
7
8
# File 'lib/urbit/message.rb', line 6

def channel
  @channel
end

#contentsObject (readonly)

Returns the value of attribute contents.



6
7
8
# File 'lib/urbit/message.rb', line 6

def contents
  @contents
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#markObject (readonly)

Returns the value of attribute mark.



6
7
8
# File 'lib/urbit/message.rb', line 6

def mark
  @mark
end

Instance Method Details

#actionObject

The value for “action” that the inbound API expects for this message type. defaults to “poke” for historical reasons, but each subclass should override appropriately.



20
21
22
# File 'lib/urbit/message.rb', line 20

def action
  "poke"
end

#channel_urlObject



24
25
26
# File 'lib/urbit/message.rb', line 24

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

#request_bodyObject



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

def request_body
  JSON.generate(self.to_a)
end

#shipObject



32
33
34
# File 'lib/urbit/message.rb', line 32

def ship
  self.channel.ship
end

#to_aObject



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

def to_a
  [self.to_h]
end

#to_hObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/urbit/message.rb', line 40

def to_h
  {
    action: self.action,
    app:    app,
    id:     id,
    json:   contents,
    mark:   mark,
    ship:   ship.untilded_name
  }
end

#to_sObject



51
52
53
# File 'lib/urbit/message.rb', line 51

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

#transmitObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/urbit/message.rb', line 55

def transmit
  response = Faraday.put(channel_url) do |req|
    req.headers['Cookie'] = self.ship.cookie
    req.headers['Content-Type'] = 'application/json'
    req.body = request_body
  end

  # TODO: handle_error if response.status != 204
  response
end