mollie-sms 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ desc "Run the specs"
2
+ task :spec do
3
+ sh "ruby ./spec/sms_spec.rb"
4
+ sh "ruby ./spec/test_helper_spec.rb"
5
+ sh "ruby ./spec/functional_sms_deliver_spec.rb"
6
+ end
7
+
8
+ task :default => :spec
9
+
10
+ begin
11
+ require 'rubygems'
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |s|
14
+ s.name = "mollie-sms"
15
+ s.homepage = "http://github.com/Fingertips/Mollie-SMS"
16
+ s.email = ["eloy@fngtps.com"]
17
+ s.authors = ["Eloy Duran"]
18
+ s.summary = s.description = "Send SMS text messages via the Mollie.nl SMS gateway."
19
+ s.files -= %w{ .gitignore TODO }
20
+ s.extra_rdoc_files -= %w{ TODO }
21
+ s.add_dependency('activesupport', '>= 2.3.8')
22
+ end
23
+ rescue LoadError
24
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/lib/mollie/sms.rb CHANGED
@@ -23,6 +23,18 @@ module Mollie
23
23
  class ValidationError < StandardError; end
24
24
  class MissingRequiredParam < StandardError; end
25
25
 
26
+ class DeliveryFailure < StandardError
27
+ attr_reader :sms, :response
28
+
29
+ def initialize(sms, response)
30
+ @sms, @response = sms, response
31
+ end
32
+
33
+ def message
34
+ "(#{@response.message}) #{@sms.to_s}"
35
+ end
36
+ end
37
+
26
38
  REQUIRED_PARAMS = %w{ username md5_password originator gateway charset type recipients message }
27
39
 
28
40
  class << self
@@ -72,8 +84,16 @@ module Mollie
72
84
  @params['message'] = body
73
85
  end
74
86
 
