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 CHANGED
@@ -1,3 +1,5 @@
1
+ v1.05. Test mode collects sent messages in an array
2
+
1
3
  v1.04. Test API without making HTTP calls. Unit tests.
2
4
 
3
5
  v1.03. Support message retrieval (check sent status). Update hostname.
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
  ------------------
@@ -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
- good[:message_status] # => 'Delivered'
54
+ good[:message_status] #=> 'Delivered'
55
55
 
56
56
  bc. bad = ExetelSms::Client.with_test_api(:fail) { retriever.check_sent('1') }
57
- bad[:message_status] # => 'Failed'
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{exetel_sms}
5
- s.version = "1.04"
5
+ s.version = "1.05"
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"]
@@ -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
@@ -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 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}\?/)
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 url.match(/\/#{Retriever.api_path}\?/) && (referencenumber = url.match(/referencenumber=(.*)/))
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 url.match(/\/#{Deleter.api_path}\?/)
47
+ elsif Deleter.matchurl?(url)
33
48
  test_api != false ? '2|No results returned' : "1|OK"
34
- elsif url.match(/\/#{CreditCheck.api_path}\?/)
49
+ elsif CreditCheck.matchurl?(url)
35
50
  test_api != false ? '1|0|OK' : '1|100|OK'
36
51
  else
37
- raise "ExetelSMS test API: Unknown URL"
38
- end.split('|',-1).map {|str| str.strip }
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
- h.start do |http|
51
- response = http.request_get(uri.request_uri)
52
- body = response.body
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
@@ -9,8 +9,8 @@ module ExetelSms
9
9
  @config = config
10
10
  end
11
11
 
12
- def send(to_mobile_number, msg, from_mobile_number, reference_number)
13
- url = self.class.build_url(
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
 
@@ -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
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: exetel_sms
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "1.04"
5
+ version: "1.05"
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrew Snow