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.
-
#uri(addr = nil, absolute = true, add_script_name = true) ⇒ Object
————————————————————- 4.
Instance Method Details
#body(value = nil, &block) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sinatra_opal_patches.rb', line 71 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.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sinatra_opal_patches.rb', line 122 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.
150 151 152 153 154 |
# File 'lib/sinatra_opal_patches.rb', line 150 def etag_matches?(list, new_resource = request.post?) return !new_resource if list == '*' list.to_s.split(/\s*,\s*/).include?(response['ETag']) 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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/sinatra_opal_patches.rb', line 95 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 |