exetel_sms 1.04 → 1.05
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/README +3 -3
- data/README.textile +4 -4
- data/exetel_sms.gemspec +1 -1
- data/lib/class_methods.rb +3 -0
- data/lib/client.rb +43 -19
- data/lib/sender.rb +7 -4
- data/test/test_all.rb +13 -0
- metadata +1 -1
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -48,7 +48,7 @@ Credit Check - also useful as a ping before sending
|
|
48
48
|
|
49
49
|
checker = ExetelSms::CreditCheck.new(config)
|
50
50
|
if result = checker.get_credit_limit
|
51
|
-
puts "Credit limit is #{result[:limit}
|
51
|
+
puts "Credit limit is #{result[:limit]}"
|
52
52
|
end
|
53
53
|
|
54
54
|
|
@@ -62,8 +62,8 @@ Test API - test responses without making actual HTTP calls
|
|
62
62
|
bad = ExetelSms::Client.with_test_api(:fail) { retriever.check_sent('1') }
|
63
63
|
bad[:message_status] # => 'Failed'
|
64
64
|
|
65
|
-
See test/ for more examples
|
66
|
-
|
65
|
+
Sent messages end up in ExetelSms::Client.sent_messages array. See test/ for more examples.
|
66
|
+
|
67
67
|
|
68
68
|
Contact the author
|
69
69
|
------------------
|
data/README.textile
CHANGED
@@ -44,20 +44,20 @@ h3. Credit Check: also useful as a ping before sending
|
|
44
44
|
|
45
45
|
bc. checker = ExetelSms::CreditCheck.new(config)
|
46
46
|
if result = checker.get_credit_limit
|
47
|
-
puts "Credit limit is #{result[:limit}
|
47
|
+
puts "Credit limit is #{result[:limit]}"
|
48
48
|
end
|
49
49
|
|
50
50
|
|
51
51
|
h3. Test API: test responses without making actual HTTP calls
|
52
52
|
|
53
53
|
bc. good = ExetelSms::Client.with_test_api { retriever.check_sent('1') }
|
54
|
-
|
54
|
+
good[:message_status] #=> 'Delivered'
|
55
55
|
|
56
56
|
bc. bad = ExetelSms::Client.with_test_api(:fail) { retriever.check_sent('1') }
|
57
|
-
|
57
|
+
bad[:message_status] #=> 'Failed'
|
58
58
|
|
59
|
+
Sent messages end up in ExetelSms::Client.sent_messages array. See test/ for more examples.
|
59
60
|
|
60
|
-
See test/ for more examples
|
61
61
|
|
62
62
|
|
63
63
|
h3. Contact the author
|
data/exetel_sms.gemspec
CHANGED
data/lib/class_methods.rb
CHANGED
@@ -30,6 +30,9 @@ module ExetelSms
|
|
30
30
|
URI.encode(URI.encode(str), /=|&|\?/)
|
31
31
|
end
|
32
32
|
|
33
|
+
def matchurl?(url)
|
34
|
+
url.match(/\/#{api_path}\?/)
|
35
|
+
end
|
33
36
|
|
34
37
|
def response_to_hash(fields)
|
35
38
|
raise "Missing fields in response body? Expected #{response_fields.map(&:to_s).join(',')}, got #{fields.inspect}" unless fields.length >= response_fields.length
|
data/lib/client.rb
CHANGED
@@ -7,7 +7,7 @@ require 'uri'
|
|
7
7
|
module ExetelSms
|
8
8
|
class Client
|
9
9
|
class << self
|
10
|
-
attr_accessor :test_api
|
10
|
+
attr_accessor :test_api, :sent_messages
|
11
11
|
|
12
12
|
def with_test_api(failmode=false)
|
13
13
|
old_test_api = @test_api
|
@@ -20,40 +20,64 @@ module ExetelSms
|
|
20
20
|
def test_api?
|
21
21
|
!@test_api.nil?
|
22
22
|
end
|
23
|
+
|
24
|
+
def parse_url_params(url)
|
25
|
+
Hash[
|
26
|
+
*url.split('?').last.split('&').map do |pair|
|
27
|
+
str1, str2 = pair.split('=')
|
28
|
+
[URI.decode(str1).to_sym, URI.decode(str2)]
|
29
|
+
end.flatten
|
30
|
+
]
|
31
|
+
end
|
23
32
|
|
24
33
|
def fake_response(url)
|
25
34
|
@counter = (@counter || 0) + 1
|
26
|
-
if
|
27
|
-
|
28
|
-
|
35
|
+
if Sender.matchurl?(url)
|
36
|
+
body = "1|0412345678|#{@counter}|#{@counter}|OK"
|
37
|
+
if test_api == false
|
38
|
+
@sent_messages << parse_url_params(url)
|
39
|
+
body
|
40
|
+
else
|
41
|
+
raise(Timeout::Error.new)
|
42
|
+
end
|
43
|
+
elsif Receiver.matchurl?(url)
|
29
44
|
test_api != false ? '2||||No results returned|' : "1|#{@counter}|0412345678|2011-03-29 23:15:03|OK|Test message"
|
30
|
-
elsif
|
45
|
+
elsif Retriever.matchurl?(url) && (referencenumber = url.match(/referencenumber=(.*)/))
|
31
46
|
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
|
47
|
+
elsif Deleter.matchurl?(url)
|
33
48
|
test_api != false ? '2|No results returned' : "1|OK"
|
34
|
-
elsif
|
49
|
+
elsif CreditCheck.matchurl?(url)
|
35
50
|
test_api != false ? '1|0|OK' : '1|100|OK'
|
36
51
|
else
|
37
|
-
raise "
|
38
|
-
end
|
52
|
+
raise "#{self.class} test API: Unknown URL"
|
53
|
+
end
|
39
54
|
end
|
40
55
|
|
41
56
|
def request(url)
|
42
|
-
return fake_response(url) if test_api?
|
43
|
-
|
44
|
-
uri = URI.parse(url)
|
45
|
-
h = Net::HTTP.new(uri.host, uri.port)
|
46
|
-
h.use_ssl = true
|
47
|
-
h.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
48
|
-
#h.set_debug_output $stderr
|
49
57
|
body = ''
|
50
|
-
|
51
|
-
|
52
|
-
|
58
|
+
if test_api?
|
59
|
+
body = fake_response(url)
|
60
|
+
else
|
61
|
+
uri = URI.parse(url)
|
62
|
+
h = Net::HTTP.new(uri.host, uri.port)
|
63
|
+
h.use_ssl = true
|
64
|
+
h.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
65
|
+
#h.set_debug_output $stderr
|
66
|
+
|
67
|
+
h.start do |http|
|
68
|
+
response = http.request_get(uri.request_uri)
|
69
|
+
body = response.body
|
70
|
+
end
|
53
71
|
end
|
72
|
+
|
54
73
|
raise "ExetelSms::Client: No valid body found: #{body.inspect}" unless body && body =~ /|/
|
74
|
+
request_to_array(body)
|
75
|
+
end
|
76
|
+
|
77
|
+
def request_to_array(body)
|
55
78
|
body.chomp.gsub(/<br>$/i, '').split('|',-1).map {|str| str.strip }
|
56
79
|
end
|
57
80
|
end
|
81
|
+
self.sent_messages ||= []
|
58
82
|
end
|
59
83
|
end
|
data/lib/sender.rb
CHANGED
@@ -9,8 +9,8 @@ module ExetelSms
|
|
9
9
|
@config = config
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
|
12
|
+
def send_request_hash(to_mobile_number, msg, from_mobile_number, reference_number)
|
13
|
+
{
|
14
14
|
:username => @config.username,
|
15
15
|
:password => @config.password,
|
16
16
|
:sender => from_mobile_number,
|
@@ -18,8 +18,11 @@ module ExetelSms
|
|
18
18
|
:messagetype => 'Text',
|
19
19
|
:referencenumber => reference_number,
|
20
20
|
:mobilenumber => to_mobile_number
|
21
|
-
|
22
|
-
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def send(to_mobile_number, msg, from_mobile_number, reference_number)
|
25
|
+
url = self.class.build_url(send_request_hash(to_mobile_number, msg, from_mobile_number, reference_number))
|
23
26
|
self.class.response_to_hash(ExetelSms::Client.request(url))
|
24
27
|
end
|
25
28
|
|
data/test/test_all.rb
CHANGED
@@ -29,8 +29,21 @@ class TestExetelSms < Test::Unit::TestCase
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def test_sender_ok
|
32
|
+
assert_equal 0, ExetelSms::Client.sent_messages.length
|
33
|
+
|
32
34
|
result = ExetelSms::Client.with_test_api { @sender.send('0412345678', 'Hello?', '0412987654', '1') }
|
33
35
|
assert result.success?
|
36
|
+
assert_equal 1, ExetelSms::Client.sent_messages.length
|
37
|
+
|
38
|
+
request = ExetelSms::Client.sent_messages.first
|
39
|
+
|
40
|
+
assert_equal '0412345678', request[:mobilenumber]
|
41
|
+
assert_equal '0412987654', request[:sender]
|
42
|
+
assert_equal '1', request[:referencenumber]
|
43
|
+
assert_equal 'Text', request[:messagetype]
|
44
|
+
assert_equal 'Hello?', request[:message]
|
45
|
+
assert_equal 'username', request[:username]
|
46
|
+
assert_equal 'password', request[:password]
|
34
47
|
end
|
35
48
|
|
36
49
|
def test_sender_exception
|