Class: Twi::Message
Overview
The representation of a direct message.
Class Method Summary collapse
- .media_params_for(media = []) ⇒ Object
- .opt_params_for(opt = nil) ⇒ Object
-
.params_for(id:, sender:, recipient:, wallflower: nil, content: nil, opt: nil, media: []) ⇒ Hash
The shape of the payload send by Twilio to the callback URL.
- .wallflower_params_for(wallflower = nil) ⇒ Object
Instance Method Summary collapse
-
#content ⇒ String?
Content.
-
#id ⇒ String
Unique identifier.
-
#image_urls ⇒ Array<String>
URLs of image attachments.
-
#opt_in? ⇒ Boolean
Whether the sender replied START to resubscribe.
-
#opt_out? ⇒ Boolean
Whether the sender replied STOP to unsubscribe.
-
#recipient ⇒ String
10-digit phone number receiving the SMS.
-
#sender ⇒ String
10-digit phone number sending the SMS.
-
#wallflower ⇒ String
10-digit phone number of any CC’d recipient.
Methods inherited from Resource
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.
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
#content ⇒ String?
Returns content.
9 |
# File 'lib/twi/message.rb', line 9 def content = @params['Body']&.squish |
#id ⇒ String
Returns unique identifier.
6 |
# File 'lib/twi/message.rb', line 6 def id = @params['MessageSid'] |
#image_urls ⇒ Array<String>
Returns 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.
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.
21 |
# File 'lib/twi/message.rb', line 21 def opt_out? = @params['OptOutType'] == 'STOP' |
#recipient ⇒ String
Returns 10-digit phone number receiving the SMS.
15 |
# File 'lib/twi/message.rb', line 15 def recipient = remove_prefix_from @params['To'] |
#sender ⇒ String
Returns 10-digit phone number sending the SMS.
12 |
# File 'lib/twi/message.rb', line 12 def sender = remove_prefix_from @params['From'] |
#wallflower ⇒ String
Returns 10-digit phone number of any CC’d recipient.
18 |
# File 'lib/twi/message.rb', line 18 def wallflower = remove_prefix_from @params['OtherRecipients0'] |