Class: Xip::Services::Twilio::ReplyHandler

Inherits:
BaseReplyHandler
  • Object
show all
Defined in:
lib/xip/services/twilio/reply_handler.rb

Constant Summary collapse

ALPHA_ORDINALS =
('A'..'Z').to_a.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipient_id: nil, reply: nil) ⇒ ReplyHandler

Returns a new instance of ReplyHandler.



12
13
14
15
# File 'lib/xip/services/twilio/reply_handler.rb', line 12

def initialize(recipient_id: nil, reply: nil)
  @recipient_id = recipient_id
  @reply = reply
end

Instance Attribute Details

#recipient_idObject (readonly)

Returns the value of attribute recipient_id.



10
11
12
# File 'lib/xip/services/twilio/reply_handler.rb', line 10

def recipient_id
  @recipient_id
end

#replyObject (readonly)

Returns the value of attribute reply.



10
11
12
# File 'lib/xip/services/twilio/reply_handler.rb', line 10

def reply
  @reply
end

Instance Method Details

#audioObject



57
58
59
60
61
# File 'lib/xip/services/twilio/reply_handler.rb', line 57

def audio
  check_text_length

  format_response({ body: reply['text'], media_url: reply['audio_url'] })
end

#delayObject



75
76
77
# File 'lib/xip/services/twilio/reply_handler.rb', line 75

def delay

end

#fileObject



69
70
71
72
73
# File 'lib/xip/services/twilio/reply_handler.rb', line 69

def file
  check_text_length

  format_response({ body: reply['text'], media_url: reply['file_url'] })
end

#imageObject



51
52
53
54
55
# File 'lib/xip/services/twilio/reply_handler.rb', line 51

def image
  check_text_length

  format_response({ body: reply['text'], media_url: reply['image_url'] })
end

#textObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xip/services/twilio/reply_handler.rb', line 17

def text
  check_text_length

  translated_reply = reply['text']

  suggestions = generate_suggestions(suggestions: reply['suggestions'])
  buttons = generate_buttons(buttons: reply['buttons'])

  if suggestions.present?
    translated_reply = [
      translated_reply,
      'Reply with:'
    ].join("\n\n")

    suggestions.each_with_index do |suggestion, i|
      translated_reply = [
        translated_reply,
        "\"#{ALPHA_ORDINALS[i]}\" for #{suggestion}"
      ].join("\n")
    end
  end

  if buttons.present?
    buttons.each do |button|
      translated_reply = [
        translated_reply,
        button
      ].join("\n\n")
    end
  end

  format_response({ body: translated_reply })
end

#videoObject



63
64
65
66
67
# File 'lib/xip/services/twilio/reply_handler.rb', line 63

def video
  check_text_length

  format_response({ body: reply['text'], media_url: reply['video_url'] })
end