Module: Sinatra::Helpers
- Defined in:
- lib/sinatra_opal_patches.rb
Overview
-
Helpers#body= (upstream base.rb:300)
Upstream uses Rack::Files::BaseIterator which exists on Rack 3.x CRuby but not in homura’s vendored Rack. The public Iterator class is available, so swap to it.
Instance Method Summary collapse
- #body(value = nil, &block) ⇒ Object
-
#content_type(type = nil, params = {}) ⇒ Object
————————————————————- 5.
-
#etag_matches?(list, new_resource = request.post?) ⇒ Boolean
————————————————————- 6.
-
#halt(*halt_response) ⇒ Object
————————————————————- 3.5.
-
#redirect(uri_value, *args) ⇒ Object
————————————————————- 4.5.
-
#uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object
————————————————————- 4.
Instance Method Details
#body(value = nil, &block) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/sinatra_opal_patches.rb', line 91 def body(value = nil, &block) if block_given? def block.each; yield(call) end response.body = block elsif value files_iterator = defined?(::Rack::Files::Iterator) ? ::Rack::Files::Iterator : nil stream_cls = defined?(::Sinatra::Stream) ? ::Sinatra::Stream : nil unless request.head? || (files_iterator && value.is_a?(files_iterator)) || (stream_cls && value.is_a?(stream_cls)) headers.delete('content-length') end response.body = value else response.body end end |
#content_type(type = nil, params = {}) ⇒ Object
-
Helpers#content_type (upstream base.rb:400)
Upstream uses ‘mime_type << ’;‘` and `mime_type << params…`. Opal Strings are immutable. Also the upstream string join uses `’;‘` which doesn’t match CGI convention for content-type; the homura port switched to ‘, ` between key=value params to match how Rack normalises Content-Type. Preserved that behaviour here.
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/sinatra_opal_patches.rb', line 175 def content_type(type = nil, params = {}) return response['content-type'] unless type default = params.delete :default mime_type = mime_type(type) || default raise format('Unknown media type: %p', type) if mime_type.nil? mime_type = mime_type.dup unless params.include?(:charset) || settings.add_charset.all? { |p| !(p === mime_type) } params[:charset] = params.delete('charset') || settings.default_encoding end params.delete(:charset) if mime_type.include?('charset') unless params.empty? mime_type += (mime_type.include?(';') ? ', ' : ';') mime_type += params.map do |key, val| val = val.inspect if val =~ /[";,]/ "#{key}=#{val}" end.join(', ') end response['content-type'] = mime_type end |
#etag_matches?(list, new_resource = request.post?) ⇒ Boolean
-
Helpers#etag_matches? (upstream base.rb:722)
Minor: upstream splits on ‘,’ and strips each entry. Regex split is equivalent and slightly faster.
203 204 205 206 207 |
# File 'lib/sinatra_opal_patches.rb', line 203 def etag_matches?(list, new_resource = request.post?) return !new_resource if list == '*' list.to_s.split(/\s*,\s*/).include?(response['ETag']) end |
#halt(*halt_response) ⇒ Object
3.5. Helpers#halt (upstream base.rb:1030)
Upstream uses ‘throw :halt`. That works for synchronous Ruby, but not once an Opal-compiled route has crossed an async boundary. Snapshot the final Rack tuple and raise a dedicated exception so both sync and async routes preserve Sinatra semantics.
117 118 119 |
# File 'lib/sinatra_opal_patches.rb', line 117 def halt(*halt_response) raise HaltResponse.new(materialize_halt_payload(*halt_response)) end |
#redirect(uri_value, *args) ⇒ Object
4.5. Helpers#redirect (upstream base.rb:309)
Upstream delegates to ‘halt(*args)` after mutating the response. Under Opal async routes that path can degrade into a plain `[]` body after awaited Sequel calls. Raise HaltResponse directly with the fully materialized Rack tuple so both sync and async routes preserve the redirect status/location reliably.
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/sinatra_opal_patches.rb', line 154 def redirect(uri_value, *args) http_version = env['SERVER_PROTOCOL'] || env['HTTP_VERSION'] if (http_version == 'HTTP/1.1') && (env['REQUEST_METHOD'] != 'GET') status 303 else status 302 end response['Location'] = uri(uri_value.to_s, settings.absolute_redirects?, settings.prefixed_redirects?) raise HaltResponse.new(materialize_halt_payload(*args)) end |
#uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object
-
Helpers#uri (upstream base.rb:330)
Upstream mutates ‘host` with `<<`. Opal Strings are JS Strings and therefore immutable, so we build with `+` and reassignment.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/sinatra_opal_patches.rb', line 127 def uri(addr = nil, absolute = true, add_script_name = true) return addr if addr.to_s =~ /\A[a-z][a-z0-9+.\-]*:/i host = '' if absolute host = host + "http#{'s' if request.secure?}://" host = host + if request.forwarded? || (request.port != (request.secure? ? 443 : 80)) request.host_with_port else request.host end end uri = [host] uri << request.script_name.to_s if add_script_name uri << (addr || request.path_info).to_s File.join(uri) end |