Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Validate params before sending, instead of on class level.

  • Loading branch information...
commit cbd821ffba0d064cd417e84adf63044891d66425 1 parent 6ea2c58
Eloy Durán alloy authored
Showing with 31 additions and 18 deletions.
  1. +16 −14 lib/mollie/sms.rb
  2. +15 −4 spec/sms_spec.rb
30 lib/mollie/sms.rb
View
@@ -32,17 +32,6 @@ def password=(password)
@password = Digest::MD5.hexdigest(password)
end
- def originator=(originator)
- if originator =~ /^\d+$/
- if originator.size > 14
- raise ValidationError, "Originator may have a maximimun of 14 numerical characters."
- end
- elsif originator.size > 11
- raise ValidationError, "Originator may have a maximimun of 11 alphanumerical characters."
- end
- @originator = originator
- end
-
def default_params
{
'username' => @username,
@@ -88,9 +77,7 @@ def inspect
end
def deliver
- params.slice(*REQUIRED_PARAMS).each do |key, value|
- raise MissingRequiredParam, "The required parameter `#{key}' is missing." if value.blank?
- end
+ validate_params!
post = Net::HTTP::Post.new(GATEWAY_URI.path)
post.form_data = params
@@ -102,6 +89,21 @@ def deliver
end
end
+ def validate_params!
+ params.slice(*REQUIRED_PARAMS).each do |key, value|
+ raise MissingRequiredParam, "The required parameter `#{key}' is missing." if value.blank?
+ end
+
+ originator = params['originator']
+ if originator =~ /^\d+$/
+ if originator.size > 14
+ raise ValidationError, "Originator may have a maximimun of 14 numerical characters."
+ end
+ elsif originator.size > 11
+ raise ValidationError, "Originator may have a maximimun of 11 alphanumerical characters."
+ end
+ end
+
class Response
attr_reader :http_response
19 spec/sms_spec.rb
View
@@ -104,6 +104,7 @@
it "posts the post body to the gateway" do
@sms.stubs(:params).returns('a key' => 'a value')
+ @sms.stubs(:validate_params!)
@sms.deliver
request, post = Net::HTTP.posted
@@ -208,23 +209,33 @@
end
describe "Mollie::SMS, concerning validation" do
+ before do
+ @sms = Mollie::SMS.new
+ @sms.telephone_number = '+31612345678'
+ @sms.body = "The stars tell me you will have chicken noodle soup for breakfast."
+ end
+
it "accepts an originator of upto 14 numbers" do
- lambda { Mollie::SMS.originator = "00000000001111" }.should.not.raise
+ @sms.params['originator'] = "00000000001111"
+ lambda { @sms.deliver }.should.not.raise
end
it "does not accept an originator string with more than 14 numbers" do
+ @sms.params['originator'] = "000000000011112"
lambda do
- Mollie::SMS.originator = "000000000011112"
+ @sms.deliver
end.should.raise(Mollie::SMS::ValidationError, "Originator may have a maximimun of 14 numerical characters.")
end
it "accepts an originator of upto 11 alphanumerical characters" do
- lambda { Mollie::SMS.originator = "0123456789A" }.should.not.raise
+ @sms.params['originator'] = "0123456789A"
+ lambda { @sms.deliver }.should.not.raise
end
it "does not accept an originator string with more than 11 alphanumerical characters" do
+ @sms.params['originator'] = "0123456789AB"
lambda do
- Mollie::SMS.originator = "0123456789AB"
+ @sms.deliver
end.should.raise(Mollie::SMS::ValidationError, "Originator may have a maximimun of 11 alphanumerical characters.")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.