lownode 0.5.9 → 0.5.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/lownode.rb +6 -0
- data/lib/templates/renderer.rb +26 -6
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 88f8f9f10353fbd2b27cfc19fba0f9d562c500e4b6883940e117076ae8b0fbf6
|
|
4
|
+
data.tar.gz: df3d7c0a6d21c5fa87979696298065d217630c05f19c53865be32fb24a42fdfd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 06a9eaa678de704be7064f90db14932906cf95bcd9d2394be471e3c99f889ac9db21111765c62dc134eb17869a0f291f183db5a49d25ec716fd6e7f8edb10288
|
|
7
|
+
data.tar.gz: f9c2a6b4922a2625621a87c13aa34f61651bc3f180f38ae9af0b27978475341644fbf954ddd6cfbe4278c56579025d18b05e976a58ca5158f46648c0d5e7eb3e
|
data/lib/lownode.rb
CHANGED
|
@@ -27,6 +27,12 @@ class LowNode
|
|
|
27
27
|
@response_headers ||= Protocol::HTTP::Headers.new
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
+
# Adds a header to this node's response, like add_header('cache-control', 'public, max-age=3600'), for an action that
|
|
31
|
+
# returns its body, like a string, a hash or an array.
|
|
32
|
+
def add_header(name, value)
|
|
33
|
+
response_headers.add(name, value)
|
|
34
|
+
end
|
|
35
|
+
|
|
30
36
|
# Sets a cookie with this node's response. Expire one with "max_age: 0".
|
|
31
37
|
def set_cookie(name, value, **)
|
|
32
38
|
response_headers.add('set-cookie', Low::Responses::Cookie.new(name:, value:, **).to_s)
|
data/lib/templates/renderer.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require_relative 'template'
|
|
4
4
|
require 'antlers/keywords'
|
|
5
5
|
require 'protocol/http'
|
|
6
|
+
require 'json'
|
|
6
7
|
|
|
7
8
|
module Low
|
|
8
9
|
module Templates
|
|
@@ -96,18 +97,37 @@ module Low
|
|
|
96
97
|
# GOAL: Make return value configurable; ResponseEvent, Response, or body.
|
|
97
98
|
def response_event(body:, event:, headers: nil)
|
|
98
99
|
headers ||= Protocol::HTTP::Headers.new
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
case body
|
|
101
|
+
when Low::Events::ResponseEvent then body
|
|
102
|
+
when Low::Types::Status::StatusCode then status_response_event(status_code: body.status_code, headers:)
|
|
103
|
+
when Low::Responses::Redirect then redirect_response_event(redirect: body, headers:)
|
|
104
|
+
when Protocol::HTTP::Response then protocol_response_event(response: body, headers:)
|
|
105
|
+
when Hash, Array then json_response_event(data: body, event:, headers:)
|
|
106
|
+
when nil, '' then nil
|
|
107
|
+
else html_response_event(html: body, event:, headers:)
|
|
103
108
|
end
|
|
104
|
-
|
|
109
|
+
end
|
|
105
110
|
|
|
111
|
+
# What a template or action rendered, as HTML.
|
|
112
|
+
def html_response_event(html:, event:, headers:)
|
|
106
113
|
headers.add('content-type', 'text/html')
|
|
107
|
-
response = Protocol::HTTP::Response.new('HTTP/1.1', response_status(event), headers, Protocol::HTTP::Body::Buffered.wrap(
|
|
114
|
+
response = Protocol::HTTP::Response.new('HTTP/1.1', response_status(event), headers, Protocol::HTTP::Body::Buffered.wrap(html.strip))
|
|
108
115
|
Low::Events::ResponseEvent.new(response:).tap(&:branch)
|
|
109
116
|
end
|
|
110
117
|
|
|
118
|
+
# A response an action made itself, with the headers the node set, like its cookies, added to its own.
|
|
119
|
+
def protocol_response_event(response:, headers:)
|
|
120
|
+
headers.each { |name, value| response.headers.add(name, value) }
|
|
121
|
+
Low::Events::ResponseEvent.new(response:)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# A hash or array an action returned, as JSON.
|
|
125
|
+
def json_response_event(data:, event:, headers:)
|
|
126
|
+
headers.add('content-type', 'application/json')
|
|
127
|
+
body = Protocol::HTTP::Body::Buffered.wrap(JSON.generate(data))
|
|
128
|
+
Low::Events::ResponseEvent.new(response: Protocol::HTTP::Response.new('HTTP/1.1', response_status(event), headers, body))
|
|
129
|
+
end
|
|
130
|
+
|
|
111
131
|
# Actions like "post" can return "Status[200]" to respond without a body.
|
|
112
132
|
def status_response_event(status_code:, headers:)
|
|
113
133
|
response = Protocol::HTTP::Response.new('HTTP/1.1', status_code, headers, nil)
|
data/lib/version.rb
CHANGED