Class: ExetelSms::Client

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.sent_messagesObject

Returns the value of attribute sent_messages.



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

def sent_messages
  @sent_messages
end

.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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/client.rb', line 33

def fake_response(url)
  @counter = (@counter || 0) + 1
  if Sender.matchurl?(url)
    body = "1|0412345678|#{@counter}|#{@counter}|OK"
    if test_api == false
      @sent_messages << parse_url_params(url)
      body
    else
      raise(Timeout::Error.new)
    end
  elsif Receiver.matchurl?(url)
    test_api != false ? '2||||No results returned|' : "1|#{@counter}|0412345678|2011-03-29 23:15:03|OK|Test message"
  elsif Retriever.matchurl?(url) && (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 Deleter.matchurl?(url)
    test_api != false ? '2|No results returned' : "1|OK"
  elsif CreditCheck.matchurl?(url)
    test_api != false ? '1|0|OK' : '1|100|OK'
  else
    raise "#{self.class} test API: Unknown URL"
  end
end

.parse_url_params(url) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/client.rb', line 24

def parse_url_params(url)
  Hash[
    *url.split('?').last.split('&').map do |pair|
      str1, str2 = pair.split('=')
      [URI.decode(str1).to_sym, URI.decode(str2)]
    end.flatten
  ]
end

.request(url) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/client.rb', line 56

def request(url)
  body = ''
  if test_api?
    body = fake_response(url)
  else
    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
    
    h.start do |http|
      response = http.request_get(uri.request_uri)
      body = response.body
    end
  end
  
  raise "ExetelSms::Client: No valid body found: #{body.inspect}" unless body && body =~ /|/
  request_to_array(body)
end

.request_to_array(body) ⇒ Object



77
78
79
# File 'lib/client.rb', line 77

def request_to_array(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