module ElvantoAPI::Resource::ClassMethods

Public Instance Methods

collection_method?(method) click to toggle source

@param [Symbol] method The name of the method to call @return [Boolean] True if API method returns a set of objects, otherwise false

# File lib/elvanto/resources/resource.rb, line 100
def collection_method? method
  return unless defined? resource_class::COLLECTION_METHODS
  resource_class::COLLECTION_METHODS.keys.include? method
end
collection_name() click to toggle source
# File lib/elvanto/resources/resource.rb, line 51
def collection_name
  Utils.pluralize Utils.underscore(resource_name)
end
collection_path() click to toggle source
# File lib/elvanto/resources/resource.rb, line 55
def collection_path
  collection_name
end
Also aliased as: href
construct_from_response(payload) click to toggle source

@param [Symbol] payload Body of the API response @return [Object] Instance of the class specified in body

# File lib/elvanto/resources/resource.rb, line 67
def construct_from_response(payload)

  payload = ElvantoAPI::Utils.indifferent_read_access payload
  # the remaining keys here are just hypermedia resources
  payload.slice!(member_name)

  instance = nil
  
  payload.each do |key, value|
    if value.class == Hash
      resource_body = value
    else
      resource_body = value.first
    end
    # > Singular resources are represented as JSON objects. However,
    # they are still wrapped inside an array:
    #resource_body = value.first
    cls = ("ElvantoAPI::" + ElvantoAPI::Utils.classify(key)).constantize
    instance = cls.new resource_body
  end
  instance
  
end
def_instance_methods(instance_methods={}) click to toggle source
# File lib/elvanto/resources/resource.rb, line 115
def def_instance_methods instance_methods={}
  instance_methods.each do |key, value|
    define_method(key) do |options={}|
      self.query_member(value, options)
    end
  end
end
href()
Alias for: collection_path
member_method?(method) click to toggle source

@param [Symbol] method The name of the method to call @return [Boolean] True if API method returns a single object, otherwise false

# File lib/elvanto/resources/resource.rb, line 93
def member_method? method
  return unless defined? resource_class::MEMBER_METHODS
  resource_class::MEMBER_METHODS.keys.include? method
end
member_name() click to toggle source
# File lib/elvanto/resources/resource.rb, line 61
def member_name
  Utils.underscore resource_name
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/elvanto/resources/resource.rb, line 105
def method_missing(method, *args, &block)
  if member_method? method
    query_member(resource_class::MEMBER_METHODS[method], *args)
  elsif collection_method? method
    query_collection(resource_class::COLLECTION_METHODS[method], *args)
  else
    super method, *args, &block
  end
end
query_collection(method, options={}) click to toggle source

@params [Symbol] method The name of the method to call @params [Hash] options The parameters to pass to the method. @return [Array] The response from the API method.

# File lib/elvanto/resources/resource.rb, line 135
def query_collection(method, options={})
  uri = href + "/" + method.to_s
  pager = ElvantoAPI::Pager.new uri, options
  pager.to_a
end
query_member(method, options={}) click to toggle source

@params [Symbol] method The name of the method to call @params [Hash] options The parameters to pass to the method. @return [Object] The response from the API method.

# File lib/elvanto/resources/resource.rb, line 126
def query_member(method, options={})
  uri = href + "/" + method.to_s
  response = ElvantoAPI.post uri, options
  construct_from_response response.body         
end
resource_class() click to toggle source
# File lib/elvanto/resources/resource.rb, line 47
def resource_class
  name.constantize
end
resource_name() click to toggle source
# File lib/elvanto/resources/resource.rb, line 43
def resource_name
  Utils.demodulize name
end