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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c02fc29557e688ab2333bcef47fd614de510c7bc46b57f796054c95e5f71686c
4
- data.tar.gz: 9822f642ec7c1a3d05743b421ae5a3ce0b526ac2040b871915dcdb513a1d8d38
3
+ metadata.gz: 88f8f9f10353fbd2b27cfc19fba0f9d562c500e4b6883940e117076ae8b0fbf6
4
+ data.tar.gz: df3d7c0a6d21c5fa87979696298065d217630c05f19c53865be32fb24a42fdfd
5
5
  SHA512:
6
- metadata.gz: e372071f4889e9a073be6af4849a16fd92f3c82eebb0dc2d7736a959041fedb79b2d80567ddb2217ab2a907b7517f82a2667661333f94a7cf8435f3746d47c68
7
- data.tar.gz: b58f490bdf20c1865c3b29e5c3e834a361e6d68a144418bc658c5da6381e800560dd1cc9586bb9f3ea58eb69c3b1c589aa1e9a70cbb62f1ddfdb1a25b34da13c
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)
@@ -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
- unless body.is_a?(String)
100
- return body if body.is_a?(Low::Events::ResponseEvent)
101
- return status_response_event(status_code: body.status_code, headers:) if body.is_a?(Low::Types::Status::StatusCode)
102
- return redirect_response_event(redirect: body, headers:) if body.is_a?(Low::Responses::Redirect)
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
- return if body.nil? || body == ''
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(body.strip))
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Low
4
4
  module Node
5
- VERSION = '0.5.9'
5
+ VERSION = '0.5.10'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lownode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.9
4
+ version: 0.5.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi