Module: Webmachine::Decision::Helpers

Includes:
QuotedString, Streaming
Included in:
FSM
Defined in:
lib/webmachine/decision/helpers.rb

Overview

Methods that assist the Decision Flow.

Constant Summary

Constant Summary

Constants included from QuotedString

QuotedString::QS_ANCHORED, QuotedString::QUOTED_STRING

Instance Method Summary (collapse)

Methods included from QuotedString

#escape_quotes, #quote, #unescape_quotes, #unquote

Instance Method Details

- (Object) accept_helper

Assists in receiving request bodies



58
59
60
61
62
63
64
65
66
# File 'lib/webmachine/decision/helpers.rb', line 58

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.



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/webmachine/decision/helpers.rb', line 79

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

- (Boolean) body_is_fixed_length?

Determines whether the response is of a fixed lenghth, i.e. it is a String or IO with known size.

Returns:

  • (Boolean)


124
125
126
127
# File 'lib/webmachine/decision/helpers.rb', line 124

def body_is_fixed_length?
  response.body.respond_to?(:bytesize) &&
    Fixnum === response.body.bytesize
end

- (Object) encode_body

Encodes the body in the selected charset and encoding.



27
28
29
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
# File 'lib/webmachine/decision/helpers.rb', line 27

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?
    set_content_length
  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.

See Also:



22
23
24
# File 'lib/webmachine/decision/helpers.rb', line 22

def encode_body_if_set
  encode_body if has_response_body?
end

- (Object) ensure_content_length

Ensures that responses have an appropriate Content-Length header



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/webmachine/decision/helpers.rb', line 93

def ensure_content_length
  case
  when response.headers['Transfer-Encoding']
    return
  when [204, 205, 304].include?(response.code)
    response.headers.delete 'Content-Length'
  when has_response_body?
    set_content_length
  else
    response.headers['Content-Length'] = '0'
  end
end

- (Object) ensure_date_header

Ensures that responses have an appropriate Date header



107
108
109
110
111
# File 'lib/webmachine/decision/helpers.rb', line 107

def ensure_date_header
  if (200..499).include?(response.code)
    response.headers['Date'] ||= Time.now.httpdate
  end
end

- (Boolean) has_response_body?

Determines if the response has a body/entity set.

Returns:

  • (Boolean)


16
17
18
# File 'lib/webmachine/decision/helpers.rb', line 16

def has_response_body?
  !response.body.nil? && !response.body.empty?
end

- (Object) set_content_length

Sets the Content-Length header on the response



114
115
116
117
118
119
120
# File 'lib/webmachine/decision/helpers.rb', line 114

def set_content_length
  if response.body.respond_to?(:bytesize)
    response.headers['Content-Length'] = response.body.bytesize.to_s
  else
    response.headers['Content-Length'] = response.body.length.to_s
  end
end

- (Object) variances

Computes the entries for the 'Vary' response header



69
70
71
72
73
74
75
76
# File 'lib/webmachine/decision/helpers.rb', line 69

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