Class: Cb::Utils::Api
Class Method Summary collapse
Instance Method Summary collapse
- #append_api_responses(obj, resp) ⇒ Object
- #cb_get(*args, &block) ⇒ Object
- #cb_get_secure(*args, &block) ⇒ Object
- #cb_post(*args, &block) ⇒ Object
-
#initialize ⇒ Api
constructor
A new instance of Api.
Constructor Details
#initialize ⇒ Api
Returns a new instance of Api.
10 11 12 13 14 15 16 |
# File 'lib/cb/utils/api.rb', line 10 def initialize self.class.debug_output $stderr if Cb.configuration.debug_api self.class.default_params :developerkey => Cb.configuration.dev_key, :outputjson => Cb.configuration.use_json.to_s self.class.default_timeout Cb.configuration.time_out end |
Class Method Details
.camelize(input) ⇒ Object
97 98 99 100 |
# File 'lib/cb/utils/api.rb', line 97 def self.camelize(input) input.sub!(/^[a-z\d]*/) { $&.capitalize } input.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$2.capitalize}" }.gsub('/', '::') end |
.criteria_to_hash(criteria) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cb/utils/api.rb', line 78 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 return params end |
.is_numeric?(obj) ⇒ Boolean
91 92 93 |
# File 'lib/cb/utils/api.rb', line 91 def self.is_numeric?(obj) true if Float(obj) rescue false end |
Instance Method Details
#append_api_responses(obj, resp) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/cb/utils/api.rb', line 45 def append_api_responses(obj, resp) if obj.respond_to?('cb_response') = obj.cb_response else = Cb::Utils::MetaValues.new() end resp.each do | api_key, api_value | = 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, ) end |
#cb_get(*args, &block) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/cb/utils/api.rb', line 18 def cb_get(*args, &block) self.class.base_uri Cb.configuration.base_uri response = self.class.get(*args, &block) validated_response = ResponseValidator.validate(response) set_api_error(validated_response) return validated_response end |
#cb_get_secure(*args, &block) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/cb/utils/api.rb', line 36 def cb_get_secure(*args, &block) self.class.base_uri Cb.configuration.base_uri_secure response = self.class.get(*args, &block) validated_response = ResponseValidator.validate(response) set_api_error(validated_response) return validated_response end |
#cb_post(*args, &block) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/cb/utils/api.rb', line 27 def cb_post(*args, &block) self.class.base_uri Cb.configuration.base_uri_secure response = self.class.post(*args, &block) validated_response = ResponseValidator.validate(response) set_api_error(validated_response) return validated_response end |