Module: ApiModel::ClassMethods

Included in:
Base
Defined in:
lib/api_model/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#attribute_synonym(primary_method_name, *alternate_names) ⇒ Object

TODO - try to think of a more memorable name for this…



5
6
7
8
9
# File 'lib/api_model/class_methods.rb', line 5

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
  end
end

#cache(path, &block) ⇒ Object



43
44
45
46
47
# File 'lib/api_model/class_methods.rb', line 43

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



37
38
39
40
41
# File 'lib/api_model/class_methods.rb', line 37

def cache_id(path, options={})
  return @cache_id if @cache_id
  p = (options[:params] || {}).collect{ |k,v| "#{k}#{v}" }.join("")
  "#{path}#{p}"
end

#call_api(method, path, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/api_model/class_methods.rb', line 28

def call_api(method, path, options={})
  cache cache_id(path, options) do
    request = HttpRequest.new path: path, method: method, config: api_model_configuration
    request.builder = options.delete(:builder) || api_model_configuration.builder || self
    request.options.deep_merge! options
    request.run.build_objects
  end
end

#call_api_with_json(method, path, body = nil, options = {}) ⇒ Object



23
24
25
26
# File 'lib/api_model/class_methods.rb', line 23

def call_api_with_json(method, path, body=nil, options={})
  body = body.to_json if body.is_a?(Hash)
  call_api method, path, options.merge(body: body)
end

#get_json(path, params = {}, options = {}) ⇒ Object



11
12
13
# File 'lib/api_model/class_methods.rb', line 11

def get_json(path, params={}, options={})
  call_api :get, path, options.merge(params: params)
end

#post_json(path, body = nil, options = {}) ⇒ Object



15
16
17
# File 'lib/api_model/class_methods.rb', line 15

def post_json(path, body=nil, options={})
  call_api_with_json :post, path, body, options
end

#put_json(path, body = nil, options = {}) ⇒ Object



19
20
21
# File 'lib/api_model/class_methods.rb', line 19

def put_json(path, body=nil, options={})
  call_api_with_json :put, path, body, options
end