module Her::Model::Paths::ClassMethods

Public Instance Methods

build_request_path(path=nil, parameters={}) click to toggle source

Return a custom path based on the collection path and variable parameters

@example

class User
  include Her::Model
  collection_path "/utilisateurs"
end

User.all # Fetched via GET /utilisateurs
# File lib/her/model/paths.rb, line 62
def build_request_path(path=nil, parameters={})
  unless path.is_a?(String)
    parameters = path || {}
    path = parameters.include?(:id) && !parameters[:id].nil? ? resource_path : collection_path
  end

  path.gsub(/:([\w_]+)/) do
    # Look for :key or :_key, otherwise raise an exception
    parameters.delete($1.to_sym) || parameters.delete("_#{$1}".to_sym) || raise(Her::Errors::PathError.new("Missing :_#{$1} parameter to build the request path (#{path})."))
  end
end
collection_path(path=nil) click to toggle source

Defines a custom collection path for the resource

@example

class User
  include Her::Model
  collection_path "/users"
end
# File lib/her/model/paths.rb, line 27
def collection_path(path=nil)
  @her_collection_path ||= begin
    superclass.collection_path.dup if superclass.respond_to?(:collection_path)
  end

  return @her_collection_path unless path
  @her_resource_path = "#{path}/:id"
  @her_collection_path = path
end
resource_path(path=nil) click to toggle source

Defines a custom resource path for the resource

@example

class User
  include Her::Model
  resource_path "/users/:id"
end
# File lib/her/model/paths.rb, line 44
def resource_path(path=nil)
  @her_resource_path ||= begin
    superclass.resource_path.dup if superclass.respond_to?(:resource_path)
  end

  return @her_resource_path unless path
  @her_resource_path = path
end
root_element(value=nil) click to toggle source

Return or change the value of ‘root_element`

# File lib/her/model/paths.rb, line 75
def root_element(value=nil)
  return @root_element if value.nil?
  @root_element = value
end