Class: ExetelSms::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/client.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.test_apiObject

Returns the value of attribute test_api.



10
11
12
# File 'lib/client.rb', line 10

def test_api
  @test_api
end

Class Method Details

.fake_response(url) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/client.rb', line 24

def fake_response(url)
  @counter = (@counter || 0) + 1
  if url.match(/\/#{Sender.api_path}\?/)
    test_api != false ? raise(Timeout::Error.new) : "1|0412345678|#{@counter}|#{@counter}|OK"
  elsif url.match(/\/#{Receiver.api_path}\?/)
    test_api != false ? '2||||No results returned|' : "1|#{@counter}|0412345678|2011-03-29 23:15:03|OK|Test message"
  elsif url.match(/\/#{Retriever.api_path}\?/) && (referencenumber = url.match(/referencenumber=(.*)/))
    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"
  elsif url.match(/\/#{Deleter.api_path}\?/)
    test_api != false ? '2|No results returned' : "1|OK"
  elsif url.match(/\/#{CreditCheck.api_path}\?/)
    test_api != false ? '1|0|OK' : '1|100|OK'
  else
    raise "ExetelSMS test API: Unknown URL"
  end.split('|',-1).map {|str| str.strip }
end

.request(url) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/client.rb', line 41

def request(url)
  return fake_response(url) if test_api?

  uri = URI.parse(url)
  h = Net::HTTP.new(uri.host, uri.port)
  h.use_ssl = true
  h.verify_mode = OpenSSL::SSL::VERIFY_NONE
  #h.set_debug_output $stderr
  body = ''
  h.start do |http|
    response = http.request_get(uri.request_uri)
    body = response.body
  end
  raise "ExetelSms::Client: No valid body found: #{body.inspect}" unless body && body =~ /|/
  body.chomp.gsub(/<br>$/i, '').split('|',-1).map {|str| str.strip }
end

.test_api?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/client.rb', line 20

def test_api?
  !@test_api.nil?
end

.with_test_api(failmode = false) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/client.rb', line 12

def with_test_api(failmode=false)
  old_test_api = @test_api
  @test_api = failmode
  retval = yield
  @test_api = old_test_api
  retval
end