Class: Tropo::Message
- Inherits:
-
Object
- Object
- Tropo::Message
- Defined in:
- lib/tropo_message.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
-
#tropo_session ⇒ Object
Returns the value of attribute tropo_session.
Instance Method Summary collapse
-
#action ⇒ Object
Getter/Setter methods.
- #answer_on_media ⇒ Object
- #channel ⇒ Object
- #from ⇒ Object
- #from=(value) ⇒ Object
- #headers ⇒ Object
-
#initialize(tropo_session = {}) ⇒ Message
constructor
A new instance of Message.
- #network ⇒ Object
-
#outgoing? ⇒ Boolean
Determines whether a message is meant for sending by checking if it has session parameters.
-
#parse(tropo_session) ⇒ Object
An alternative to the constructor.
- #recording ⇒ Object
-
#request_xml ⇒ Object
Generates xml suitable for an XML POST request to Tropo.
-
#response_params ⇒ Object
Generates a hash suitable for using to as input to: Tropo::Generator#message (see: github.com/voxeo/tropo-webapi-ruby).
- #text ⇒ Object
- #text=(value) ⇒ Object
- #timeout ⇒ Object
- #to ⇒ Object
- #to=(value) ⇒ Object
- #token ⇒ Object
- #token=(value) ⇒ Object
Constructor Details
#initialize(tropo_session = {}) ⇒ Message
Returns a new instance of Message.
6 7 8 9 |
# File 'lib/tropo_message.rb', line 6 def initialize(tropo_session = {}) @params = {} @tropo_session = tropo_session end |
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
4 5 6 |
# File 'lib/tropo_message.rb', line 4 def params @params end |
#tropo_session ⇒ Object
Returns the value of attribute tropo_session.
4 5 6 |
# File 'lib/tropo_message.rb', line 4 def tropo_session @tropo_session end |
Instance Method Details
#action ⇒ Object
Getter/Setter methods
111 112 113 |
# File 'lib/tropo_message.rb', line 111 def action params["action"] || tropo_parameters["action"] end |
#answer_on_media ⇒ Object
115 116 117 |
# File 'lib/tropo_message.rb', line 115 def answer_on_media params["answer_on_media"] || tropo_parameters["answer_on_media"] end |
#channel ⇒ Object
119 120 121 |
# File 'lib/tropo_message.rb', line 119 def channel params["channel"] || tropo_parameters["channel"] || "TEXT" end |
#from ⇒ Object
123 124 125 |
# File 'lib/tropo_message.rb', line 123 def from params["from"] || tropo_parameters["from"] end |
#from=(value) ⇒ Object
127 128 129 |
# File 'lib/tropo_message.rb', line 127 def from=(value) params["from"] = value end |
#headers ⇒ Object
131 132 133 |
# File 'lib/tropo_message.rb', line 131 def headers params["headers"] || tropo_parameters["headers"] end |
#network ⇒ Object
135 136 137 |
# File 'lib/tropo_message.rb', line 135 def network params["network"] || tropo_parameters["network"] || "SMS" end |
#outgoing? ⇒ Boolean
22 23 24 |
# File 'lib/tropo_message.rb', line 22 def outgoing? tropo_session["session"] && tropo_session["session"]["parameters"] end |
#parse(tropo_session) ⇒ Object
33 34 35 |
# File 'lib/tropo_message.rb', line 33 def parse(tropo_session) self.tropo_session = tropo_session end |
#recording ⇒ Object
139 140 141 |
# File 'lib/tropo_message.rb', line 139 def recording params["recording"] || tropo_parameters["recording"] end |
#request_xml ⇒ Object
Generates xml suitable for an XML POST request to Tropo
Example:
= Tropo::Message.new
.to = "44122782474"
.text = "Hi John, how r u today?"
.token = "1234512345"
.request_xml # =>
# <sessions>
# <token>1234512345</token>
# <var name="to" value="44122782474"/>
# <var name="text" value="Hi+John%2C+how+r+u+today%3F"/>
# </sessions>"
52 53 54 55 56 57 58 59 60 |
# File 'lib/tropo_message.rb', line 52 def request_xml request_params = @params.dup token = request_params.delete("token") xml = "" request_params.each do |key, value| xml << "<var name=\"#{escape(key)}\" value=\"#{escape(value)}\"/>" end "<sessions><token>#{token}</token>#{xml}</sessions>" end |
#response_params ⇒ Object
Generates a hash suitable for using to as input to: Tropo::Generator#message (see: github.com/voxeo/tropo-webapi-ruby)
By default, “channel” => “TEXT” and “network” => “SMS” You can override these values and any other optional parameters by setting their values e.g. tropo_message.channel = “VOICE”
Example:
tropo_object = Tropo::Generator.parse(raw_json)
tropo_object # => {
# "session" => {
# ...
# "parameters" => {
# "to" => "44122782474",
# "text" => "Hi+John%2C+how+r+u+today%3F",
# "my_favourite_food" => "Pizza"
# }
# }
#}
= Tropo::Message.new(tropo_object)
response_params = .response_params # => {
# "to" => "44122782474",
# "channel" => "TEXT",
# "network" => "SMS"
#}
text = .text
Tropo::Generator.new.(response_params) do
say :value => text
end
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tropo_message.rb', line 96 def response_params params = { "to" => to, "channel" => channel, "network" => network } params.merge!("from" => from) if from params.merge!("timeout" => timeout) if timeout params.merge!("answer_on_media" => answer_on_media) if answer_on_media params.merge!("headers" => headers) if headers params.merge!("recording" => recording) if recording params end |
#text ⇒ Object
143 144 145 |
# File 'lib/tropo_message.rb', line 143 def text params["text"] || tropo_parameters["text"] end |
#text=(value) ⇒ Object
147 148 149 |
# File 'lib/tropo_message.rb', line 147 def text=(value) params["text"] = value end |
#timeout ⇒ Object
151 152 153 |
# File 'lib/tropo_message.rb', line 151 def timeout params["timeout"] || tropo_parameters["timeout"] end |
#to ⇒ Object
155 156 157 |
# File 'lib/tropo_message.rb', line 155 def to params["to"] || tropo_parameters["to"] end |
#to=(value) ⇒ Object
159 160 161 |
# File 'lib/tropo_message.rb', line 159 def to=(value) params["to"] = value end |
#token ⇒ Object
163 164 165 |
# File 'lib/tropo_message.rb', line 163 def token params["token"] || tropo_parameters["token"] end |
#token=(value) ⇒ Object
167 168 169 |
# File 'lib/tropo_message.rb', line 167 def token=(value) params["token"] = value end |