class DTK::Common::Response::RestClientWrapper

Constants

RestClientErrors

Public Class Methods

delete(url, body={}, opts={}) click to toggle source
# File lib/response.rb, line 185
def delete(url, body={}, opts={})
  delete_raw(url,body,opts){|raw_response|Response.new(json_parse_if_needed(raw_response))}
end
delete_raw(url,body={},opts={},&block) click to toggle source
# File lib/response.rb, line 172
def delete_raw(url,body={},opts={},&block)
  error_handling(opts) do
    # DELETE method supports only query params
    url_with_params = generate_query_params_url(url, body)
    raw_response = ::RestClient::Resource.new(url_with_params,opts).delete()
    block ? block.call(raw_response) : raw_response
  end
end
get(url, body={}, opts={}) click to toggle source
# File lib/response.rb, line 161
def get(url, body={}, opts={})
  get_raw(url,body, opts){|raw_response|Response.new(json_parse_if_needed(raw_response))}
end
get_raw(url,body={},opts={},&block) click to toggle source
# File lib/response.rb, line 153
def get_raw(url,body={},opts={},&block)
  error_handling(opts) do
    url_with_params = generate_query_params_url(url, body)
    raw_response = ::RestClient::Resource.new(url_with_params,opts).get()
    block ? block.call(raw_response) : raw_response
  end
end
json_parse_if_needed(item) click to toggle source
# File lib/response.rb, line 189
def json_parse_if_needed(item)
  item.kind_of?(String) ? JSON.parse(item) : item
end
post(url,body={},opts={}) click to toggle source
# File lib/response.rb, line 181
def post(url,body={},opts={})
  post_raw(url,body,opts){|raw_response|Response.new(json_parse_if_needed(raw_response))}
end
post_raw(url,body={},opts={},&block) click to toggle source
# File lib/response.rb, line 165
def post_raw(url,body={},opts={},&block)
  error_handling(opts) do
    raw_response = ::RestClient::Resource.new(url,opts).post(body)
    block ? block.call(raw_response) : raw_response
  end
end

Private Class Methods

error_handling(opts={},&block) click to toggle source
# File lib/response.rb, line 215
def error_handling(opts={},&block)
  begin
    block.call
  rescue  ::RestClient::ResourceNotFound, RestClient::Request::Unauthorized, RestClient::BadRequest,::RestClient::InternalServerError => e
    # with latest set of changes we will consider this as special case since most of legacy code is expecting Response class
    parsed_response = safe_json_parse(e.response) || {}
    errors = parsed_response['errors'] || parsed_response['error'] || errors_field('Server Error')
    Response.new(StatusField => StatusNotok, ErrorsField => errors)
  rescue ::RestClient::Forbidden => e
    return error_response({ErrorsSubFieldCode => RestClientErrors[e.class.to_s]||GenericError, ErrorsOriginalException => e},opts) unless e.inspect.to_s.include?("PG::Error")

    errors = {"code" => "pg_error", "message" => e.inspect.to_s.strip, ErrorsOriginalException => e}
    error_response(errors)
  rescue ::RestClient::ServerBrokeConnection,::RestClient::RequestTimeout, Errno::ECONNREFUSED => e
    error_response({ErrorsSubFieldCode => RestClientErrors[e.class.to_s]||GenericError, ErrorsOriginalException => e},opts)
  rescue Exception => e
    error_response({ErrorsSubFieldCode => RestClientErrors[e.class.to_s], ErrorsOriginalException => e},opts)
  end
end
error_response(error_or_errors,opts={}) click to toggle source
# File lib/response.rb, line 235
def error_response(error_or_errors,opts={})
  errors = error_or_errors.kind_of?(Hash) ? [error_or_errors] : error_or_errors
  (opts[:error_response_class]||Error).new(StatusField => StatusNotok, ErrorsField => errors)
end
errors_field(msg=nil) click to toggle source
# File lib/response.rb, line 211
def errors_field(msg=nil)
  [msg.nil? ? {} : {'message' => msg}]
end
generate_query_params_url(url, params_hash) click to toggle source
# File lib/response.rb, line 194
def generate_query_params_url(url, params_hash)
  if params_hash.empty?
    return url
  else
    query_params_string = params_hash.map { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
    return url.concat('?').concat(query_params_string)
  end
end
safe_json_parse(string) click to toggle source
# File lib/response.rb, line 203
def safe_json_parse(string)
  begin
    JSON.parse(string)
  rescue => e
    nil
  end
end