Class: Twi::Message

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

Overview

The representation of a direct message.

Direct Known Subclasses

Twi::Mock::Message

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Twi::Resource

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



39
40
41
# File 'lib/twi/message.rb', line 39

def status
  @status
end

Class Method Details

.media_params_for(media = []) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/twi/message.rb', line 64

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



72
73
74
75
76
77
78
# File 'lib/twi/message.rb', line 72

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.



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

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

.url_for(id) ⇒ String

Returns log URL for given message.

Returns:

  • (String)

    log URL for given message.



34
35
36
37
# File 'lib/twi/message.rb', line 34

def self.url_for(id)
   = Twi.lio.
  "https://console.twilio.com/us1/monitor/logs/sms/#{}/#{id}"
end

.wallflower_params_for(wallflower = nil) ⇒ Object



80
81
82
# File 'lib/twi/message.rb', line 80

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

#createObject

Sends a message.



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

def create
  message = api_client.messages.create messaging_service_sid: Twi.lio.messaging_sid.to_s,
    from: "+1#{@params[:sender]}", to: "+1#{@params[:recipient]}", body: @params[:content]

  @id = message.sid
  @status = message.status
rescue Twilio::REST::RestError => error
  raise Error, error
end

#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']