Class: Talkbird::Entity::Message

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

Overview

SendBird message

Constant Summary collapse

DEFAULTS =
{
  type: 'MESG'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to, body, options = {}) ⇒ Message

Returns a new instance of Message.



24
25
26
27
28
29
30
# File 'lib/talkbird/entity/message.rb', line 24

def initialize(from, to, body, options = {})
  @sender = User.find_or_create(from)
  @receiver = User.find_or_create(to)
  @body = body

  @options = options
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



10
11
12
# File 'lib/talkbird/entity/message.rb', line 10

def body
  @body
end

#receiverObject (readonly)

Returns the value of attribute receiver.



9
10
11
# File 'lib/talkbird/entity/message.rb', line 9

def receiver
  @receiver
end

#senderObject (readonly)

Returns the value of attribute sender.



8
9
10
# File 'lib/talkbird/entity/message.rb', line 8

def sender
  @sender
end

Class Method Details

.build(response) ⇒ Object



18
19
20
# File 'lib/talkbird/entity/message.rb', line 18

def build(response)
  response
end

Instance Method Details

#deliverObject



32
33
34
35
36
37
# File 'lib/talkbird/entity/message.rb', line 32

def deliver
  return false if !sender || !receiver

  channel = Entity::Channel.find_or_create(sender.id, receiver.id)
  channel.update(self)
end

#optionsObject



46
47
48
# File 'lib/talkbird/entity/message.rb', line 46

def options
  DEFAULTS.merge(@options)
end

#to_hObject



39
40
41
42
43
44
# File 'lib/talkbird/entity/message.rb', line 39

def to_h
  options.merge(
    user_id: sender.id,
    message: body
  )
end