Class: Stealth::Services::Bandwidth::MessageHandler

Inherits:
Stealth::Services::BaseMessageHandler
  • Object
show all
Defined in:
lib/stealth/services/bandwidth/message_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, headers:) ⇒ MessageHandler

Returns a new instance of MessageHandler.



9
10
11
12
# File 'lib/stealth/services/bandwidth/message_handler.rb', line 9

def initialize(params:, headers:)
  @params = params
  @headers = headers
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/stealth/services/bandwidth/message_handler.rb', line 7

def headers
  @headers
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/stealth/services/bandwidth/message_handler.rb', line 7

def params
  @params
end

#service_messageObject (readonly)

Returns the value of attribute service_message.



7
8
9
# File 'lib/stealth/services/bandwidth/message_handler.rb', line 7

def service_message
  @service_message
end

Instance Method Details

#coordinateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/stealth/services/bandwidth/message_handler.rb', line 14

def coordinate
  case params.dig('message', 'direction')
  when "in"
    Stealth::Services::HandleMessageJob.perform_async(
      'bandwidth',
      params,
      headers
    )
  when "out"
    # Ignoring outbound messages
  end

  # Relay our acceptance
  [202, 'Accepted']
end

#processObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/stealth/services/bandwidth/message_handler.rb', line 30

def process
  @service_message = ServiceMessage.new(service: 'bandwidth')

  service_message.sender_id = params.dig('message', 'from')
  service_message.target_id = params.dig('message', 'to')
  service_message.message = params.dig('message', 'text')
  service_message.timestamp = params.dig('message', 'time')
  params.dig('message', 'media')&.each do |attachment_url|
    service_message.attachments << {
      url: attachment_url
    }
  end

  service_message
end