87
+ def ==(other)
88
+ other.is_a?(SMS) && other.params == params
89
+ end
90
+
91
+ def to_s
92
+ %{from: <#{params['originator']}> to: <#{telephone_number}> body: "#{body}"}
93
+ end
94
+
75
95
  def inspect
76
- "#<#{self.class.name} from: <#{@params['originator']}> to: <#{telephone_number}> body: \"#{body}\" >"
96
+ %{#<#{self.class.name} #{to_s}>}
77
97
  end
78
98
 
79
99
  def deliver
@@ -89,6 +109,12 @@ module Mollie
89
109
  end
90
110
  end
91
111
 
112
+ def deliver!
113
+ response = deliver
114
+ raise DeliveryFailure.new(self, response) unless response.success?
115
+ response
116
+ end
117
+
92
118
  def validate_params!
93
119
  params.slice(*REQUIRED_PARAMS).each do |key, value|
94
120
  raise MissingRequiredParam, "The required parameter `#{key}' is missing." if value.blank?
@@ -0,0 +1,76 @@
1
+ module Mollie
2
+ class SMS
3
+ def self.deliveries
4
+ @deliveries ||= []
5
+ end
6
+
7
+ def self.reset!
8
+ @deliveries = []
9
+ success!
10
+ end
11
+
12
+ def self.success!
13
+ http_response = Net::HTTPOK.new('1.1', '200', 'OK')
14
+ http_response.add_field('Content-type', 'application/xml')
15
+ def http_response.read_body; TestHelper::SUCCESS_BODY; end
16
+ @stubbed_response = Mollie::SMS::Response.new(http_response)
17
+ end
18
+
19
+ def self.gateway_failure!
20
+ http_response = Net::HTTPOK.new('1.1', '200', 'OK')
21
+ http_response.add_field('Content-type', 'application/xml')
22
+ def http_response.read_body; TestHelper::FAILURE_BODY; end
23
+ @stubbed_response = Mollie::SMS::Response.new(http_response)
24
+ end
25
+
26
+ def self.http_failure!
27
+ @stubbed_response = Mollie::SMS::Response.new(Net::HTTPBadRequest.new('1.1', '400', 'Bad request'))
28
+ end
29
+
30
+ def self.stubbed_response
31
+ @stubbed_response || success!
32
+ end
33
+
34
+ def deliver
35
+ validate_params!
36
+ self.class.deliveries << self
37
+ self.class.stubbed_response
38
+ end
39
+
40
+ module TestHelper
41
+ SUCCESS_BODY = %{
42
+ <?xml version="1.0" ?>
43
+ <response>
44
+ <item type="sms">
45
+ <recipients>1</recipients>
46
+ <success>true</success>
47
+ <resultcode>10</resultcode>
48
+ <resultmessage>Message successfully sent.</resultmessage>
49
+ </item>
50
+ </response>}
51
+
52
+ FAILURE_BODY = %{
53
+ <?xml version="1.0"?>
54
+ <response>
55
+ <item type="sms">
56
+ <recipients>1</recipients>
57
+ <success>false</success>
58
+ <resultcode>20</resultcode>
59
+ <resultmessage>No username given.</resultmessage>
60
+ </item>
61
+ </response>}
62
+
63
+ def assert_sms_messages(number_of_messages)
64
+ before = Mollie::SMS.deliveries.length
65
+ yield
66
+ diff = Mollie::SMS.deliveries.length - before
67
+ assert(diff == number_of_messages, "expected `#{number_of_messages}' SMS messages to be sent, actually sent `#{diff}'")
68
+ end
69
+
70
+ def assert_no_sms_messages
71
+ assert_sms_messages(0) { yield }
72
+ end
73
+ end
74
+ end
75
+ end
76
+
@@ -0,0 +1,57 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mollie-sms}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Eloy Duran"]
12
+ s.date = %q{2010-08-03}
13
+ s.description = %q{Send SMS text messages via the Mollie.nl SMS gateway.}
14
+ s.email = ["eloy@fngtps.com"]
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README"
18
+ ]
19
+ s.files = [
20
+ "LICENSE",
21
+ "README",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "lib/mollie/sms.rb",
25
+ "lib/mollie/sms/test_helper.rb",
26
+ "mollie-sms.gemspec",
27
+ "spec/functional_sms_deliver_spec.rb",
28
+ "spec/sms_spec.rb",
29
+ "spec/spec_helper.rb",
30
+ "spec/test_helper_spec.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/Fingertips/Mollie-SMS}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Send SMS text messages via the Mollie.nl SMS gateway.}
37
+ s.test_files = [
38
+ "spec/functional_sms_deliver_spec.rb",
39
+ "spec/sms_spec.rb",
40
+ "spec/spec_helper.rb",
41
+ "spec/test_helper_spec.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.8"])
50
+ else
51
+ s.add_dependency(%q<activesupport>, [">= 2.3.8"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<activesupport>, [">= 2.3.8"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,71 @@
1
+ require File.expand_path("../spec_helper", __FILE__)
2
+
3
+ module Net
4
+ class HTTP
5
+ class << self
6
+ attr_accessor :posted, :stubbed_response
7
+
8
+ def reset!
9
+ @posted = nil
10
+ @stubbed_response = nil
11
+ end
12
+ end
13
+
14
+ def host
15
+ @address
16
+ end
17
+
18
+ def start
19
+ yield self
20
+ end
21
+
22
+ def request(request)
23
+ self.class.posted = [self, request]
24
+ self.class.stubbed_response
25
+ end
26
+ end
27
+ end
28
+
29
+ SUCCESS_BODY = %{
30
+ <?xml version="1.0" ?>
31
+ <response>
32
+ <item type="sms">
33
+ <recipients>1</recipients>
34
+ <success>true</success>
35
+ <resultcode>10</resultcode>
36
+ <resultmessage>Message successfully sent.</resultmessage>
37
+ </item>
38
+ </response>}
39
+
40
+ describe "When sending a Mollie::SMS message" do
41
+ before do
42
+ @sms = Mollie::SMS.new
43
+ @sms.telephone_number = '+31612345678'
44
+ @sms.body = "The stars tell me you will have chicken noodle soup for breakfast."
45
+ end
46
+
47
+ after do
48
+ Net::HTTP.reset!
49
+ end
50
+
51
+ it "posts the post body to the gateway" do
52
+ @sms.stubs(:params).returns('a key' => 'a value')
53
+ @sms.stubs(:validate_params!)
54
+ @sms.deliver
55
+
56
+ request, post = Net::HTTP.posted
57
+ request.should.use_ssl
58
+ request.host.should == Mollie::SMS::GATEWAY_URI.host
59
+ request.port.should == Mollie::SMS::GATEWAY_URI.port
60
+ post.path.should == Mollie::SMS::GATEWAY_URI.path
61
+ post.body.should == "a%20key=a%20value"
62
+ end
63
+
64
+ it "returns a Mollie::SMS::Response object, with the Net::HTTP response" do
65
+ Net::HTTP.stubbed_response = Net::HTTPOK.new('1.1', '200', 'OK')
66
+ Net::HTTP.stubbed_response.stubs(:read_body).returns(SUCCESS_BODY)
67
+ response = @sms.deliver!
68
+ response.should.be.instance_of Mollie::SMS::Response
69
+ response.http_response.should == Net::HTTP.stubbed_response
70
+ end
71
+ end
data/spec/sms_spec.rb CHANGED
@@ -1,9 +1,5 @@
1
1
  require File.expand_path("../spec_helper", __FILE__)
2
- require "mollie/sms"
3
-
4
- Mollie::SMS.username = 'AstroRadio'
5
- Mollie::SMS.password = 'secret'
6
- Mollie::SMS.originator = 'Astro INC'
2
+ require "mollie/sms/test_helper"
7
3
 
8
4
  describe "Mollie::SMS" do
9
5
  it "holds the gateway uri" do
@@ -89,6 +85,16 @@ describe "A Mollie::SMS instance" do
89
85
  )
90
86
  @sms.params.should == params
91
87
  end
88
+
89
+ it "checks equality by comparing the params" do
90
+ @sms.should == Mollie::SMS.new('+31612345678', "The stars tell me you will have chicken noodle soup for breakfast.")
91
+ @sms.should.not == Mollie::SMS.new('+31612345678', "Different message")
92
+ @sms.should.not == Mollie::SMS.new('+31612345678', "The stars tell me you will have chicken noodle soup for breakfast.", 'originator' => 'Some being')
93
+ end
94
+
95
+ it "returns a string representation of itself" do
96
+ @sms.to_s.should == 'from: <Astro INC> to: <+31612345678> body: "The stars tell me you will have chicken noodle soup for breakfast."'
97
+ end
92
98
  end
93
99
 
94
100
  describe "When sending a Mollie::SMS message" do
@@ -98,46 +104,30 @@ describe "When sending a Mollie::SMS message" do
98
104
  @sms.body = "The stars tell me you will have chicken noodle soup for breakfast."
99
105
  end
100
106
 
101
- after do
102
- Net::HTTP.reset!
103
- end
104
-
105
- it "posts the post body to the gateway" do
106
- @sms.stubs(:params).returns('a key' => 'a value')
107
- @sms.stubs(:validate_params!)
108
- @sms.deliver
107
+ it "raises a Mollie::SMS::DeliveryFailure exception when sending, through the #deliver! method, fails" do
108
+ Mollie::SMS.gateway_failure!
109
109
 
110
- request, post = Net::HTTP.posted
111
- request.should.use_ssl
112
- request.host.should == Mollie::SMS::GATEWAY_URI.host
113
- request.port.should == Mollie::SMS::GATEWAY_URI.port
114
- post.path.should == Mollie::SMS::GATEWAY_URI.path
115
- post.body.should == "a%20key=a%20value"
116
- end
110
+ exception = nil
111
+ begin
112
+ @sms.deliver!
113
+ rescue Mollie::SMS::DeliveryFailure => exception
114
+ end
117
115
 
118
- it "returns a Mollie::SMS::Response object, with the Net::HTTP response" do
119
- Net::HTTP.stubbed_response = Net::HTTPOK.new('1.1', '200', 'OK')
120
- Net::HTTP.stubbed_response.stubs(:read_body).returns(SUCCESS_BODY)
121
- response = @sms.deliver
122
- response.should.be.instance_of Mollie::SMS::Response
123
- response.http_response.should == Net::HTTP.stubbed_response
116
+ exception.message.should == "(No username given.) #{@sms.to_s}"
124
117
  end
125
118
  end
126
119
 
127
120
  describe "A Mollie::SMS::Response instance, for a succeeded request" do
128
121
  before do
129
- @http_response = Net::HTTPOK.new('1.1', '200', 'OK')
130
- @http_response.stubs(:read_body).returns(SUCCESS_BODY)
131
- @http_response.add_field('Content-type', 'application/xml')
132
- @response = Mollie::SMS::Response.new(@http_response)
122
+ @response = Mollie::SMS.success!
133
123
  end
134
124
 
135
125
  it "returns the Net::HTTP response object" do
136
- @response.http_response.should == @http_response
126
+ @response.http_response.should.is_a?(Net::HTTPSuccess)
137
127
  end
138
128
 
139
129
  it "returns the response body as a hash" do
140
- @response.params.should == Hash.from_xml(SUCCESS_BODY)['response']['item']
130
+ @response.params.should == Hash.from_xml(Mollie::SMS::TestHelper::SUCCESS_BODY)['response']['item']
141
131
  end
142
132
 
143
133
  it "returns whether or not it was a success" do
@@ -158,10 +148,7 @@ end
158
148
 
159
149
  describe "A Mollie::SMS::Response instance, for a request that failed at the gateway" do
160
150
  before do
161
- @http_response = Net::HTTPOK.new('1.1', '200', 'OK')
162
- @http_response.stubs(:read_body).returns(FAILURE_BODY)
163
- @http_response.add_field('Content-type', 'application/xml')
164
- @response = Mollie::SMS::Response.new(@http_response)
151
+ @response = Mollie::SMS.gateway_failure!
165
152
  end
166
153
 
167
154
  it "returns that the request was not a success" do
@@ -183,8 +170,7 @@ end
183
170
 
184
171
  describe "A Mollie::SMS::Response instance, for a failed HTTP request" do
185
172
  before do
186
- @http_response = Net::HTTPBadRequest.new('1.1', '400', 'Bad request')
187
- @response = Mollie::SMS::Response.new(@http_response)
173
+ @response = Mollie::SMS.http_failure!
188
174
  end
189
175
 
190
176
  it "returns an empty hash as the params" do
data/spec/spec_helper.rb CHANGED
@@ -2,57 +2,11 @@ require "rubygems"
2
2
  require "bacon"
3
3
  require "mocha"
4
4
 
5
- $:.unshift File.expand_path("../../lib", __FILE__)
6
-
7
5
  Bacon.summary_on_exit
8
6
 
9
- module Net
10
- class HTTP
11
- class << self
12
- attr_accessor :posted, :stubbed_response
13
-
14
- def reset!
15
- @posted = nil
16
- @stubbed_response = nil
17
- end
18
- end
19
-
20
- def host
21
- @address
22
- end
23
-
24
- def start
25
- yield self
26
- end
27
-
28
- def request(request)
29
- self.class.posted = [self, request]
30
- self.class.stubbed_response
31
- end
32
- end
33
- end
34
-
35
- class ResponseStub
36
- end
37
-
38
- SUCCESS_BODY = %{
39
- <?xml version="1.0" ?>
40
- <response>
41
- <item type="sms">
42
- <recipients>1</recipients>
43
- <success>true</success>
44
- <resultcode>10</resultcode>
45
- <resultmessage>Message successfully sent.</resultmessage>
46
- </item>
47
- </response>}
7
+ $:.unshift File.expand_path("../../lib", __FILE__)
8
+ require "mollie/sms"
48
9
 
49
- FAILURE_BODY = %{
50
- <?xml version="1.0"?>
51
- <response>
52
- <item type="sms">
53
- <recipients>1</recipients>
54
- <success>false</success>
55
- <resultcode>20</resultcode>
56
- <resultmessage>No username given.</resultmessage>
57
- </item>
58
- </response>}
10
+ Mollie::SMS.username = 'AstroRadio'
11
+ Mollie::SMS.password = 'secret'
12
+ Mollie::SMS.originator = 'Astro INC'
@@ -0,0 +1,81 @@
1
+ require File.expand_path("../spec_helper", __FILE__)
2
+ require "mollie/sms/test_helper"
3
+
4
+ describe "Mollie::SMS ext" do
5
+ before do
6
+ Mollie::SMS.reset!
7
+ @sms = Mollie::SMS.new("+31612345678", "Future is coming.")
8
+ end
9
+
10
+ it "stubs any next delivery to be successful" do
11
+ Mollie::SMS.success!
12
+ @sms.deliver.should.be.success
13
+ end
14
+
15
+ it "stubs any next delivery to fail at the gateway" do
16
+ Mollie::SMS.gateway_failure!
17
+ response = @sms.deliver
18
+ response.should.not.be.success
19
+ response.should.not.be.http_failure
20
+ end
21
+
22
+ it "stubs any next delivery to fail at HTTP level" do
23
+ Mollie::SMS.http_failure!
24
+ response = @sms.deliver
25
+ response.should.not.be.success
26
+ response.should.be.http_failure
27
+ end
28
+
29
+ it "returns the stubbed response" do
30
+ @sms.deliver.should == Mollie::SMS.stubbed_response
31
+ @sms.deliver!.should == Mollie::SMS.stubbed_response
32
+ end
33
+
34
+ it "adds sms instance to a global array instead of actually delivering" do
35
+ @sms.deliver
36
+ @sms.deliver!
37
+ Mollie::SMS.deliveries.last(2).should == [@sms, @sms]
38
+ end
39
+
40
+ it "empties the deliveries array and stubs a succeeded response" do
41
+ @sms.deliver!
42
+ Mollie::SMS.reset!
43
+ Mollie::SMS.deliveries.should.be.empty
44
+ end
45
+ end
46
+
47
+ describe "Mollie::SMS::TestHelper" do
48
+ extend Mollie::SMS::TestHelper
49
+
50
+ before do
51
+ Mollie::SMS.reset!
52
+ @sms = Mollie::SMS.new("+31612345678", "Future is coming.")
53
+ @result = @message = nil
54
+ end
55
+
56
+ def assert(result, message)
57
+ @result, @message = result, message
58
+ end
59
+
60
+ it "asserts that an email is sent while executing the block" do
61
+ assert_sms_messages(2) { @sms.deliver; @sms.deliver }
62
+ @result.should == true
63
+ end
64
+
65
+ it "fails if not the given amount of messages are sent" do
66
+ assert_sms_messages(2) { @sms.deliver }
67
+ @result.should == false
68
+ @message.should == "expected `2' SMS messages to be sent, actually sent `1'"
69
+ end
70
+
71
+ it "asserts that no emails were sent while executing the block" do
72
+ assert_no_sms_messages { }
73
+ @result.should == true
74
+ end
75
+
76
+ it "fails if messages are actually sent" do
77
+ assert_no_sms_messages { @sms.deliver }
78
+ @result.should == false
79
+ @message.should == "expected `0' SMS messages to be sent, actually sent `1'"
80
+ end
81
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mollie-sms
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Eloy Duran
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-02 00:00:00 +02:00
18
+ date: 2010-08-03 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -47,9 +47,15 @@ extra_rdoc_files:
47
47
  files:
48
48
  - LICENSE
49
49
  - README
50
+ - Rakefile
51
+ - VERSION
50
52
  - lib/mollie/sms.rb
53
+ - lib/mollie/sms/test_helper.rb
54
+ - mollie-sms.gemspec
55
+ - spec/functional_sms_deliver_spec.rb
51
56
  - spec/sms_spec.rb
52
57
  - spec/spec_helper.rb
58
+ - spec/test_helper_spec.rb
53
59
  has_rdoc: true
54
60
  homepage: http://github.com/Fingertips/Mollie-SMS
55
61
  licenses: []
@@ -85,5 +91,7 @@ signing_key:
85
91
  specification_version: 3
86
92
  summary: Send SMS text messages via the Mollie.nl SMS gateway.
87
93
  test_files:
94
+ - spec/functional_sms_deliver_spec.rb
88
95
  - spec/sms_spec.rb
89
96
  - spec/spec_helper.rb
97
+ - spec/test_helper_spec.rb