class Cb::Utils::Api

Public Class Methods

criteria_to_hash(criteria) click to toggle source
# File lib/cb/utils/api.rb, line 107
def self.criteria_to_hash(criteria)
  params = {}
  if criteria.respond_to?(:instance_variables)
    criteria.instance_variables.each do |var|
      var_name = var.to_s
      var_name.slice!(0)
      var_name_hash_safe = camelize(var_name)
      params["#{var_name_hash_safe}"] = criteria.instance_variable_get(var)
    end
  end
  params
end
instance(headers: {}, use_default_params: true) click to toggle source
# File lib/cb/utils/api.rb, line 21
def self.instance(headers: {}, use_default_params: true)
  api = Cb::Utils::Api.new(headers: headers, use_default_params: use_default_params)
  Cb.configuration.observers.each do |class_name|
    api.add_observer(class_name.new)
  end
  if Cb.configuration.debug_api && !Cb.configuration.observers.include?(Cb::Utils::ConsoleObserver)
    api.add_observer(Cb::Utils::ConsoleObserver.new)
  end
  api
end
is_numeric?(obj) click to toggle source
# File lib/cb/utils/api.rb, line 120
def self.is_numeric?(obj)
  true if Float(obj) rescue false
end
new(headers: {}, use_default_params: true) click to toggle source
# File lib/cb/utils/api.rb, line 32
def initialize(headers: {}, use_default_params: true)
  self.class.default_params add_default_params if use_default_params
  self.class.default_timeout Cb.configuration.time_out
  h = { 'developerkey' => Cb.configuration.dev_key }
  h.merge! ({ 'accept-encoding' => 'deflate, gzip' }) unless Cb.configuration.debug_api
  h.merge! headers
  self.class.headers(h)
end

Private Class Methods

camelize(input) click to toggle source
# File lib/cb/utils/api.rb, line 164
def self.camelize(input)
  input.sub!(/^[a-z\d]*/) { $&.capitalize }
  input.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{Regexp.last_match(2).capitalize}" }.gsub('/', '::')
end

Public Instance Methods

append_api_responses(obj, resp) click to toggle source
# File lib/cb/utils/api.rb, line 75
def append_api_responses(obj, resp)
  #As of ruby 2.2 nil is frozen so stop monkey patching it please -jyeary
  if obj.nil? && obj.frozen?
    obj = Cb::Utils::NilResponse.new
  end
  meta_class = ensure_non_nil_metavalues(obj)

  resp.each do |api_key, api_value|
    meta_name = format_hash_key(api_key)
    unless meta_name.empty?
      if meta_name == 'errors' && api_value.is_a?(Hash)
        api_value = api_value.values
      elsif meta_name == 'error' && api_value.is_a?(String)
        # this is a horrible hack to get consistent object.cb_response.errors behavior for the client
        meta_name = 'errors'
        api_value = [api_value]
      elsif self.class.is_numeric?(api_value)
        api_value = api_value.to_i
      end

      meta_class.class.send(:attr_reader, meta_name)
      meta_class.instance_variable_set(:"@#{meta_name}", api_value)
    end
  end
  obj.class.send(:attr_reader, 'api_error')
  obj.instance_variable_set(:@api_error, @api_error)

  obj.class.send(:attr_reader, 'cb_response')
  obj.instance_variable_set(:@cb_response, meta_class)
  obj
end
cb_delete(path, options = {}, &block) click to toggle source
# File lib/cb/utils/api.rb, line 53
def cb_delete(path, options = {}, &block)
  timed_http_request(:delete, nil, path, options, &block)
end
cb_get(path, options = {}, &block) click to toggle source
# File lib/cb/utils/api.rb, line 41
def cb_get(path, options = {}, &block)
  timed_http_request(:get, nil, path, options, &block)
end
cb_post(path, options = {}, &block) click to toggle source
# File lib/cb/utils/api.rb, line 45
def cb_post(path, options = {}, &block)
  timed_http_request(:post, nil, path, options, &block)
