Class: Eipiai::ApiRepresenter

Inherits:
Roar::Decorator
  • Object
show all
Includes:
Representer
Defined in:
lib/eipiai/roar/representers/api.rb

Overview

ApiRepresenter

This representer returns an object that represents the ‘/api` endpoint of the API. It contains all routes as defined in the Webmachine route, and also tells the client if the route is templated or not.

Class Method Summary collapse

Methods included from Representer

included, #path, #url

Class Method Details

.add_route(route, options) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/eipiai/roar/representers/api.rb', line 25

def self.add_route(route, options)
  link(options) do |context|
    request = context[:request]
    path    = '/' + route.path_spec.map { |e| e.is_a?(Symbol) ? "{#{e}}" : e }.join('/')

    request.present? ? Addressable::URI.parse(request.uri).merge(path: path).to_s : path
  end
end

.populate_from_app(app) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/eipiai/roar/representers/api.rb', line 13

def self.populate_from_app(app)
  app.routes.each do |route|
    next unless route.resource.respond_to?(:link_identifier)

    templated = route.path_spec.any? { |r| r.is_a?(Symbol) }
    options = { rel: route.resource.link_identifier }
    options.merge!(templated: templated) if templated

    add_route(route, options)
  end
end