kapow 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ == 0.4.0 2009-10-21
2
+
3
+ * Added additional options to the deliver sms method
4
+ * More specs
5
+
6
+ == 0.3.0 2008-08-12
7
+
8
+ * Renamed Message class to SMS, as this identifies the class better
9
+
10
+ == 0.2.0 2008-08-05
11
+
12
+ * Fixed typo in :long_sms option
13
+ * Added ability to send sms as flash message
14
+ * Added a few more specs
15
+
16
+ == 0.1.2 2008-07-29
17
+
18
+ * Improved rdoc
19
+
20
+ == 0.1.1 2008-07-28
21
+
22
+ * Updated gemspec as gem was not building on Github
23
+
24
+ == 0.1.0 2008-07-27
25
+
26
+ * Initial release.
@@ -0,0 +1,49 @@
1
+ %w(rubygems rake rake/rdoctask fileutils).each { |lib| require lib }
2
+
3
+ begin
4
+ require 'spec/rake/spectask'
5
+ rescue LoadError
6
+ puts 'To use rspec for testing you must install rspec gem:'
7
+ puts '$ sudo gem install rspec'
8
+ exit
9
+ end
10
+
11
+ include FileUtils
12
+ require File.join(File.dirname(__FILE__), 'lib', 'kapow', 'version')
13
+
14
+ AUTHOR = 'Kiean Johnson'
15
+ EMAIL = "kieran[AT]invisiblelines.com"
16
+ DESCRIPTION = "Ruby library for Kapow SMS gateway service."
17
+ GEM_NAME = 'kapow'
18
+
19
+ Rake::RDocTask.new('docs') do |rd|
20
+ rd.main = 'README.txt'
21
+ rd.rdoc_files.include('README', 'History.txt', 'License.txt', 'lib/**/*.rb')
22
+ rd.rdoc_dir = 'doc'
23
+ rd.options << '--tab-width=2'
24
+ rd.options << '--inline-source'
25
+ rd.options << '--line-numbers'
26
+ end
27
+
28
+ namespace :spec do
29
+ desc "Run the specs under spec"
30
+ Spec::Rake::SpecTask.new('all') do |t|
31
+ t.spec_opts = ['--options', "spec/spec.opts"]
32
+ t.spec_files = FileList['spec/*_spec.rb']
33
+ end
34
+
35
+ desc "Run the specs under spec in specdoc format"
36
+ Spec::Rake::SpecTask.new('doc') do |t|
37
+ t.spec_opts = ['--format', "specdoc"]
38
+ t.spec_files = FileList['spec/*_spec.rb']
39
+ end
40
+
41
+ desc "Run the specs in HTML format"
42
+ Spec::Rake::SpecTask.new('html') do |t|
43
+ t.spec_files = FileList['spec/*_spec.rb']
44
+ t.spec_opts = ['--format', "html:website/specs.html"]
45
+ end
46
+ end
47
+
48
+ desc "Default task is to run specs"
49
+ task :default => 'spec:all'
@@ -0,0 +1,47 @@
1
+ = Read Me
2
+
3
+ Kapow - Ruby library for Kapow SMS gateway service
4
+
5
+ by Kieran Johnson
6
+ http://github.com/kieranj/kapow
7
+
8
+ == Installation:
9
+
10
+ $ gem sources -a http://gems.github.com/ (you only need to do this once)
11
+ $ gem install kieranj-kapow
12
+
13
+ == Usage:
14
+
15
+ To use this library you first need to sign up for an account at www.kapow.com.
16
+
17
+ The library wraps the HTTP POST service provided as this has the advantages of
18
+ speed and an immediate response message.
19
+
20
+ require 'rubygems'
21
+ require 'kieranj-kapow'
22
+
23
+ # Create a new sms
24
+ msg = Kapow::SMS.new('username', 'password')
25
+ msg.deliver('mobile_no', 'sms')
26
+
27
+ All options supported by Kapow can be passed into the deliver method as a hash
28
+
29
+ # Send sms as a flash
30
+ msg.deliver('mobile_no', 'sms', :flash => true)
31
+
32
+ # Send sms with from_id (if enabled in account)
33
+ msg.deliver('mobile_no', 'sms', :from_id => 'from_id')
34
+
35
+ # Send sms up to 1377 characters long
36
+ msg.deliver('mobile_no', 'sms', :long_sms => true)
37
+
38
+ Responses provided by the Kapow SMS Gateway are:
39
+
40
+ <tt>OK</tt>:: Message has been accepted for delivery.
41
+ <tt>USERPASS</tt>:: Invalid username or password.
42
+ <tt>NOCREDIT</tt>:: Account has no credits or credit limit has been reached.
43
+ <tt>ERROR</tt>:: Any other error has occurred.
44
+
45
+ The amount of remaining credit is included with the 'OK' response from the gateway. This is available as:
46
+
47
+ Kapow::Credit #=> 325
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "kapow"
3
+ s.version = "0.4.0"
4
+ s.date = "2009-10-21"
5
+ s.summary = "Ruby library for Kapow SMS gateway service"
6
+ s.email = "kieran[AT]invisiblelines.com"
7
+ s.homepage = "http://gemcutter.org/gems/kapow"
8
+ s.rubyforge_project = "kapow"
9
+ s.description = "Ruby library for sending SMS messages using the Kapow SMS gateway service."
10
+ s.has_rdoc = true
11
+ s.authors = ["Kieran Johnson"]
12
+ s.files = ["History.txt",
13
+ "Readme.rdoc",
14
+ "Rakefile",
15
+ "kapow.gemspec",
16
+ "lib/kapow.rb",
17
+ "lib/kapow/sms.rb",
18
+ "lib/kapow/response.rb",
19
+ "lib/kapow/credit.rb",
20
+ "lib/kapow/version.rb"]
21
+ s.test_files = ["spec/sms_spec.rb", "spec/response_spec.rb"]
22
+ s.rdoc_options = ["--main", "Readme.rdoc"]
23
+ s.extra_rdoc_files = ["Readme.rdoc"]
24
+ end
@@ -0,0 +1,22 @@
1
+ # = Overview:
2
+ # A simple library for sending sms messages using the Kapow sms gateway.
3
+ # ---
4
+ # = License:
5
+ # Author:: Kieran Johnson
6
+ # Copyright:: July, 2008
7
+ # License:: Ruby License
8
+ # ---
9
+ # = Usage:
10
+ # require 'rubygems'
11
+ # require 'kieranj-kapow'
12
+ #
13
+ # msg = Kapow::SMS.new('username', 'password')
14
+ # msg.deliver('mobile_no', 'sms')
15
+
16
+ module Kapow
17
+
18
+ %w(version response sms credit).each do |lib|
19
+ require File.join(File.dirname(__FILE__), 'kapow', lib)
20
+ end
21
+
22
+ end
@@ -0,0 +1,21 @@
1
+ module Kapow
2
+
3
+ # Module to access the amount of available credit.
4
+
5
+ module Credit
6
+
7
+ class << self
8
+
9
+ attr_accessor :available
10
+
11
+ # Returns the available credit
12
+ #
13
+ def to_s
14
+ available.to_s
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,43 @@
1
+ module Kapow
2
+
3
+ class NoCreditError < StandardError; end
4
+ class AuthenticationError < StandardError; end
5
+ class Error < StandardError; end
6
+
7
+ # A small class for parsing the response from the sms gateway.
8
+
9
+ class Response
10
+
11
+ def initialize(sms, message)
12
+ @sms = sms
13
+ @message = parse(message)
14
+ end
15
+
16
+ attr_reader :message
17
+
18
+ # Parses the response body. Raises the appropriate error or returns true.
19
+ # Also updates the amount of available credit.
20
+ #
21
+ def parse(message)
22
+ if message.is_a?(Net::HTTPSuccess || Net::HTTPRedirection)
23
+ case message.body
24
+ when "USERPASS"
25
+ raise AuthenticationError
26
+ when "NOCREDIT"
27
+ raise NoCreditError
28
+ when "ERROR"
29
+ raise Error
30
+ when /STATUS (.*)/ # This needs to change
31
+ $1
32
+ when /^OK (\d+)\s?(.*)?/
33
+ Kapow::Credit.available = $1.to_i
34
+ @sms.unique_id = $2 unless $2 == ""
35
+ end
36
+ else
37
+ raise message.error!
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,74 @@
1
+ %w(uri net/http).each { |lib| require lib }
2
+
3
+ module Kapow
4
+
5
+ # A class for creating and sending an sms message using the Kapow sms gateway.
6
+
7
+ class SMS
8
+
9
+ MESSAGE_URL = "https://www.kapow.co.uk/scripts/sendsms.php"
10
+
11
+ STATUS_URL = "https://www.kapow.co.uk/scripts/chk_status.php"
12
+
13
+ VALID_OPTIONS = [ :flash, :from_id, :long_sms, :returnid, :route, :url ]
14
+
15
+ attr_accessor :unique_id
16
+
17
+ # Required values are: username, password
18
+ # <tt>:username</tt>:: Your account username.
19
+ # <tt>:password</tt>:: Your account password.
20
+ #
21
+ def initialize(username, password)
22
+ @username = username
23
+ @password = password
24
+ end
25
+
26
+ # Delivers the message
27
+ # <tt>:mobile</tt>:: Recipient number, or list of comma separated numbers.
28
+ # <tt>:sms</tt>:: Text for the message itself, truncated by gateway at 160 characters
29
+ #
30
+ # Optional parameters passed in as a hash
31
+ # <tt>:flash</tt>:: Boolean - Send SMS as a flash
32
+ # <tt>:from_id</tt>:: The message originator (if enabled for your account)
33
+ # <tt>:long_sms</tt>:: Boolean (if enabled in your account)
34
+ # <tt>:returnid</tt>:: Boolean - Returns unique id in response, so status can be tracked
35
+ # <tt>:route</tt>:: Your shortcode - Enables premium sms messages (reverse billed)
36
+ # <tt>:url</tt>:: URL to call after sending the request
37
+ #
38
+ def deliver(mobile, sms, options={})
39
+ options = options.reject { |k,v| !VALID_OPTIONS.include?(k) }
40
+
41
+ sms = "FLASH#{sms}" if options.include?(:flash) && options[:flash] == true
42
+
43
+ options[:returnid] = "TRUE" if options.include?(:returnid) && options[:returnid] == true
44
+
45
+ sms_parameters = {
46
+ :username => @username,
47
+ :password => @password,
48
+ :mobile => mobile,
49
+ :sms => sms
50
+ }.merge(options)
51
+
52
+ response = Net::HTTP.post_form(URI.parse(MESSAGE_URL), sms_parameters)
53
+ Response.new(self, response).message
54
+ end
55
+
56
+ # Returns the delivery status of a message
57
+ # <tt>:returnid</tt>:: The unique id received when sending a message with returnid=TRUE
58
+ #
59
+ def status(returnid)
60
+ response = Net::HTTP.post_form(URI.parse(STATUS_URL), { :username => @username, :returnid => returnid } )
61
+ Response.new(self, response).message
62
+ end
63
+
64
+ protected
65
+
66
+ def format_number(number)
67
+ number.sub!(/^\+/, "")
68
+ number.sub!(/^00/, "")
69
+ number
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,16 @@
1
+ module Kapow
2
+
3
+ module VERSION
4
+ MAJOR = 0
5
+ MINOR = 4
6
+ TINY = 0
7
+
8
+ STRING = [MAJOR, MINOR, TINY].join('.')
9
+
10
+ def self.to_s
11
+ STRING
12
+ end
13
+
14
+ end
15
+
16
+ end
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Kapow::Response do
4
+
5
+ before do
6
+ @sms = Kapow::SMS.new("kieran", "secret")
7
+ @response_text = Net::HTTPSuccess.new('1.1', '200', 'OK')
8
+ end
9
+
10
+ it "should raise Kapow::Authentication error if response is USERPASS" do
11
+ @response_text.stubs(:body).returns("USERPASS")
12
+ lambda { Kapow::Response.new(@sms, @response_text) }.should raise_error(Kapow::AuthenticationError)
13
+ end
14
+
15
+ it "should raise Kapow::NoCreditError error if response is NOCREDIT" do
16
+ @response_text.stubs(:body).returns("NOCREDIT")
17
+ lambda { Kapow::Response.new(@sms, @response_text) }.should raise_error(Kapow::NoCreditError)
18
+ end
19
+
20
+ it "should raise Kapow::Error error if response is ERROR" do
21
+ @response_text.stubs(:body).returns("ERROR")
22
+ lambda { Kapow::Response.new(@sms, @response_text) }.should raise_error(Kapow::Error)
23
+ end
24
+
25
+ it "should return true if response is OK" do
26
+ @response_text.stubs(:body).returns("OK CREDITS")
27
+ lambda { Kapow::Response.new(@sms, @response_text) }.should_not raise_error
28
+ end
29
+
30
+ it "should return the amount of credit available if response is OK" do
31
+ @response_text.stubs(:body).returns("OK 111")
32
+ Kapow::Response.new(@sms, @response_text)
33
+ Kapow::Credit.to_s.should == "111"
34
+ end
35
+
36
+ it "should raise Kapow::Error if response is not success or redirection" do
37
+ error = Net::HTTPServerError.new("1.1", "500", "Error")
38
+ lambda { Kapow::Response.new(@sms, error) }.should raise_error
39
+ end
40
+
41
+ # # response with unique_id
42
+ it "should return the unique_id if available" do
43
+ @response_text.stubs(:body).returns("OK 111 UNIQUEID")
44
+ Kapow::Response.new(@sms, @response_text)
45
+ @sms.unique_id.should == "UNIQUEID"
46
+ end
47
+
48
+ # response for status
49
+ it "should return the status of the message" do
50
+ @response_text.stubs(:body).returns("STATUS SENT")
51
+ Kapow::Response.new(@sms, @response_text).message.should == "SENT"
52
+ end
53
+
54
+ end
@@ -0,0 +1,91 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Kapow::SMS do
4
+
5
+ before do
6
+ @sms = Kapow::SMS.new("kieran", "secret")
7
+ end
8
+
9
+ describe "posting a message" do
10
+
11
+ before do
12
+ @response = Net::HTTPSuccess.new("1.1", "200", "OK")
13
+ @response.stubs(:body).returns("OK")
14
+ Net::HTTP.stubs(:post_form).returns(@response)
15
+ @uri = URI.parse(Kapow::SMS::MESSAGE_URL)
16
+ end
17
+
18
+ it "should send an sms" do
19
+ Net::HTTP.expects(:post_form).with(@uri, {:username => "kieran", :mobile => "4401234567890", :password => "secret", :sms => "Yo!"}).returns(@response)
20
+ lambda { @sms.deliver("4401234567890", "Yo!") }.should_not raise_error
21
+ end
22
+
23
+ context "with flash" do
24
+
25
+ it "should send an sms as a flash" do
26
+ Net::HTTP.expects(:post_form).with(@uri, {:flash => true, :username => "kieran", :mobile => "4401234567890", :password => "secret", :sms => "FLASHYo!"}).returns(@response)
27
+ lambda { @sms.deliver("4401234567890", "Yo!", :flash => true) }.should_not raise_error
28
+ end
29
+
30
+ end
31
+
32
+ context "with from_id" do
33
+
34
+ it "should send an sms with a from_id" do
35
+ Net::HTTP.expects(:post_form).with(@uri, {:from_id => "Kieran_j", :username => "kieran", :mobile => "4401234567890", :password => "secret", :sms => "Yo!"}).returns(@response)
36
+ lambda { @sms.deliver("4401234567890", "Yo!", :from_id => "Kieran_j") }.should_not raise_error
37
+ end
38
+
39
+ end
40
+
41
+ context "with long_sms" do
42
+
43
+ it "should send a long sms" do
44
+ msg = ""
45
+ 4.times { msg << "The quick brown fox jumps over the lazy dog. " }
46
+ Net::HTTP.expects(:post_form).with(@uri, {:long_sms => true, :username => "kieran", :mobile => "4401234567890", :password => "secret", :sms => msg}).returns(@response)
47
+ lambda { @sms.deliver("4401234567890", msg, :long_sms => true) }.should_not raise_error
48
+ end
49
+
50
+ end
51
+
52
+ context "with returnid=TRUE" do
53
+
54
+ it "should send an sms with returnid=TRUE" do
55
+ Net::HTTP.expects(:post_form).with(@uri, {:returnid => "TRUE", :mobile => "4401234567890", :username => "kieran", :sms => "Yo!", :password => "secret"}).returns(@response)
56
+ lambda { @sms.deliver("4401234567890", "Yo!", :returnid => true) }.should_not raise_error
57
+ end
58
+
59
+ end
60
+
61
+ context "with URL" do
62
+
63
+ it "should send an sms with a callback URL" do
64
+ Net::HTTP.expects(:post_form).with(@uri, {:url => "http://my.callback.com", :mobile => "4401234567890", :username => "kieran", :sms => "Yo!", :password => "secret"}).returns(@response)
65
+ lambda { @sms.deliver("4401234567890", "Yo!", :url => "http://my.callback.com") }.should_not raise_error
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ describe "returning a message status" do
73
+
74
+ #
75
+ # The actual response needs to be inserted here.
76
+ #
77
+ before do
78
+ @response = Net::HTTPSuccess.new("1.1", "200", "OK")
79
+ @response.stubs(:body).returns("STATUS SENT") # status body response
80
+ Net::HTTP.stubs(:post_form).returns(@response)
81
+ @uri = URI.parse(Kapow::SMS::STATUS_URL)
82
+ end
83
+
84
+ it "should return the status" do
85
+ Net::HTTP.expects(:post_form).with(@uri, {:username => "kieran", :returnid => "12345"}).returns(@response)
86
+ @sms.status("12345").should be_instance_of(String)
87
+ end
88
+
89
+ end
90
+
91
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kapow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Kieran Johnson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-21 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Ruby library for sending SMS messages using the Kapow SMS gateway service.
17
+ email: kieran[AT]invisiblelines.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - Readme.rdoc
24
+ files:
25
+ - History.txt
26
+ - Readme.rdoc
27
+ - Rakefile
28
+ - kapow.gemspec
29
+ - lib/kapow.rb
30
+ - lib/kapow/sms.rb
31
+ - lib/kapow/response.rb
32
+ - lib/kapow/credit.rb
33
+ - lib/kapow/version.rb
34
+ has_rdoc: true
35
+ homepage: http://gemcutter.org/gems/kapow
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --main
41
+ - Readme.rdoc
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project: kapow
59
+ rubygems_version: 1.3.5
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Ruby library for Kapow SMS gateway service
63
+ test_files:
64
+ - spec/sms_spec.rb
65
+ - spec/response_spec.rb