exetel_sms 1.03 → 1.04
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/Manifest +2 -0
- data/README +26 -0
- data/README.textile +23 -1
- data/exetel_sms.gemspec +6 -6
- data/lib/class_methods.rb +12 -6
- data/lib/client.rb +33 -1
- data/lib/config.rb +1 -0
- data/lib/credit_check.rb +35 -0
- data/lib/deleter.rb +3 -5
- data/lib/exetel_sms.rb +1 -1
- data/lib/receiver.rb +2 -2
- data/lib/retriever.rb +3 -3
- data/lib/sender.rb +3 -4
- data/test/test_all.rb +79 -0
- metadata +9 -14
data/CHANGELOG
CHANGED
data/Manifest
CHANGED
data/README
CHANGED
@@ -38,6 +38,31 @@ Receiving messages from a Virtual Mobile Number
|
|
38
38
|
if result = receiver.receive('my virtual mobile number')
|
39
39
|
puts "Received from #{result[:from_mobile_number]}: #{result[:message]}"
|
40
40
|
end
|
41
|
+
|
42
|
+
Then you can delete the messages with ExetelSms::Deleter
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
Credit Check - also useful as a ping before sending
|
47
|
+
---------------------------------------------------
|
48
|
+
|
49
|
+
checker = ExetelSms::CreditCheck.new(config)
|
50
|
+
if result = checker.get_credit_limit
|
51
|
+
puts "Credit limit is #{result[:limit}}"
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
Test API - test responses without making actual HTTP calls
|
57
|
+
----------------------------------------------------------
|
58
|
+
|
59
|
+
good = ExetelSms::Client.with_test_api { retriever.check_sent('1') }
|
60
|
+
good[:message_status] # => 'Delivered'
|
61
|
+
|
62
|
+
bad = ExetelSms::Client.with_test_api(:fail) { retriever.check_sent('1') }
|
63
|
+
bad[:message_status] # => 'Failed'
|
64
|
+
|
65
|
+
See test/ for more examples
|
41
66
|
|
42
67
|
|
43
68
|
Contact the author
|
@@ -45,4 +70,5 @@ Contact the author
|
|
45
70
|
|
46
71
|
Andrew Snow <andrew@modulus.org>
|
47
72
|
Andys^ on irc.freenode.net
|
73
|
+
@andy_snow on Twitter
|
48
74
|
|
data/README.textile
CHANGED
@@ -37,12 +37,34 @@ bc. receiver = ExetelSms::Receiver.new(config)
|
|
37
37
|
if result = receiver.receive('my virtual mobile number')
|
38
38
|
puts "Received from #{result[:from_mobile_number]}: #{result[:message]}"
|
39
39
|
end
|
40
|
-
|
41
40
|
|
41
|
+
Then you can delete the messages with ExetelSms::Deleter
|
42
|
+
|
43
|
+
h3. Credit Check: also useful as a ping before sending
|
44
|
+
|
45
|
+
bc. checker = ExetelSms::CreditCheck.new(config)
|
46
|
+
if result = checker.get_credit_limit
|
47
|
+
puts "Credit limit is #{result[:limit}}"
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
h3. Test API: test responses without making actual HTTP calls
|
52
|
+
|
53
|
+
bc. good = ExetelSms::Client.with_test_api { retriever.check_sent('1') }
|
54
|
+
good[:message_status] # => 'Delivered'
|
55
|
+
|
56
|
+
bc. bad = ExetelSms::Client.with_test_api(:fail) { retriever.check_sent('1') }
|
57
|
+
bad[:message_status] # => 'Failed'
|
58
|
+
|
59
|
+
|
60
|
+
See test/ for more examples
|
42
61
|
|
43
62
|
|
44
63
|
h3. Contact the author
|
45
64
|
|
46
65
|
Andrew Snow <andrew@modulus.org>
|
47
66
|
Andys^ on irc.freenode.net
|
67
|
+
@andy_snow on Twitter
|
68
|
+
|
69
|
+
|
48
70
|
|
data/exetel_sms.gemspec
CHANGED
@@ -2,24 +2,24 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{exetel_sms}
|
5
|
-
s.version = "1.
|
5
|
+
s.version = "1.04"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Andrew Snow"]
|
9
|
-
s.date = %q{2011-
|
9
|
+
s.date = %q{2011-03-30}
|
10
10
|
s.description = %q{Ruby gem to send and receive SMS via Exetel's API}
|
11
11
|
s.email = %q{andrew@modulus.org}
|
12
|
-
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "README.textile", "lib/class_methods.rb", "lib/client.rb", "lib/config.rb", "lib/deleter.rb", "lib/exetel_sms.rb", "lib/receiver.rb", "lib/retriever.rb", "lib/sender.rb"]
|
13
|
-
s.files = ["CHANGELOG", "LICENSE", "README", "README.textile", "Rakefile", "lib/class_methods.rb", "lib/client.rb", "lib/config.rb", "lib/deleter.rb", "lib/exetel_sms.rb", "lib/receiver.rb", "lib/retriever.rb", "lib/sender.rb", "Manifest", "exetel_sms.gemspec"]
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "LICENSE", "README", "README.textile", "lib/class_methods.rb", "lib/client.rb", "lib/config.rb", "lib/credit_check.rb", "lib/deleter.rb", "lib/exetel_sms.rb", "lib/receiver.rb", "lib/retriever.rb", "lib/sender.rb"]
|
13
|
+
s.files = ["CHANGELOG", "LICENSE", "README", "README.textile", "Rakefile", "lib/class_methods.rb", "lib/client.rb", "lib/config.rb", "lib/credit_check.rb", "lib/deleter.rb", "lib/exetel_sms.rb", "lib/receiver.rb", "lib/retriever.rb", "lib/sender.rb", "test/test_all.rb", "Manifest", "exetel_sms.gemspec"]
|
14
14
|
s.homepage = %q{}
|
15
15
|
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Exetel_sms", "--main", "README"]
|
16
16
|
s.require_paths = ["lib"]
|
17
17
|
s.rubyforge_project = %q{exetel_sms}
|
18
|
-
s.rubygems_version = %q{1.
|
18
|
+
s.rubygems_version = %q{1.6.2}
|
19
19
|
s.summary = %q{Ruby gem to send and receive SMS via Exetel's API}
|
20
|
+
s.test_files = ["test/test_all.rb"]
|
20
21
|
|
21
22
|
if s.respond_to? :specification_version then
|
22
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
23
|
s.specification_version = 3
|
24
24
|
|
25
25
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
data/lib/class_methods.rb
CHANGED
@@ -2,7 +2,17 @@
|
|
2
2
|
require 'uri'
|
3
3
|
|
4
4
|
module ExetelSms
|
5
|
+
module ResultHashMethods
|
6
|
+
def success?
|
7
|
+
self[:status] == '1'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
5
11
|
module ClassMethods
|
12
|
+
def exetel_url
|
13
|
+
"https://smsgw.exetel.com.au/sendsms/#{api_path}?"
|
14
|
+
end
|
15
|
+
|
6
16
|
def new_reference_number(ident='')
|
7
17
|
@@counter ||= 0
|
8
18
|
@@counter += 1
|
@@ -20,17 +30,13 @@ module ExetelSms
|
|
20
30
|
URI.encode(URI.encode(str), /=|&|\?/)
|
21
31
|
end
|
22
32
|
|
33
|
+
|
23
34
|
def response_to_hash(fields)
|
24
35
|
raise "Missing fields in response body? Expected #{response_fields.map(&:to_s).join(',')}, got #{fields.inspect}" unless fields.length >= response_fields.length
|
25
36
|
ret = {}
|
26
37
|
response_fields.each {|field| ret[field] = fields.shift }
|
27
38
|
ret[:other] = fields
|
28
|
-
|
29
|
-
def success?
|
30
|
-
self[:status] == '1'
|
31
|
-
end
|
32
|
-
end
|
33
|
-
ret
|
39
|
+
ret.extend ResultHashMethods
|
34
40
|
end
|
35
41
|
|
36
42
|
end
|
data/lib/client.rb
CHANGED
@@ -7,8 +7,40 @@ require 'uri'
|
|
7
7
|
module ExetelSms
|
8
8
|
class Client
|
9
9
|
class << self
|
10
|
+
attr_accessor :test_api
|
11
|
+
|
12
|
+
def with_test_api(failmode=false)
|
13
|
+
old_test_api = @test_api
|
14
|
+
@test_api = failmode
|
15
|
+
retval = yield
|
16
|
+
@test_api = old_test_api
|
17
|
+
retval
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_api?
|
21
|
+
!@test_api.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
def fake_response(url)
|
25
|
+
@counter = (@counter || 0) + 1
|
26
|
+
if url.match(/\/#{Sender.api_path}\?/)
|
27
|
+
test_api != false ? raise(Timeout::Error.new) : "1|0412345678|#{@counter}|#{@counter}|OK"
|
28
|
+
elsif url.match(/\/#{Receiver.api_path}\?/)
|
29
|
+
test_api != false ? '2||||No results returned|' : "1|#{@counter}|0412345678|2011-03-29 23:15:03|OK|Test message"
|
30
|
+
elsif url.match(/\/#{Retriever.api_path}\?/) && (referencenumber = url.match(/referencenumber=(.*)/))
|
31
|
+
test_api != false ? "1|#{@counter}|#{referencenumber}|0412987654|0412345678||Failed|0.05|OK" : "1|#{@counter}|#{referencenumber}|0412987654|0412345678|2011-03-29 23:15:03|Delivered|0.05|OK"
|
32
|
+
elsif url.match(/\/#{Deleter.api_path}\?/)
|
33
|
+
test_api != false ? '2|No results returned' : "1|OK"
|
34
|
+
elsif url.match(/\/#{CreditCheck.api_path}\?/)
|
35
|
+
test_api != false ? '1|0|OK' : '1|100|OK'
|
36
|
+
else
|
37
|
+
raise "ExetelSMS test API: Unknown URL"
|
38
|
+
end.split('|',-1).map {|str| str.strip }
|
39
|
+
end
|
10
40
|
|
11
41
|
def request(url)
|
42
|
+
return fake_response(url) if test_api?
|
43
|
+
|
12
44
|
uri = URI.parse(url)
|
13
45
|
h = Net::HTTP.new(uri.host, uri.port)
|
14
46
|
h.use_ssl = true
|
@@ -19,7 +51,7 @@ module ExetelSms
|
|
19
51
|
response = http.request_get(uri.request_uri)
|
20
52
|
body = response.body
|
21
53
|
end
|
22
|
-
raise "No body" unless body && body =~ /|/
|
54
|
+
raise "ExetelSms::Client: No valid body found: #{body.inspect}" unless body && body =~ /|/
|
23
55
|
body.chomp.gsub(/<br>$/i, '').split('|',-1).map {|str| str.strip }
|
24
56
|
end
|
25
57
|
end
|
data/lib/config.rb
CHANGED
data/lib/credit_check.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
|
2
|
+
module ExetelSms
|
3
|
+
|
4
|
+
class CreditCheck
|
5
|
+
extend ExetelSms::ClassMethods
|
6
|
+
|
7
|
+
attr_reader :config
|
8
|
+
def initialize(config)
|
9
|
+
@config = config
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_credit_limit
|
13
|
+
url = self.class.build_url(
|
14
|
+
:username => @config.username,
|
15
|
+
:password => @config.password
|
16
|
+
)
|
17
|
+
|
18
|
+
self.class.response_to_hash(ExetelSms::Client.request(url))
|
19
|
+
end
|
20
|
+
|
21
|
+
class << self
|
22
|
+
def response_fields
|
23
|
+
[:status, :limit, :notes]
|
24
|
+
end
|
25
|
+
|
26
|
+
def request_fields
|
27
|
+
[:username, :password]
|
28
|
+
end
|
29
|
+
|
30
|
+
def api_path
|
31
|
+
'api_sms_credit.php'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/deleter.rb
CHANGED
@@ -17,11 +17,10 @@ module ExetelSms
|
|
17
17
|
:messageid => message_id
|
18
18
|
)
|
19
19
|
|
20
|
-
response_to_hash(ExetelSms::Client.request(url))
|
20
|
+
self.class.response_to_hash(ExetelSms::Client.request(url))
|
21
21
|
end
|
22
22
|
|
23
23
|
class << self
|
24
|
-
|
25
24
|
def response_fields
|
26
25
|
[:status, :notes]
|
27
26
|
end
|
@@ -30,10 +29,9 @@ module ExetelSms
|
|
30
29
|
[:username, :password, :mobilenumber, :messageid]
|
31
30
|
end
|
32
31
|
|
33
|
-
def
|
34
|
-
'
|
32
|
+
def api_path
|
33
|
+
'api_sms_mvn_delete.php'
|
35
34
|
end
|
36
|
-
|
37
35
|
end
|
38
36
|
end
|
39
37
|
end
|
data/lib/exetel_sms.rb
CHANGED
data/lib/receiver.rb
CHANGED
data/lib/retriever.rb
CHANGED
@@ -8,7 +8,7 @@ module ExetelSms
|
|
8
8
|
def initialize(config)
|
9
9
|
@config = config
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def check_sent(reference_number)
|
13
13
|
url = self.class.build_url(
|
14
14
|
:username => @config.username,
|
@@ -29,8 +29,8 @@ module ExetelSms
|
|
29
29
|
[:username, :password, :referencenumber]
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
33
|
-
'
|
32
|
+
def api_path
|
33
|
+
'api_sms_detail.php'
|
34
34
|
end
|
35
35
|
|
36
36
|
end
|
data/lib/sender.rb
CHANGED
@@ -8,7 +8,7 @@ module ExetelSms
|
|
8
8
|
def initialize(config)
|
9
9
|
@config = config
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def send(to_mobile_number, msg, from_mobile_number, reference_number)
|
13
13
|
url = self.class.build_url(
|
14
14
|
:username => @config.username,
|
@@ -33,9 +33,8 @@ module ExetelSms
|
|
33
33
|
[:username, :password, :mobilenumber, :message, :sender, :messagetype, :referencenumber]
|
34
34
|
end
|
35
35
|
|
36
|
-
def
|
37
|
-
'
|
38
|
-
#username=xxxxxxxx&password=xxxxxxxx&mobilenumber=xxxxxxxx&message=xxxxxxxx&sender=xxxxxxxxx&messagetype=Text&referencenumber=xxxxxx'
|
36
|
+
def api_path
|
37
|
+
'api_sms.php'
|
39
38
|
end
|
40
39
|
|
41
40
|
end
|
data/test/test_all.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
|
2
|
+
require 'test/unit'
|
3
|
+
require "./#{File.dirname(__FILE__)}/../lib/exetel_sms.rb"
|
4
|
+
|
5
|
+
class TestExetelSms < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
config = ExetelSms::Config.new('username', 'password')
|
9
|
+
@sender = ExetelSms::Sender.new(config)
|
10
|
+
@retriever = ExetelSms::Retriever.new(config)
|
11
|
+
@receiver = ExetelSms::Receiver.new(config)
|
12
|
+
@deleter = ExetelSms::Deleter.new(config)
|
13
|
+
@credit_check = ExetelSms::CreditCheck.new(config)
|
14
|
+
end
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_credit_check_ok
|
20
|
+
result = ExetelSms::Client.with_test_api { @credit_check.get_credit_limit }
|
21
|
+
assert result.success?
|
22
|
+
assert result[:limit].to_f > 0
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_credit_check_fail
|
26
|
+
result = ExetelSms::Client.with_test_api(:fail) { @credit_check.get_credit_limit }
|
27
|
+
assert result.success?
|
28
|
+
assert_equal 0, result[:limit].to_f
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_sender_ok
|
32
|
+
result = ExetelSms::Client.with_test_api { @sender.send('0412345678', 'Hello?', '0412987654', '1') }
|
33
|
+
assert result.success?
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_sender_exception
|
37
|
+
assert_raises(Timeout::Error) do
|
38
|
+
ExetelSms::Client.with_test_api(:fail) { @sender.send('0412345678', 'Hello?', '0412987654', '1') }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_receiver_ok
|
43
|
+
result = ExetelSms::Client.with_test_api { @receiver.receive('0412345678') }
|
44
|
+
assert result.success?
|
45
|
+
assert_not_equal '', result[:message]
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_receiver_fail
|
49
|
+
result = ExetelSms::Client.with_test_api(:fail) { @receiver.receive('0412345678') }
|
50
|
+
assert !result.success?
|
51
|
+
assert_equal '', result[:message]
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_retriever_ok
|
55
|
+
result = ExetelSms::Client.with_test_api { @retriever.check_sent('1') }
|
56
|
+
assert result.success?
|
57
|
+
assert_equal 'Delivered', result[:message_status]
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_retriever_fail
|
61
|
+
result = ExetelSms::Client.with_test_api(:fail) { @retriever.check_sent('1') }
|
62
|
+
assert result.success?
|
63
|
+
assert_equal 'Failed', result[:message_status]
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_deleter_ok
|
67
|
+
result = ExetelSms::Client.with_test_api { @deleter.delete('0412345678', '1') }
|
68
|
+
assert result.success?
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_deleter_fail
|
72
|
+
result = ExetelSms::Client.with_test_api(:fail) { @deleter.delete('0412345678', '1') }
|
73
|
+
assert !result.success?
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
metadata
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: exetel_sms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 3
|
8
|
-
version: "1.03"
|
4
|
+
prerelease:
|
5
|
+
version: "1.04"
|
9
6
|
platform: ruby
|
10
7
|
authors:
|
11
8
|
- Andrew Snow
|
@@ -13,7 +10,7 @@ autorequire:
|
|
13
10
|
bindir: bin
|
14
11
|
cert_chain: []
|
15
12
|
|
16
|
-
date: 2011-
|
13
|
+
date: 2011-03-30 00:00:00 +11:00
|
17
14
|
default_executable:
|
18
15
|
dependencies: []
|
19
16
|
|
@@ -31,6 +28,7 @@ extra_rdoc_files:
|
|
31
28
|
- lib/class_methods.rb
|
32
29
|
- lib/client.rb
|
33
30
|
- lib/config.rb
|
31
|
+
- lib/credit_check.rb
|
34
32
|
- lib/deleter.rb
|
35
33
|
- lib/exetel_sms.rb
|
36
34
|
- lib/receiver.rb
|
@@ -45,11 +43,13 @@ files:
|
|
45
43
|
- lib/class_methods.rb
|
46
44
|
- lib/client.rb
|
47
45
|
- lib/config.rb
|
46
|
+
- lib/credit_check.rb
|
48
47
|
- lib/deleter.rb
|
49
48
|
- lib/exetel_sms.rb
|
50
49
|
- lib/receiver.rb
|
51
50
|
- lib/retriever.rb
|
52
51
|
- lib/sender.rb
|
52
|
+
- test/test_all.rb
|
53
53
|
- Manifest
|
54
54
|
- exetel_sms.gemspec
|
55
55
|
has_rdoc: true
|
@@ -71,24 +71,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
71
71
|
requirements:
|
72
72
|
- - ">="
|
73
73
|
- !ruby/object:Gem::Version
|
74
|
-
segments:
|
75
|
-
- 0
|
76
74
|
version: "0"
|
77
75
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
76
|
none: false
|
79
77
|
requirements:
|
80
78
|
- - ">="
|
81
79
|
- !ruby/object:Gem::Version
|
82
|
-
segments:
|
83
|
-
- 1
|
84
|
-
- 2
|
85
80
|
version: "1.2"
|
86
81
|
requirements: []
|
87
82
|
|
88
83
|
rubyforge_project: exetel_sms
|
89
|
-
rubygems_version: 1.
|
84
|
+
rubygems_version: 1.6.2
|
90
85
|
signing_key:
|
91
86
|
specification_version: 3
|
92
87
|
summary: Ruby gem to send and receive SMS via Exetel's API
|
93
|
-
test_files:
|
94
|
-
|
88
|
+
test_files:
|
89
|
+
- test/test_all.rb
|