Class: Stealth::Services::Bandwidth::EventHandler

Inherits:
Stealth::ServiceEvent
  • Object
show all
Defined in:
lib/stealth/services/bandwidth/event_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, headers:) ⇒ EventHandler

Returns a new instance of EventHandler.



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

def initialize(params:, headers:)
  super(service: 'bandwidth')
  @params = parse(params)
  @headers = headers
  @attachments = []
end

Instance Attribute Details

#attachmentsObject

Returns the value of attribute attachments.



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

def attachments
  @attachments
end

#eventObject

Returns the value of attribute event.



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

def event
  @event
end

#event_typeObject

Returns the value of attribute event_type.



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

def event_type
  @event_type
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/stealth/services/bandwidth/event_handler.rb', line 6

def headers
  @headers
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#nlp_resultObject

Returns the value of attribute nlp_result.



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

def nlp_result
  @nlp_result
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/stealth/services/bandwidth/event_handler.rb', line 6

def params
  @params
end

#previous_messageObject

Returns the value of attribute previous_message.



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

def previous_message
  @previous_message
end

#selected_optionObject

Returns the value of attribute selected_option.



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

def selected_option
  @selected_option
end

#sender_idObject

Returns the value of attribute sender_id.



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

def sender_id
  @sender_id
end

#target_idObject

Returns the value of attribute target_id.



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

def target_id
  @target_id
end

#timestampObject

Returns the value of attribute timestamp.



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

def timestamp
  @timestamp
end

Instance Method Details

#coordinateObject



16
17
18
19
20
# File 'lib/stealth/services/bandwidth/event_handler.rb', line 16

def coordinate
  return [202, 'Accepted'] if should_ignore_event?(params)
  Stealth::Services::HandleEventJob.perform_async('bandwidth', params, headers)
  [202, 'Accepted']
end

#processObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/stealth/services/bandwidth/event_handler.rb', line 23

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

  mapped_event = map_event_type
  self.event_type = mapped_event[:event_type]
  self.event = mapped_event[:event]

  self
end