Class: Mollie::SMS
- Inherits:
-
Object
- Object
- Mollie::SMS
- Defined in:
- lib/mollie/sms.rb,
lib/mollie/sms/test_helper.rb
Overview
A class that allows you to send SMS messages through the Mollie.nl SMS webservice.
See the README for examples on how to use this.
Defined Under Namespace
Modules: Exceptions, TestHelper Classes: Response
Constant Summary
- GATEWAY_URI =
The SSL URI to which the parameters of a SMS are posted.
Note that the certificate is not verified.
URI.parse("https://secure.mollie.nl/xml/sms")
- GATEWAYS =
The possible values that indicate which SMS gateway should be used.
{ 'basic' => '2', 'business' => '4', 'business+' => '1', 'landline' => '8' }
- REQUIRED_PARAMS =
A list of paramaters that must to be included in the parameters send to the gateway.
%w{ username
Class Attribute Summary (collapse)
-
+ (String) charset
A character set name, describing the message???s body encoding.
-
+ (String) gateway
The gateway that should be used to send messages.
-
+ (String) originator
The number, or display name, that will be used to indicate the originator of a message.
-
+ (String) password
A MD5 hashed version of your password for the Mollie.nl SMS webservice.
-
+ (String) type
The type of messages that will be send.
-
+ (String) username
Your username for the Mollie.nl SMS webservice.
Instance Attribute Summary (collapse)
-
- (Hash) params
readonly
The parameters that will be send to the gateway.
Class Method Summary (collapse)
-
+ (Hash) default_params
Returns the default parameters that will be we merged with each instance???s params.
Instance Method Summary (collapse)
-
- (Boolean) ==(other)
Compares whether or not this and the other Mollie::SMS instance are equal in recipient, body, and other parameters.
-
- (String) body
The message body.
-
- (String) body=(body)
Assigns the message???s body.
-
- (Response) deliver
Posts the parameters to the gateway, through SSL.
-
- (Response) deliver!
Posts the parameters through #deliver, but raises a DeliveryFailure in case the request fails.
-
- (SMS) initialize(telephone_number = nil, body = nil, extra_params = {})
constructor
Initializes a new Mollie::SMS instance.
-
- (String) inspect
A `inspect' string representation of this instance.
-
- (String) telephone_number
The recipient???s telephone number.
-
- (String) telephone_number=(telephone_number)
Assigns the recipient???s telephone number.
-
- (String) to_s
A string representation of this instance.
-
- (nil) validate_params!
Checks if all required parameters are present and if the SMS.originator is of the right size.
Constructor Details
- (SMS) initialize(telephone_number = nil, body = nil, extra_params = {})
Initializes a new Mollie::SMS instance.
You can either specify the recipient???s telephone number and message body here, or later on through the accessors for these attributes.
161 162 163 164 165 |
# File 'lib/mollie/sms.rb', line 161 def initialize(telephone_number = nil, body = nil, extra_params = {}) @params = self.class.default_params.merge(extra_params) self.telephone_number = telephone_number if telephone_number self.body = body if body end |
Class Attribute Details
+ (String) charset
A character set name, describing the message???s body encoding. Defaults to ???UTF-8???.
96 97 98 |
# File 'lib/mollie/sms.rb', line 96 def charset @charset end |
+ (String) gateway
122 123 124 |
# File 'lib/mollie/sms.rb', line 122 def gateway @gateway end |
+ (String) originator
The number, or display name, that will be used to indicate the originator of a message.
It should be upto either fourteen numerical characters, or eleven alphanumerical characters.
92 93 94 |
# File 'lib/mollie/sms.rb', line 92 def originator @originator end |
+ (String) password
A MD5 hashed version of your password for the Mollie.nl SMS webservice.
78 79 80 |
# File 'lib/mollie/sms.rb', line 78 def password @password end |
+ (String) type
The type of messages that will be send. Possible values are:
-
normal
-
wappush
-
vcard
-
flash
-
binary
-
long
Defaults to ???normal???
109 110 111 |
# File 'lib/mollie/sms.rb', line 109 def type @type end |
+ (String) username
Your username for the Mollie.nl SMS webservice.
74 75 76 |
# File 'lib/mollie/sms.rb', line 74 def username @username end |
Instance Attribute Details
- (Hash) params (readonly)
The parameters that will be send to the gateway.
148 149 150 |
# File 'lib/mollie/sms.rb', line 148 def params @params end |
Class Method Details
+ (Hash) default_params
Returns the default parameters that will be we merged with each instance???s params.
This includes username, md5_password, originator, gateway, charset, and type.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mollie/sms.rb', line 131 def default_params { 'username' => @username, 'md5_password' => @password, 'originator' => @originator, 'gateway' => @gateway, 'charset' => @charset, 'type' => @type } end |
Instance Method Details
- (Boolean) ==(other)
Compares whether or not this and the other Mollie::SMS instance are equal in recipient, body, and other parameters.
198 199 200 |
# File 'lib/mollie/sms.rb', line 198 def ==(other) other.is_a?(SMS) && other.params == params end |
- (String) body
The message body.
181 182 183 |
# File 'lib/mollie/sms.rb', line 181 def body @params['message'] end |
- (String) body=(body)
Assigns the message???s body.
189 190 191 |
# File 'lib/mollie/sms.rb', line 189 def body=(body) @params['message'] = body end |
- (Response) deliver
Posts the parameters to the gateway, through SSL.
The params are validated before attempting to post them.
219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/mollie/sms.rb', line 219 def deliver validate_params! post = Net::HTTP::Post.new(GATEWAY_URI.path) post.form_data = params request = Net::HTTP.new(GATEWAY_URI.host, GATEWAY_URI.port) request.use_ssl = true request.verify_mode = OpenSSL::SSL::VERIFY_NONE request.start do |http| response = http.request(post) Response.new(response) end end |
- (Response) deliver!
Posts the parameters through #deliver, but raises a DeliveryFailure in case the request fails.
This happens if an HTTP error occurs, or the gateway didn???t accept the parameters.
244 245 246 247 248 |
# File 'lib/mollie/sms.rb', line 244 def deliver! response = deliver raise Exceptions::DeliveryFailure.new(self, response) unless response.success? response end |
- (String) inspect
A `inspect' string representation of this instance.
208 209 210 |
# File 'lib/mollie/sms.rb', line 208 def inspect %{#<#{self.class.name} #{to_s}>} end |
- (String) telephone_number
The recipient???s telephone number.
168 169 170 |
# File 'lib/mollie/sms.rb', line 168 def telephone_number @params['recipients'] end |
- (String) telephone_number=(telephone_number)
Assigns the recipient???s telephone number.
176 177 178 |
# File 'lib/mollie/sms.rb', line 176 def telephone_number=(telephone_number) @params['recipients'] = telephone_number end |
- (String) to_s
A string representation of this instance.
203 204 205 |
# File 'lib/mollie/sms.rb', line 203 def to_s %{from: <#{params['originator']}> to: <#{telephone_number}> body: "#{body}"} end |
- (nil) validate_params!
Checks if all required parameters are present and if the originator is of the right size.
258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/mollie/sms.rb', line 258 def validate_params! params.slice(*REQUIRED_PARAMS).each do |key, value| raise Exceptions::ValidationError, "The required parameter `#{key}' is missing." if value.blank? end originator = params['originator'] if originator =~ /^\d+$/ if originator.size > 14 raise Exceptions::ValidationError, "Originator may have a maximimun of 14 numerical characters." end elsif originator.size > 11 raise Exceptions::ValidationError, "Originator may have a maximimun of 11 alphanumerical characters." end end |