Class: Plivo::BaseClient

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

Direct Known Subclasses

Phlo, RestClient

Constant Summary collapse

@@voice_retry_count =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth_id = nil, auth_token = nil, proxy_options = nil, timeout = 5) ⇒ BaseClient

Returns a new instance of BaseClient.



16
17
18
19
20
21
22
# File 'lib/plivo/base_client.rb', line 16

def initialize(auth_id = nil, auth_token = nil, proxy_options = nil, timeout=5)
  configure_credentials(auth_id, auth_token)
  configure_proxies(proxy_options)
  configure_timeout(timeout)
  configure_headers
  configure_connection
end

Instance Attribute Details

#auth_credentialsObject (readonly)

Base stuff



14
15
16
# File 'lib/plivo/base_client.rb', line 14

def auth_credentials
  @auth_credentials
end

#headersObject (readonly)

Base stuff



14
15
16
# File 'lib/plivo/base_client.rb', line 14

def headers
  @headers
end

Instance Method Details

#auth_idObject



24
25
26
# File 'lib/plivo/base_client.rb', line 24

def auth_id
  @auth_credentials[:auth_id]
end

#process_response(method, response) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/plivo/base_client.rb', line 28

def process_response(method, response)
  handle_response_exceptions(response)
  if method == 'DELETE'
    if !([200, 204].include? response[:status])
      raise Exceptions::PlivoRESTError, "Resource at #{response[:url]} "\
      'couldn\'t be deleted'
    end
  elsif !([200, 201, 202, 204, 207, 206].include? response[:status])
    raise Exceptions::PlivoRESTError, "Received #{response[:status]} for #{method}"
  end
  @@voice_retry_count = 0
  response[:body]
end

#send_request(resource_path, method = 'GET', data = {}, timeout = nil, use_multipart_conn = false, options = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/plivo/base_client.rb', line 42

def send_request(resource_path, method = 'GET', data = {}, timeout = nil, use_multipart_conn = false, options = nil)
  timeout ||= @timeout

  if options[:is_voice_request] == true
    response = case method
               when 'GET' then send_get(resource_path, data, timeout, is_voice_request: options[:is_voice_request], voice_retry_count: @@voice_retry_count)
               when 'POST' then send_post(resource_path, data, timeout, use_multipart_conn, is_voice_request: options[:is_voice_request], voice_retry_count: @@voice_retry_count)
               when 'DELETE' then send_delete(resource_path, data, timeout, is_voice_request: options[:is_voice_request], voice_retry_count: @@voice_retry_count)
               else raise_invalid_request("#{method} not supported by Plivo, yet")
               end

    if response.status >= 500
      @@voice_retry_count += 1
      if @@voice_retry_count > 2
        return process_response(method, response.to_hash)
      end
      is_voice_request = true
      send_request(resource_path, method, data, timeout, use_multipart_conn, is_voice_request: is_voice_request)
    else
      process_response(method, response.to_hash)
    end
  elsif options[:is_lookup_request] == true
    response = case method
               when 'GET' then send_get(resource_path, data, timeout, is_lookup_request: options[:is_lookup_request])
               else raise_invalid_request("#{method} not supported by Plivo, yet")
               end
    process_response(method, response.to_hash)
  else
    response = case method
               when 'GET' then send_get(resource_path, data, timeout)
               when 'POST' then send_post(resource_path, data, timeout, use_multipart_conn)
               when 'DELETE' then send_delete(resource_path, data, timeout)
               else raise_invalid_request("#{method} not supported by Plivo, yet")
               end

    process_response(method, response.to_hash)
  end
end