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
add_preload_links(controller, response)
click to toggle source
# File lib/shaf/responder/base.rb, line 68 def add_preload_links(controller, response) response.preload_links.each do |links| links.each do |link| next unless link[:href] # Nginx http2_push_preload only processes relative URIs with absolute path href = link[:href].sub(%r{https?://[^/]+}, "") type = link.fetch(:as, 'fetch') xorigin = link.fetch(:crossorigin, 'anonymous') links = (controller.headers['Link'] || "").split(',').map(&:strip) links << "<#{href}>; rel=preload; as=#{type}; crossorigin=#{xorigin}" controller.headers["Link"] = links.join(', ') unless links.empty? end end end
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
preload_links(response)
click to toggle source
# File lib/shaf/responder/base.rb, line 111 def preload_links(response) Array(options[:preload_rels]).map do |rel| links = lookup_rel(rel, response) links = [links].compact unless links.is_a? Array log(controller, PRELOAD_FAILED_MSG % rel) if links.empty? links end 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