end
cb_put(path, options = {}, &block) click to toggle source
# File lib/cb/utils/api.rb, line 49
def cb_put(path, options = {}, &block)
  timed_http_request(:put, nil, path, options, &block)
end
execute_http_request(http_method, uri, path, options = {}) click to toggle source
# File lib/cb/utils/api.rb, line 70
def execute_http_request(http_method, uri, path, options = {})
  self.class.base_uri(uri || Cb.configuration.base_uri)
  self.class.method(http_method).call(path, options)
end
timed_http_request(http_method, uri, path, options = {}, &block) click to toggle source
# File lib/cb/utils/api.rb, line 57
def timed_http_request(http_method, uri, path, options = {}, &block)
  api_caller = find_api_caller(caller)
  response = nil
  start_time = Time.now.to_f
  cb_event(:"cb_#{ http_method }_before", path, options, api_caller, response, 0.0, &block)
  begin
    response = execute_http_request(http_method, uri, path, options)
  ensure
    cb_event(:"cb_#{ http_method }_after", path, options, api_caller, response, Time.now.to_f - start_time, &block)
  end
  validate_response(response)
end

Private Instance Methods

add_default_params() click to toggle source
# File lib/cb/utils/api.rb, line 178
def add_default_params
  {
      developerkey: Cb.configuration.dev_key,
      outputjson: Cb.configuration.use_json.to_s
  }
end
api_call_model(api_call_type, path, options, api_caller, response, time_elapsed) click to toggle source
# File lib/cb/utils/api.rb, line 145
def api_call_model(api_call_type, path, options, api_caller, response, time_elapsed)
  Cb::Models::ApiCall.new(api_call_type, path, options, api_caller, response, time_elapsed)
end
cb_event(api_call_type, path, options, api_caller, response, time_elapsed, &block) click to toggle source
# File lib/cb/utils/api.rb, line 149
def cb_event(api_call_type, path, options, api_caller, response, time_elapsed, &block)
  call_model = api_call_model(api_call_type, path, options, api_caller, response, time_elapsed)
  block.call(call_model) if block_given?
  changed(true)
  notify_observers(call_model)
end
ensure_non_nil_metavalues(obj) click to toggle source
# File lib/cb/utils/api.rb, line 156
def ensure_non_nil_metavalues(obj)
  if obj.respond_to?('cb_response') && !obj.cb_response.nil?
    obj.cb_response
  else
    Cb::Utils::MetaValues.new
  end
end
find_api_caller(call_list) click to toggle source
# File lib/cb/utils/api.rb, line 126
def find_api_caller(call_list)
  filename_regex = /.*\.rb/
  linenum_regex = /:.*:in `/
  filename, method_name = call_list.find { |l| use_this_api_caller?(l[filename_regex]) }[0..-2].split(linenum_regex)
  simplified_filename = filename.include?('/lib/') ? filename[/\/lib\/.*/] : filename
  simplified_filename = simplified_filename.include?('/app/') ? simplified_filename[/\/app\/.*/] : simplified_filename
  { file: simplified_filename, method: method_name }
end
format_hash_key(api_hash_key) click to toggle source
# File lib/cb/utils/api.rb, line 173
def format_hash_key(api_hash_key)
  return '' unless api_hash_key.respond_to?(:snakecase)
  api_hash_key.snakecase
end
set_api_error(validated_response) click to toggle source
# File lib/cb/utils/api.rb, line 169
def set_api_error(validated_response)
  @api_error = validated_response.keys.empty?
end
use_this_api_caller?(calling_file) click to toggle source
# File lib/cb/utils/api.rb, line 135
def use_this_api_caller?(calling_file)
  (calling_file == __FILE__ || calling_file.include?('/lib/cb/client.rb')) ? false : true
end
validate_response(response) click to toggle source
# File lib/cb/utils/api.rb, line 139
def validate_response(response)
  validated_response = ResponseValidator.validate(response)
  set_api_error(validated_response)
  validated_response
end