Module: NextcallerClient

Defined in:
lib/test_nextcaller_client/utils.rb,
lib/test_nextcaller_client/client.rb,
lib/test_nextcaller_client/version.rb,
lib/test_nextcaller_client/constants.rb,
lib/test_nextcaller_client/transport.rb,
lib/test_nextcaller_client/exceptions.rb

Defined Under Namespace

Classes: Client, TooManyRedirects

Constant Summary collapse

VERSION =
"0.0.2"
DEFAULT_REQUEST_TIMEOUT =

default values

60
JSON_RESPONSE_FORMAT =
'json'
XML_RESPONSE_FORMAT =
'xml'
RESPONSE_FORMATS =
[JSON_RESPONSE_FORMAT, XML_RESPONSE_FORMAT]
DEFAULT_PHONE_LENGTH =
10
DEFAULT_USER_AGENT =
'nextcaller/ruby/0.0.1'
JSON_CONTENT_TYPE =
'application/json; charset=utf-8'
DEFAULT_REDIRECT_ATTEMPTS =
10
BASE_URL =

urls

'api.nextcaller.com/v2/'
FULL_URL =
'https://api.nextcaller.com/v2/'

Class Method Summary collapse

Class Method Details

.default_handle_response(resp, response_format = 'json') ⇒ Object



19
20
21
22
23
# File 'lib/test_nextcaller_client/utils.rb', line 19

def self.default_handle_response(resp, response_format='json')
  return JSON.parse(resp.body) if response_format == 'json'
  return Nokogiri::XML(resp.body) if response_format == 'xml'
  resp
end

.logObject



3
4
5
6
7
# File 'lib/test_nextcaller_client/transport.rb', line 3

def self.log
  log ||= Logger.new(STDOUT)
  log.level = Logger::DEBUG
  log
end

.make_http_request(auth, url, method = 'GET', debug = false, data = {}, user_agent = DEFAULT_USER_AGENT, redirect_attempts_left = DEFAULT_REDIRECT_ATTEMPTS) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/test_nextcaller_client/transport.rb', line 9

def self.make_http_request(auth, url, method='GET', debug=false, data={}, user_agent=DEFAULT_USER_AGENT,
                      redirect_attempts_left = DEFAULT_REDIRECT_ATTEMPTS)
  raise TooManyRedirects if redirect_attempts_left < 0
  log = NextcallerClient.log
  uri = URI.parse(url)
  case method
    when 'GET'
      request = Net::HTTP::Get.new(uri.path)
    when 'POST'
      request = Net::HTTP::Post.new(uri.path)
      request['Content-Type'] = 'application/json'
      request.body = data.to_json
  end
  request.basic_auth auth[:username], auth[:password]
  request['Connection'] = 'Keep-Alive'
  request['User-Agent'] = user_agent if user_agent

  http = Net::HTTP.new(uri.hostname, uri.port)
  http.read_timeout = DEFAULT_REQUEST_TIMEOUT
  http.use_ssl = true
  response = http.start { |http| http.request(request) }

  log.debug('Request url: %s' % url) if debug
  log.debug('Request body: %s' % data.to_s) if debug and method == 'POST'
  case response
    when Net::HTTPSuccess then
      response
    when Net::HTTPRedirection then
      location = response['location']
      log.debug("redirected to: #{location}") if debug
      make_http_request(auth, location, data, method, user_agent, debug, redirect_attempts_left - 1)
    # else
    #   if 400 <= response.code < 500
    #       raise HttpException '%s Client Error: %s' % [response.code, self.reason]
    #   elsif 500 <= response.code < 600
    #     raise HttpException '%s Server Error: %s' % [response.code, self.reason]
    #   end
  end
end

.prepare_json_data(data) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/test_nextcaller_client/utils.rb', line 3

def self.prepare_json_data(data)
  begin
    return data.to_json
  rescue ArgumentError, TypeError
    return data
  end
end

.prepare_url(path, url_params = {}) ⇒ Object

Prepare url from path and params



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/test_nextcaller_client/utils.rb', line 52

def self.prepare_url(path, url_params={})
  url = '%s%s' % [FULL_URL, path]
  unless url.end_with?('/')
    url += '/'
  end
  unless url_params.empty?
    url_params_str = CGI::escape(url_params.collect { |k, v| "#{k}=#{v}" }.join('&'))
    url += '?' + url_params_str
  end
  url
end

.prepare_url_for_test(auth, path) ⇒ Object



64
65
66
# File 'lib/test_nextcaller_client/utils.rb', line 64

def self.prepare_url_for_test(auth, path)
  return Regexp.new 'https://%s:%s@%s%s*' %[auth[:username], auth[:password], BASE_URL, path]
end

.to_json(data) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/test_nextcaller_client/utils.rb', line 11

def self.to_json(data)
  begin
    return JSON.parse(data)
  rescue ArgumentError, TypeError
    return data
  end
end

.validate_format(response_format) ⇒ Object

Validate response format



45
46
47
48
49
# File 'lib/test_nextcaller_client/utils.rb', line 45

def self.validate_format(response_format)
  unless RESPONSE_FORMATS.include? response_format
    raise ArgumentError, 'Unsupported format: %s. Supported formats are: %s' % [response_format, RESPONSE_FORMATS]
  end
end

.validate_phone(value, length = DEFAULT_PHONE_LENGTH) ⇒ Object

Validate phone format



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/test_nextcaller_client/utils.rb', line 26

def self.validate_phone(value, length=DEFAULT_PHONE_LENGTH)
  unless value
    raise ArgumentError, 'Invalid phone number: %s. Phone cannot be blank.' % value
  end
  if value.is_a? Integer
    value = value.to_s
  end
  unless value.is_a? String
    raise ArgumentError, 'Invalid phone number: %s. Phone cannot be type of %s.' % [value, value.class]
  end
  unless value.length == length
    raise ArgumentError, 'Invalid phone number: %s. Phone should has length %s.' % [value, length]
  end
  unless value =~ /^[0-9]+$/
    raise ArgumentError, 'Invalid phone number: %s. Phone should consists of only digits.' % value
  end
end