Module: Eipiai::Representer

Included in:
ApiRepresenter
Defined in:
lib/eipiai/representers/base.rb

Overview

Representer

The base Representer which all representers should include.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



13
14
15
16
# File 'lib/eipiai/representers/base.rb', line 13

def self.included(base)
  base.include(Roar::JSON::HAL)
  base.include(ClassMethods)
end

Instance Method Details

#pathString

path

The path of the represented object, based on the representer name, and the object being represented.

Given ‘UserItemRepresenter`, the following parts are assembled: * ’/user’ * ‘/user/user_uid` if object responds to `user_uid` * `/user/user_uid/item` (or `/user/item`) * `/user/user_uid/item/uid` if object responds to `uid`

Examples:

user = User.new(uid: 'bartsimpson')
UserRepresenter.new(user).path # => '/user/bartsimpson'
UserRepresenter.new(nil).path # => '/user'

Returns:

  • (String)

    the path of the represented object



38
39
40
41
# File 'lib/eipiai/representers/base.rb', line 38

def path
  parts = path_parts.map { |part| part_path(part) }
  File.join(*parts)
end

#url(uri = nil) ⇒ String

url

The url of the object.

Based on the input, the result can vary:

Examples:

as a relative path

user = User.new(uid: 'bartsimpson')
UserRepresenter.new(user).url # => '/user/bartsimpson'

with url given as argument

user = User.new(uid: 'bartsimpson')
UserRepresenter.new(user).url('https://example.org')
  # => 'https://example.org/user/bartsimpson'

with url as global configuration

Eipiai.configuration.base_uri = 'https://base.org'
user = User.new(uid: 'bartsimpson')
UserRepresenter.new(user).url # => 'https://base.org/user/bartsimpson'

Parameters:

  • uri (String) (defaults to: nil)

    string to prepend to the object path

Returns:

  • (String)

    uri with path of object appended



66
67
68
69
70
# File 'lib/eipiai/representers/base.rb', line 66

def url(uri = nil)
  uri ||= Eipiai.configuration.base_uri

  File.join uri.to_s, path
end