Class: Cb::Utils::Api
- Inherits:
-
Object
- Object
- Cb::Utils::Api
- Includes:
- HTTParty, Observable
- Defined in:
- lib/cb/utils/api.rb
Class Method Summary collapse
Instance Method Summary collapse
- #append_api_responses(obj, resp) ⇒ Object
- #cb_delete(path, options = {}, &block) ⇒ Object
- #cb_get(path, options = {}, &block) ⇒ Object
- #cb_post(path, options = {}, &block) ⇒ Object
- #cb_put(path, options = {}, &block) ⇒ Object
- #execute_http_request(http_method, path, options = {}, &block) ⇒ Object
-
#initialize ⇒ Api
constructor
A new instance of Api.
Constructor Details
#initialize ⇒ Api
Returns a new instance of Api.
22 23 24 25 26 27 28 29 |
# File 'lib/cb/utils/api.rb', line 22 def initialize 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 end |
Class Method Details
.criteria_to_hash(criteria) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cb/utils/api.rb', line 84 def self.criteria_to_hash(criteria) params = Hash.new 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 ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/cb/utils/api.rb', line 11 def self.instance api = Cb::Utils::Api.new 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
97 98 99 |
# File 'lib/cb/utils/api.rb', line 97 def self.is_numeric?(obj) true if Float(obj) rescue false end |
Instance Method Details
#append_api_responses(obj, resp) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cb/utils/api.rb', line 55 def append_api_responses(obj, resp) = (obj) resp.each do |api_key, api_value| = format_hash_key(api_key) unless .empty? if == 'errors' && api_value.is_a?(Hash) api_value = api_value.values elsif == 'error' && api_value.is_a?(String) # this is a horrible hack to get consistent object.cb_response.errors behavior for the client = 'errors' api_value = [api_value] elsif self.class.is_numeric?(api_value) api_value = api_value.to_i end .class.send(:attr_reader, ) .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, ) obj end |
#cb_delete(path, options = {}, &block) ⇒ Object
43 44 45 |
# File 'lib/cb/utils/api.rb', line 43 def cb_delete(path, ={}, &block) execute_http_request(:delete, path, , &block) end |
#cb_get(path, options = {}, &block) ⇒ Object
31 32 33 |
# File 'lib/cb/utils/api.rb', line 31 def cb_get(path, ={}, &block) execute_http_request(:get, path, , &block) end |
#cb_post(path, options = {}, &block) ⇒ Object
35 36 37 |
# File 'lib/cb/utils/api.rb', line 35 def cb_post(path, ={}, &block) execute_http_request(:post, path, , &block) end |
#cb_put(path, options = {}, &block) ⇒ Object
39 40 41 |
# File 'lib/cb/utils/api.rb', line 39 def cb_put(path, ={}, &block) execute_http_request(:put, path, , &block) end |
#execute_http_request(http_method, path, options = {}, &block) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/cb/utils/api.rb', line 47 def execute_http_request(http_method, path, ={}, &block) self.class.base_uri Cb.configuration.base_uri cb_event(:"cb_#{http_method.to_s}_before", path, , nil, &block) response = self.class.method(http_method).call(path, ) cb_event(:"cb_#{http_method.to_s}_after", path, , response, &block) validate_response(response) end |