Module: Webmachine::Decision::Helpers
- Included in:
- FSM
- Defined in:
- lib/webmachine/decision/helpers.rb
Overview
Methods that assist the Decision Flow.
Constant Summary
- QUOTED =
Pattern for quoted headers
/^"(.*)"$/
Instance Method Summary (collapse)
-
- (Object) accept_helper
Assists in receiving request bodies.
-
- (Object) add_caching_headers
Adds caching-related headers to the response.
-
- (Object) encode_body
Encodes the body in the selected charset and encoding.
-
- (Object) encode_body_if_set
If the response body exists, encode it.
-
- (Object) ensure_quoted_header(value)
Ensures that a header is quoted (like ETag).
-
- (Boolean) has_response_body?
Determines if the response has a body/entity set.
-
- (Object) unquote_header(value)
Unquotes request headers (like ETag).
-
- (Object) variances
Computes the entries for the 'Vary' response header.
Instance Method Details
- (Object) accept_helper
Assists in receiving request bodies
70 71 72 73 74 75 76 77 78 |
# File 'lib/webmachine/decision/helpers.rb', line 70 def accept_helper content_type = MediaType.parse(request.content_type || 'application/octet-stream') acceptable = resource.content_types_accepted.find {|ct, _| content_type.match?(ct) } if acceptable resource.send(acceptable.last) else 415 end end |
- (Object) add_caching_headers
Adds caching-related headers to the response.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/webmachine/decision/helpers.rb', line 91 def add_caching_headers if etag = resource.generate_etag response.headers['ETag'] = ensure_quoted_header(etag) end if expires = resource.expires response.headers['Expires'] = expires.httpdate end if modified = resource.last_modified response.headers['Last-Modified'] = modified.httpdate end end |
- (Object) encode_body
Encodes the body in the selected charset and encoding.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/webmachine/decision/helpers.rb', line 23 def encode_body body = response.body chosen_charset = ['Charset'] chosen_encoding = ['Content-Encoding'] charsetter = resource.charsets_provided && resource.charsets_provided.find {|c,_| c == chosen_charset }.last || :charset_nop encoder = resource.encodings_provided[chosen_encoding] response.body = case body when String # 1.8 treats Strings as Enumerable resource.send(encoder, resource.send(charsetter, body)) when Fiber FiberEncoder.new(resource, encoder, charsetter, body) when Enumerable EnumerableEncoder.new(resource, encoder, charsetter, body) else if body.respond_to?(:call) CallableEncoder.new(resource, encoder, charsetter, body) else resource.send(encoder, resource.send(charsetter, body)) end end if String === response.body response.headers['Content-Length'] = response.body.respond_to?(:bytesize) ? response.body.bytesize.to_s : response.body.length.to_s else response.headers.delete 'Content-Length' response.headers['Transfer-Encoding'] = 'chunked' end end |
- (Object) encode_body_if_set
If the response body exists, encode it.
18 19 20 |
# File 'lib/webmachine/decision/helpers.rb', line 18 def encode_body_if_set encode_body if has_response_body? end |
- (Object) ensure_quoted_header(value)
Ensures that a header is quoted (like ETag)
52 53 54 55 56 57 58 |
# File 'lib/webmachine/decision/helpers.rb', line 52 def ensure_quoted_header(value) if value =~ QUOTED value else '"' << value << '"' end end |
- (Boolean) has_response_body?
Determines if the response has a body/entity set.
12 13 14 |
# File 'lib/webmachine/decision/helpers.rb', line 12 def has_response_body? !response.body.nil? && !response.body.empty? end |
- (Object) unquote_header(value)
Unquotes request headers (like ETag)
61 62 63 64 65 66 67 |
# File 'lib/webmachine/decision/helpers.rb', line 61 def unquote_header(value) if value =~ QUOTED $1 else value end end |
- (Object) variances
Computes the entries for the 'Vary' response header
81 82 83 84 85 86 87 88 |
# File 'lib/webmachine/decision/helpers.rb', line 81 def variances resource.variances.tap do |v| v.unshift "Accept-Language" if resource.languages_provided.size > 1 v.unshift "Accept-Charset" if resource.charsets_provided && resource.charsets_provided.size > 1 v.unshift "Accept-Encoding" if resource.encodings_provided.size > 1 v.unshift "Accept" if resource.content_types_provided.size > 1 end end |