Class: Shaf::Responder::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/shaf/responder/base.rb

Direct Known Subclasses

AlpsJson, Hal, Html, ProblemJson

Constant Summary collapse

PRELOAD_FAILED_MSG =
"Failed to preload '%s'. Link could not be extracted from response"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, resource, **options) ⇒ Base

Returns a new instance of Base.



86
87
88
89
90
# File 'lib/shaf/responder/base.rb', line 86

def initialize(controller, resource, **options)
  @controller = controller
  @resource = resource
  @options = options
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



84
85
86
# File 'lib/shaf/responder/base.rb', line 84

def controller
  @controller
end

#optionsObject (readonly)

Returns the value of attribute options.



84
85
86
# File 'lib/shaf/responder/base.rb', line 84

def options
  @options
end

#resourceObject (readonly)

Returns the value of attribute resource.



84
85
86
# File 'lib/shaf/responder/base.rb', line 84

def resource
  @resource
end

Class Method Details

.call(controller, resource, preload: [], **kwargs) ⇒ Object



40
41
42
43
44
45
# 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) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/shaf/responder/base.rb', line 47

def can_handle?(_obj)
  true
end

.mime_type(type = nil, value = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# 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

.use_as_default!Object



36
37
38
# File 'lib/shaf/responder/base.rb', line 36

def use_as_default!
  Responder.default = self
end

Instance Method Details

#bodyObject

Raises:

  • (NotImplementedError)


92
93
94
# File 'lib/shaf/responder/base.rb', line 92

def body
  raise NotImplementedError, "#{self.class} must implement #body"
end

#build_responseObject



100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Object



120
121
122
# File 'lib/shaf/responder/base.rb', line 120

def lookup_rel(_rel, _response)
  []
end


111
112
113
114
115
116
117
118
# 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_hashObject



96
97
98
# File 'lib/shaf/responder/base.rb', line 96

def serialized_hash
  {}
end