lownode 0.5.16 → 0.5.17

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: 5a01009c6584f1352d24f61bdf12335cf29eb9b04a5e691296cd808f5cebf507
4
- data.tar.gz: a50a01e2d46f9f063df2774e2ac3c09dac744792b7ba2bf93eb5f051e4a86616
3
+ metadata.gz: 9ebedc5ce0d14fe37bf8da82b53667396090f5420f3c67fea6c97f1dd4b81574
4
+ data.tar.gz: a293bc4836581a5aa4804a0fa409e02ba20243ab880aeccc076923ef24e056ba
5
5
  SHA512:
6
- metadata.gz: 5473139ad4b873a578a90db695a3442d8cfc46eac6e110db2e1ce6c2606c0fcc9a483b18c4edbda917c450412a169678306a9eeae7eae90d8cccc71c7f2d216c
7
- data.tar.gz: 1d9b4edb457dadeb2bc9c759a18eb44bd674a6245ad5b120d532d28b07be7e85e7e2bf81f8ae9e842da283d70ecc5bf2ac60235e616121d5b3f2e06eef8ec3db
6
+ metadata.gz: be5574331e3c4225179bc6712651dbbc9b5f6386eff4daa1b67c7d394419715f98b5cf41d4109ae72e2653b67859caadaeb4ecb20934527f25adb806fd27ae34
7
+ data.tar.gz: 21f955a2a0bb6f91c02cf82e9cb0fc96c9d728bc7aca7f1d133bb2e36b8bbbca62640a018ae9775fcbe7aaa50de9b342029dab91458110371ff57a429dfd7d66
data/lib/lownode.rb CHANGED
@@ -27,6 +27,11 @@ class LowNode
27
27
  @response_headers ||= Protocol::HTTP::Headers.new
28
28
  end
29
29
 
30
+ # The headers the node added, or nil when it added none, so a response without any doesn't make them.
31
+ def added_headers
32
+ @response_headers
33
+ end
34
+
30
35
  # Adds a header to this node's response, like add_header('cache-control', 'public, max-age=3600'), for an action that
31
36
  # returns its body, like a string, a hash or an array.
32
37
  def add_header(name, value)
@@ -25,9 +25,9 @@ module Low
25
25
  mod.module_eval <<~RUBY, __FILE__, __LINE__ + 1
26
26
  def #{action}(**kwargs)
27
27
  node, body = action_body(:#{action}, kwargs)
28
- return if body.nil? || body == ''
28
+ return if body.nil? || (body.is_a?(String) && body.empty?)
29
29
 
30
- response_event(body:, event: kwargs[:event], headers: node&.response_headers)
30
+ response_event(body:, event: kwargs[:event], headers: node&.added_headers)
31
31
  end
32
32
  RUBY
33
33
  end
@@ -113,9 +113,12 @@ module Low
113
113
  # The keywords the method takes, like Antlers::Keywords.accepted, but with its params read once for each method,
114
114
  # as reading them makes new objects every time. A method that's defined again, like when a file reloads, is a
115
115
  # different method, so its params are read again.
116
+ # When the method takes every keyword given, like a node's initialize(event:), they're passed as they are.
116
117
  def accepted_keywords(method, kwargs)
117
118
  names = Renderer.keyword_names[method] ||= keyword_names(method.parameters)
118
- names == :all ? kwargs : kwargs.slice(*names)
119
+ return kwargs if names == :all || kwargs.all? { |key, _| names.include?(key) }
120
+
121
+ kwargs.slice(*names)
119
122
  end
120
123
 
121
124
  def keyword_names(parameters)
@@ -138,13 +141,18 @@ module Low
138
141
 
139
142
  # What a template or action rendered, as HTML unless the node added its own content type, like text/plain.
140
143
  def html_response_event(html:, event:, headers:)
141
- headers.add('content-type', 'text/html') unless headers.include?('content-type')
144
+ headers.add('content-type', 'text/html') unless content_type?(headers)
142
145
  # A rendered page is a new string, so strip it in place rather than copying it. A frozen string is copied.
143
146
  html = html.frozen? ? html.strip : html.tap(&:strip!)
144
147
  response = Protocol::HTTP::Response.new('HTTP/1.1', response_status(event), headers, Protocol::HTTP::Body::Buffered.wrap(html))
145
148
  Low::Events::ResponseEvent.new(response:).tap(&:branch)
146
149
  end
147
150
 
151
+ # Whether the node set a content type. Its headers are looked through as a list, as include? indexes them all.
152
+ def content_type?(headers)
153
+ headers.fields.any? { |name, _| (name = name.to_s).length == 12 && name.casecmp('content-type') == 0 } # rubocop:disable Style/NumericPredicate
154
+ end
155
+
148
156
  # A response an action made itself, with the headers the node set, like its cookies, added to its own.
149
157
  def protocol_response_event(response:, headers:)
150
158
  headers.each { |name, value| response.headers.add(name, value) }
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Low
4
4
  module Node
5
- VERSION = '0.5.16'
5
+ VERSION = '0.5.17'
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.16
4
+ version: 0.5.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - maedi