Module: ApiModel::ClassMethods
- Included in:
- Base
- Defined in:
- lib/api_model/class_methods.rb
Instance Method Summary collapse
- #attribute_synonym(primary_method_name, *alternate_names) ⇒ Object
- #attribute_synonyms ⇒ Object
- #cache(path, &block) ⇒ Object
- #cache_id(path, options = {}) ⇒ Object
- #call_api(method, path, options = {}) ⇒ Object
- #call_api_with_json(method, path, body = nil, options = {}) ⇒ Object
- #get_json(path, params = {}, options = {}) ⇒ Object
- #post_json(path, body = nil, options = {}) ⇒ Object
- #put_json(path, body = nil, options = {}) ⇒ Object
- #real_attribute_name_for(name) ⇒ Object
Instance Method Details
#attribute_synonym(primary_method_name, *alternate_names) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/api_model/class_methods.rb', line 8 def attribute_synonym(primary_method_name, *alternate_names) alternate_names.each do |alternate_name| alias_method "#{alternate_name}=".to_sym, "#{primary_method_name}=".to_sym attribute_synonyms[alternate_name] = primary_method_name end end |
#attribute_synonyms ⇒ Object
4 5 6 |
# File 'lib/api_model/class_methods.rb', line 4 def attribute_synonyms @attribute_synonyms ||= {} end |
#cache(path, &block) ⇒ Object
50 51 52 53 54 |
# File 'lib/api_model/class_methods.rb', line 50 def cache(path, &block) api_model_configuration.cache_strategy.new(path, api_model_configuration.cache_settings).cache do block.call end end |
#cache_id(path, options = {}) ⇒ Object
45 46 47 48 |
# File 'lib/api_model/class_methods.rb', line 45 def cache_id(path, ={}) p = ([:params] || {}).collect{ |k,v| "#{k}#{v}" }.join("") "#{path}#{p}" end |
#call_api(method, path, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/api_model/class_methods.rb', line 36 def call_api(method, path, ={}) cache .delete(:cache_id) || cache_id(path, ) do request = HttpRequest.new path: path, method: method, config: api_model_configuration request.builder = .delete(:builder) || api_model_configuration.builder || self request..deep_merge! request.run.build_objects end end |
#call_api_with_json(method, path, body = nil, options = {}) ⇒ Object
31 32 33 34 |
# File 'lib/api_model/class_methods.rb', line 31 def call_api_with_json(method, path, body=nil, ={}) body = body.to_json if body.is_a?(Hash) call_api method, path, .merge(body: body) end |
#get_json(path, params = {}, options = {}) ⇒ Object
19 20 21 |
# File 'lib/api_model/class_methods.rb', line 19 def get_json(path, params={}, ={}) call_api :get, path, .merge(params: params) end |
#post_json(path, body = nil, options = {}) ⇒ Object
23 24 25 |
# File 'lib/api_model/class_methods.rb', line 23 def post_json(path, body=nil, ={}) call_api_with_json :post, path, body, end |
#put_json(path, body = nil, options = {}) ⇒ Object
27 28 29 |
# File 'lib/api_model/class_methods.rb', line 27 def put_json(path, body=nil, ={}) call_api_with_json :put, path, body, end |
#real_attribute_name_for(name) ⇒ Object
15 16 17 |
# File 'lib/api_model/class_methods.rb', line 15 def real_attribute_name_for(name) attribute_synonyms[name] || name end |