Class: Twilio::Rails::SMS::DelegatedResponder
- Inherits:
-
Object
- Object
- Twilio::Rails::SMS::DelegatedResponder
- Defined in:
- lib/twilio/rails/sms/delegated_responder.rb
Overview
Base class for SMS responders. To define a responder start by generating a sublcass.
rails generate twilio:rails:sms_responder ThankYou
This will create a new class in app/sms_responders/thank_you_responder.rb
which will subclass this class. It
must be registered with the framework in the initializer for it to be available. The generator does this.
# config/initializers/twilio_rails.rb
config.sms_responders.register { ThankYouResponder }
Then the responder must implement the #handle? and #reply methods. If the #handle? method returns true then the #reply method will be called to generate the body of the response, and send that message back as an SMS. Only one responder will be called for a given message.
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#sms_conversation ⇒ Object
readonly
Returns the value of attribute sms_conversation.
Class Method Summary collapse
-
.responder_name ⇒ String
Returns the name of the class, without the namespace or the
Responder
suffix.
Instance Method Summary collapse
-
#handle? ⇒ true, false
Must be implemented by the subclass otherwise will raise a
NoMethodError
. -
#initialize(message) ⇒ DelegatedResponder
constructor
A new instance of DelegatedResponder.
-
#reply ⇒ String?
Must be implemented by the subclass otherwise will raise a
NoMethodError
.
Constructor Details
#initialize(message) ⇒ DelegatedResponder
Returns a new instance of DelegatedResponder.
41 42 43 44 |
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 41 def initialize() @message = @sms_conversation = .sms_conversation end |
Instance Attribute Details
#message ⇒ Object (readonly)
Returns the value of attribute message.
30 31 32 |
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 30 def @message end |
#sms_conversation ⇒ Object (readonly)
Returns the value of attribute sms_conversation.
30 31 32 |
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 30 def sms_conversation @sms_conversation end |
Class Method Details
.responder_name ⇒ String
Returns the name of the class, without the namespace or the Responder
suffix.
36 37 38 |
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 36 def responder_name self.name.demodulize.underscore.gsub(/_responder\Z/, "") end |
Instance Method Details
#handle? ⇒ true, false
Must be implemented by the subclass otherwise will raise a NoMethodError
. Returns true if this
responder should handle the given message. If true then the #reply method will be called to generate the
body of the response. It has access to the message and the conversation.
51 52 53 |
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 51 def handle? raise NoMethodError, "#{ self.class }#handle? must be implemented." end |
#reply ⇒ String?
Must be implemented by the subclass otherwise will raise a NoMethodError
. Returns the body of the
message to be sent in response. Will only be called if #handle? returns true. It has access to the message
and the conversation.
60 61 62 |
# File 'lib/twilio/rails/sms/delegated_responder.rb', line 60 def reply raise NoMethodError, "#{ self.class }#reply must be implemented." end |