Skip to content

Commit

Permalink
Add a current state value that includes signed + now
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft committed May 17, 2024
1 parent 5c24a5a commit 1ad18e7
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 135 deletions.
13 changes: 8 additions & 5 deletions lib/turbo_boost/commands/middlewares/exit_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def modify?(env)
def modify!(env, response)
responder = env["turbo_boost_command_responder"]
status, headers, body = response
new_body = body_to_s(body)
new_body = body_to_a(body).join

case response_type(new_body)
when :body
Expand All @@ -45,10 +45,13 @@ def modify!(env, response)
[status, headers, body]
end

def body_to_s(body)
return body.join if body.respond_to?(:join)
return body.to_ary.join if body.respond_to?(:to_ary)
body.to_s
def body_to_a(body)
return [] if body.nil?
return body if body.is_a?(Array)
return [body] if body.is_a?(String)
return body.to_ary if body.respond_to?(:to_ary)
return body.each.to_a if body.respond_to?(:each)
[body.to_s]
end

def response_type(body)
Expand Down
4 changes: 2 additions & 2 deletions lib/turbo_boost/commands/responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ def initialize
end

def body
@body.join
@body.join.html_safe
end

def add_header(key, value)
headers[key.to_s.downcase] = value.to_s
end

def add_content(content)
@body << "#{sanitizer.sanitize(content.to_s).html_safe}\n"
@body << sanitizer.sanitize(content) + "\n"
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/turbo_boost/commands/sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TurboBoost::Commands::Sanitizer
attr_reader :scrubber

def sanitize(value)
super(value, scrubber: scrubber)
super(value.to_s, scrubber: scrubber)
end

private
Expand Down
8 changes: 6 additions & 2 deletions lib/turbo_boost/commands/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def initialize(payload = {})
store[:_signed] ||= {}
end

delegate_missing_to :store
def current
signed.merge now
end

delegate_missing_to :current

def now
store[:_now]
Expand All @@ -28,7 +32,7 @@ def signed
end

def cache_key
"TurboBoost::Commands::State/#{Digest::SHA2.base64digest(to_s)}"
"TurboBoost::Commands::State/#{Digest::SHA2.base64digest(current.to_s)}"
end

def to_json
Expand Down
Loading

0 comments on commit 1ad18e7

Please sign in to comment.