Class: Cb::Utils::Api

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Observable
Defined in:
lib/cb/utils/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers: {}) ⇒ Api

Returns a new instance of Api.



32
33
34
35
36
37
38
39
40
# File 'lib/cb/utils/api.rb', line 32

def initialize(headers: {})
  self.class.default_params developerkey: Cb.configuration.dev_key,
                            outputjson: Cb.configuration.use_json.to_s

  self.class.default_timeout Cb.configuration.time_out
  self.class.headers.merge! ({ 'developerkey' => Cb.configuration.dev_key })
  self.class.headers.merge! ({ 'accept-encoding' => 'deflate, gzip' }) unless Cb.configuration.debug_api
  self.class.headers.merge! headers
end

Class Method Details

.camelize(input) ⇒ Object



165
166
167
168
# File 'lib/cb/utils/api.rb', line 165

def self.camelize(input)
  input.sub!(/^[a-z\d]*/) { $&.capitalize }
  input.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{Regexp.last_match(2).capitalize}" }.gsub('/', '::')
end

.criteria_to_hash(criteria) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cb/utils/api.rb', line 108

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: {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/cb/utils/api.rb', line 21

def self.instance(headers: {})
  api = Cb::Utils::Api.new(headers: headers)
  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) ⇒ Boolean

Returns:



121
122
123
# File 'lib/cb/utils/api.rb', line 121

def self.is_numeric?(obj)
  true if Float(obj) rescue false
end

Instance Method Details

#append_api_responses(obj, resp) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cb/utils/api.rb', line 76

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) ⇒ Object



54
55
56
# File 'lib/cb/utils/api.rb', line 54

def cb_delete(path, options = {}, &block)
  timed_http_request(:delete, nil, path, options, &block)
end

#cb_get(path, options = {}, &block) ⇒ Object



42
43
44
# File 'lib/cb/utils/api.rb', line 42

def cb_get(path, options = {}, &block)
  timed_http_request(:get, nil, path, options, &block)
end

#cb_post(path, options = {}, &block) ⇒ Object



46
47
48
# File 'lib/cb/utils/api.rb', line 46

def cb_post(path, options = {}, &block)
  timed_http_request(:post, nil, path, options, &block)
end

#cb_put(path, options = {}, &block) ⇒ Object



50
51
52
# File 'lib/cb/utils/api.rb', line 50

def cb_put(path, options = {}, &block)
  timed_http_request(:put, nil, path, options, &block)
end

#execute_http_request(http_method, uri, path, options = {}) ⇒ Object



71
72
73
74
# File 'lib/cb/utils/api.rb', line 71

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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cb/utils/api.rb', line 58

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