Module: Webmachine::Decision::Helpers
- Includes:
- HeaderNegotiation, QuotedString, Streaming
- Included in:
- FSM
- Defined in:
- lib/webmachine/decision/helpers.rb
Overview
Methods that assist the Decision Flow.
Constant Summary
Constants included from QuotedString
QuotedString::QS_ANCHORED, QuotedString::QUOTED_STRING
Instance Method Summary collapse
-
#accept_helper ⇒ Object
Assists in receiving request bodies.
-
#add_caching_headers ⇒ Object
Adds caching-related headers to the response.
-
#body_is_fixed_length? ⇒ Boolean
Determines whether the response is of a fixed lenghth, i.e.
-
#encode_body ⇒ Object
Encodes the body in the selected charset and encoding.
-
#encode_body_if_set ⇒ Object
If the response body exists, encode it.
-
#has_response_body? ⇒ Boolean
Determines if the response has a body/entity set.
-
#variances ⇒ Object
Computes the entries for the ‘Vary’ response header.
Methods included from HeaderNegotiation
#ensure_content_length, #ensure_date_header
Methods included from QuotedString
#escape_quotes, #quote, #unescape_quotes, #unquote
Instance Method Details
#accept_helper ⇒ Object
Assists in receiving request bodies
61 62 63 64 65 66 67 68 69 |
# File 'lib/webmachine/decision/helpers.rb', line 61 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 |
#add_caching_headers ⇒ Object
Adds caching-related headers to the response.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/webmachine/decision/helpers.rb', line 82 def add_caching_headers if etag = resource.generate_etag response.headers['ETag'] = ETag.new(etag).to_s end if expires = resource.expires response.headers['Expires'] = expires.httpdate end if modified = resource.last_modified response.headers['Last-Modified'] = modified.httpdate end end |
#body_is_fixed_length? ⇒ Boolean
Determines whether the response is of a fixed lenghth, i.e. it is a String or IO with known size.
96 97 98 99 |
# File 'lib/webmachine/decision/helpers.rb', line 96 def body_is_fixed_length? response.body.respond_to?(:bytesize) && Integer === response.body.bytesize end |
#encode_body ⇒ Object
Encodes the body in the selected charset and encoding.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/webmachine/decision/helpers.rb', line 30 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 IO, StringIO IOEncoder.new(resource, encoder, 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 body_is_fixed_length? ensure_content_length(response) else response.headers.delete CONTENT_LENGTH response.headers[TRANSFER_ENCODING] = 'chunked' end end |
#encode_body_if_set ⇒ Object
If the response body exists, encode it.
25 26 27 |
# File 'lib/webmachine/decision/helpers.rb', line 25 def encode_body_if_set encode_body if has_response_body? end |
#has_response_body? ⇒ Boolean
Determines if the response has a body/entity set.
19 20 21 |
# File 'lib/webmachine/decision/helpers.rb', line 19 def has_response_body? !response.body.nil? && !response.body.empty? end |
#variances ⇒ Object
Computes the entries for the ‘Vary’ response header
72 73 74 75 76 77 78 79 |
# File 'lib/webmachine/decision/helpers.rb', line 72 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 |