class Shaf::Responder::Base

Constants

PRELOAD_FAILED_MSG

Attributes

controller[R]
options[R]
resource[R]

Public Class Methods

call(controller, resource, preload: [], **kwargs) click to toggle source
# File lib/shaf/responder/base.rb, line 40
def call(controller, resource, preload: [], **kwargs)
  responder = new(controller, resource, preload_rels: preload, **kwargs)
  response = responder.build_response
  log_response(controller, response)
  write_response(controller, response)
end
can_handle?(_obj) click to toggle source
# File lib/shaf/responder/base.rb, line 47
def can_handle?(_obj)
  true
end
mime_type(type = nil, value = nil) click to toggle source
# File lib/shaf/responder/base.rb, line 24
def mime_type(type = nil, value = nil)
  if type
    @mime_type = type
    @mime_type = Sinatra::Base.mime_type(type, value) if type.is_a? Symbol
    Responder.register(self)
  elsif defined? @mime_type
    @mime_type
  else
    raise Error, "Class #{self} must register a mime type"
  end
end
new(controller, resource, **options) click to toggle source
# File lib/shaf/responder/base.rb, line 86
def initialize(controller, resource, **options)
  @controller = controller
  @resource = resource
  @options = options
end
use_as_default!() click to toggle source
# File lib/shaf/responder/base.rb, line 36
def use_as_default!
  Responder.default = self
end

Private Class Methods

log(controller, msg, type: :debug) click to toggle source
# File lib/shaf/responder/base.rb, line 57
def log(controller, msg, type: :debug)
  return unless controller.respond_to? :log
  controller.log.send(type, msg)
end
log_response(controller, response) click to toggle source
# File lib/shaf/responder/base.rb, line 53
def log_response(controller, response)
  log(controller, response.log_entry)
end
write_response(controller, response) click to toggle source
# File lib/shaf/responder/base.rb, line 62
def write_response(controller, response)
  controller.content_type(response.content_type)
  add_preload_links(controller, response)
  controller.body(response.body)
end

Public Instance Methods

body() click to toggle source
# File lib/shaf/responder/base.rb, line 92
def body
  raise NotImplementedError, "#{self.class} must implement #body"
end
build_response() click to toggle source
# File lib/shaf/responder/base.rb, line 100
def build_response
  Response.new(
    content_type: mime_type,
    body: body,
    serialized_hash: serialized_hash,
    resource: resource
  ).tap do |response|
    response.preload_links = preload_links(response)
  end
end
lookup_rel(_rel, _response) click to toggle source
# File lib/shaf/responder/base.rb, line 120
def lookup_rel(_rel, _response)
  []
end
serialized_hash() click to toggle source
# File lib/shaf/responder/base.rb, line 96
def serialized_hash
  {}
end

Private Instance Methods

mime_type() click to toggle source
# File lib/shaf/responder/base.rb, line 126
def mime_type
  self.class.mime_type
end
user() click to toggle source
# File lib/shaf/responder/base.rb, line 130
def user
  options.fetch(:current_user) do
    controller.current_user if controller.respond_to? :current_user
  end
end