module NextcallerClient
Constants
- BASE_URL
urls
- DEFAULT_PHONE_LENGTH
- DEFAULT_REDIRECT_ATTEMPTS
- DEFAULT_REQUEST_TIMEOUT
default values
- DEFAULT_USER_AGENT
- FULL_URL
- JSON_CONTENT_TYPE
- JSON_RESPONSE_FORMAT
- RESPONSE_FORMATS
- VERSION
- XML_RESPONSE_FORMAT
Public Class Methods
default_handle_response(resp, response_format='json')
click to toggle source
# 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
log()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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={})
click to toggle source
Prepare url from path and params
# 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 = url_params.collect { |k, v| "#{CGI::escape(k.to_s)}=#{CGI::escape(v.to_s)}" }.join('&') url += '?' + url_params_str end url end
prepare_url_for_test(auth, path)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Validate response format
# 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)
click to toggle source
Validate phone format
# 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