Class: Shaf::Responder::Html
- Inherits:
-
Base
- Object
- Base
- Shaf::Responder::Html
show all
- Defined in:
- lib/shaf/responder/html.rb
Constant Summary
Constants inherited
from Base
Base::PRELOAD_FAILED_MSG
Instance Attribute Summary
Attributes inherited from Base
#controller, #options, #resource
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#build_response, can_handle?, #initialize, #lookup_rel, mime_type, #preload_links, #serialized_hash, use_as_default!
Class Method Details
.call(controller, resource, preload: [], **kwargs) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/shaf/responder/html.rb', line 7
def call(controller, resource, preload: [], **kwargs)
responder = responder_for(resource, controller, preload_rels: preload, **kwargs)
response = responder.build_response
add_preload_links(controller, response)
html_responder = new(controller, resource, response: response)
html_response = html_responder.build_response
log_response(controller, response)
write_response(controller, html_response)
end
|
.responder_for(resource, controller, **kwargs) ⇒ Object
Returns the “original” (non-html) responder
21
22
23
24
25
|
# File 'lib/shaf/responder/html.rb', line 21
def responder_for(resource, controller, **kwargs)
responders = Responder.send(:supported_responders_for, resource)
responder_class = (responders - [self]).first || Responder.default
responder_class.new(controller, resource, **kwargs)
end
|
Instance Method Details
#body ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/shaf/responder/html.rb', line 28
def body
response = options[:response]
serialized = response.serialized_hash
if serialized.empty?
serialized = begin
JSON.parse(response.body)
rescue StandardError
response.body
end
end
render serialized
end
|
#render(serialized) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/shaf/responder/html.rb', line 42
def render(serialized)
locals = {
request_headers: ,
response_headers: ,
serialized: serialized
}
template =
case resource
when Formable::Form
locals.merge!(form: resource)
:form
else
:payload
end
controller.erb(template, locals: locals)
end
|
61
62
63
|
# File 'lib/shaf/responder/html.rb', line 61
def
controller.
end
|
65
66
67
68
69
70
71
72
73
|
# File 'lib/shaf/responder/html.rb', line 65
def
etag, kind = controller.send(:etag_for, options[:response].body)
prefix = kind == :weak ? 'W/' : ''
etag = %Q{#{prefix}"#{etag}"}
type = options[:response].content_type
controller..merge('Content-Type' => type, 'ETag' => etag)
end
|