Class: Eipiai::ApiRepresenter
- Inherits:
-
Roar::Decorator
- Object
- Roar::Decorator
- Eipiai::ApiRepresenter
- Includes:
- Representer
- Defined in:
- lib/eipiai/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
- .add_route(route, query_keys, options) ⇒ Object
- .populate_from_app(app) ⇒ Object
- .templated?(route) ⇒ Boolean
Methods included from Representer
Class Method Details
.add_route(route, query_keys, options) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/eipiai/representers/api.rb', line 29 def self.add_route(route, query_keys, ) link() do |context| request = context[:request] path = '/' + route.path_spec.map { |e| e.is_a?(Symbol) ? "{#{e}}" : e }.join('/') path += "{?#{query_keys.join(',')}}" if query_keys.any? request.present? ? Addressable::URI.parse(request.uri).merge(path: path).to_s : path end end |
.populate_from_app(app) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/eipiai/representers/api.rb', line 15 def self.populate_from_app(app) app.routes.each do |route| resource = route.resource.new(nil, nil) next unless resource.respond_to?(:resource_relation) && resource.top_level_relation? = { rel: resource.resource_relation, templated: templated?(route) } add_route(route, resource.query_keys, .compact) end end |
.templated?(route) ⇒ Boolean
39 40 41 42 43 44 |
# File 'lib/eipiai/representers/api.rb', line 39 def self.templated?(route) query_keys = route.resource.new(nil, nil).query_keys templated = query_keys.any? || route.path_spec.map(&:class).include?(Symbol) templated || nil end |