Module: Webmachine::Decision::Helpers

Included in:
FSM
Defined in:
lib/webmachine/decision/helpers.rb

Overview

Methods that assist the Decision Flow.

Constant Summary

QUOTED =
/^"(.*)"$/

Instance Method Summary collapse

Instance Method Details

#accept_helperObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/webmachine/decision/helpers.rb', line 53

def accept_helper
  content_type = request.content_type || 'application/octet-stream'
  type, params = media_type_to_detail(content_type)
  ['mediaparams'] = params
  acceptable = resource.content_types_accepted.select {|ct, _| ct == content_type }
  if acceptable.any?
    _, acceptor = acceptable.first
    resource.send(acceptor)
  else
    415
  end          
end

#encode_bodyObject

Encodes the body in the selected charset and encoding.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/webmachine/decision/helpers.rb', line 19

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 }.first || :charset_nop
  encoder = resource.encodings_provided[chosen_encoding]
  case body
  when Enumerable
    # TODO: Streaming support
  when body.respond_to?(:call)
    # TODO: Streaming support
  else
    response.body = resource.send(encoder, resource.send(charsetter, body))
  end
end

#encode_body_if_setObject

If the response body exists, encode it.

See Also:



14
15
16
# File 'lib/webmachine/decision/helpers.rb', line 14

def encode_body_if_set
  encode_body if has_response_body?
end

#ensure_quoted_header(value) ⇒ Object

Ensures that a header is quoted (like ETag)



36
37
38
39
40
41
42
# File 'lib/webmachine/decision/helpers.rb', line 36

def ensure_quoted_header(value)
  if value =~ QUOTED
    value
  else
    '"' << value << '"'
  end
end

#has_response_body?Boolean

Determines if the response has a body/entity set.



8
9
10
# File 'lib/webmachine/decision/helpers.rb', line 8

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

#unquote_header(value) ⇒ Object

Unquotes request headers (like ETag)



45
46
47
48
49
50
51
# File 'lib/webmachine/decision/helpers.rb', line 45

def unquote_header(value)
  if value =~ QUOTED
    $1
  else
    value
  end
end

#variancesObject

Computes the entries for the 'Vary' response header



67
68
69
70
71
72
73
# File 'lib/webmachine/decision/helpers.rb', line 67

def variances
  resource.variances.tap do |v|
    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