Class: Twi::Message

Inherits:
Resource show all
Defined in:
lib/twi/message.rb

Overview

The representation of a direct message.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Twi::Resource

Class Method Details

.media_params_for(media = []) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/twi/message.rb', line 45

def self.media_params_for(media = [])
  media.each_with_index.inject({ NumMedia: media.size.to_s }) do |hash, (item, index)|
    url_key = "MediaUrl#{index}".to_sym
    content_type_key = "MediaContentType#{index}".to_sym
    hash.merge url_key => item[:url], content_type_key => item[:content_type]
  end
end

.opt_params_for(opt = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/twi/message.rb', line 53

def self.opt_params_for(opt = nil)
  case opt
    when :out then { OptOutType: 'STOP' }
    when :in then { OptOutType: 'START' }
    else {}
  end
end

.params_for(id:, sender:, recipient:, wallflower: nil, content: nil, opt: nil, media: []) ⇒ Hash

Returns the shape of the payload send by Twilio to the callback URL.

Returns:

  • (Hash)

    the shape of the payload send by Twilio to the callback URL.



34
35
36
37
38
39
40
41
# File 'lib/twi/message.rb', line 34

def self.params_for(id:, sender:, recipient:, wallflower: nil, content: nil, opt: nil, media: [])
  {
    MessageSid: id,
    From: "+1#{sender}",
    To: "+1#{recipient}",
    Body: content,
  }.merge media_params_for(media).merge opt_params_for(opt).merge wallflower_params_for(wallflower)
end

.wallflower_params_for(wallflower = nil) ⇒ Object



61
62
63
# File 'lib/twi/message.rb', line 61

def self.wallflower_params_for(wallflower = nil)
  wallflower ? { OtherRecipients0: "+1#{wallflower}" } : {}
end

Instance Method Details

#contentString?

Returns content.

Returns:

  • (String, nil)

    content



9
# File 'lib/twi/message.rb', line 9

def content = @params['Body']&.squish

#idString

Returns unique identifier.

Returns:

  • (String)

    unique identifier



6
# File 'lib/twi/message.rb', line 6

def id = @params['MessageSid']

#image_urlsArray<String>

Returns URLs of image attachments.

Returns:

  • (Array<String>)

    URLs of image attachments



27
28
29
30
31
# File 'lib/twi/message.rb', line 27

def image_urls
  (0...@params['NumMedia'].to_i).filter_map do |index|
    @params["MediaUrl#{index}"] if @params["MediaContentType#{index}"].match? %r{^image/}
  end
end

#opt_in?Boolean

Returns whether the sender replied START to resubscribe.

Returns:

  • (Boolean)

    whether the sender replied START to resubscribe.



24
# File 'lib/twi/message.rb', line 24

def opt_in? = @params['OptOutType'] == 'START'

#opt_out?Boolean

Returns whether the sender replied STOP to unsubscribe.

Returns:

  • (Boolean)

    whether the sender replied STOP to unsubscribe.



21
# File 'lib/twi/message.rb', line 21

def opt_out? = @params['OptOutType'] == 'STOP'

#recipientString

Returns 10-digit phone number receiving the SMS.

Returns:

  • (String)

    10-digit phone number receiving the SMS.



15
# File 'lib/twi/message.rb', line 15

def recipient = remove_prefix_from @params['To']

#senderString

Returns 10-digit phone number sending the SMS.

Returns:

  • (String)

    10-digit phone number sending the SMS.



12
# File 'lib/twi/message.rb', line 12

def sender = remove_prefix_from @params['From']

#wallflowerString

Returns 10-digit phone number of any CC’d recipient.

Returns:

  • (String)

    10-digit phone number of any CC’d recipient.



18
# File 'lib/twi/message.rb', line 18

def wallflower = remove_prefix_from @params['OtherRecipients0']