Class: Babili::Platform::Message

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/babili/platform/message.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(params = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/babili/platform/message.rb', line 8

def self.create(params = {})
  room_id          = params.delete(:room_id)
  create_path      = path.gsub(":room_id", room_id)
  sender_id        = params[:sender_id] || params("sender_id")
  formatted_params = {
    data: {
      type:          "mesage",
      id:            params[:id] || params["id"],
      relationships: {},
      attributes:    {
        content:          params[:content] || params["content"],
        content_type:     params[:content_type] || params["content_type"],
        read_by_user_ids: params[:read_by_user_ids] || params["read_by_user_ids"]
      }
    }
  }
  if sender_id
    formatted_params[:data][:relationships][:user] = {
      data: { id: sender_id }
    }
  end
  raw_message     = Babili::Client.post(create_path, formatted_params)["data"]
  message         = new(raw_message["attributes"])
  message.id      = raw_message["id"]
  message.room_id = room_id
  message
end

.pathObject



4
5
6
# File 'lib/babili/platform/message.rb', line 4

def self.path
  "platform/rooms/:room_id/messages"
end

Instance Method Details

#deleteObject



36
37
38
39
40
41
42
# File 'lib/babili/platform/message.rb', line 36

def delete
  path        = self.class.path.gsub(":room_id", room_id) + "/#{id}"
  raw_message = Babili::Client.delete(path)["data"]
  message     = self.class.new(raw_message["attributes"])
  message.id  = raw_message["id"]
  message
end