Class: Rack::Accept::Request

Inherits:
Request
  • Object
show all
Defined in:
lib/rack/accept/request.rb

Overview

A container class for convenience methods when Rack::Accept is used on the request level as Rack middleware. Instances of this class also manage a lightweight cache of various header instances to speed up execution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

Returns a new instance of Request.



10
11
12
# File 'lib/rack/accept/request.rb', line 10

def initialize(env)
  @env = env
end

Instance Attribute Details

#env ⇒ Object (readonly)

Returns the value of attribute env.



8
9
10
# File 'lib/rack/accept/request.rb', line 8

def env
  @env
end

Instance Method Details

#best_charset(values) ⇒ Object

Determines the best character set to use in a response from the given character sets, if any is acceptable. For more information on how this value is determined, see the documentation for Rack::Accept::Header::PublicInstanceMethods#sort.



70
71
72
# File 'lib/rack/accept/request.rb', line 70

def best_charset(values)
  charset.best_of(values)
end

#best_encoding(values) ⇒ Object

Determines the best encoding to use in a response from the given encodings, if any is acceptable. For more information on how this value is determined, see the documentation for Rack::Accept::Header::PublicInstanceMethods#sort.



78
79
80
# File 'lib/rack/accept/request.rb', line 78

def best_encoding(values)
  encoding.best_of(values)
end

#best_language(values) ⇒ Object

Determines the best language to use in a response from the given languages, if any is acceptable. For more information on how this value is determined, see the documentation for Rack::Accept::Header::PublicInstanceMethods#sort.



86
87
88
# File 'lib/rack/accept/request.rb', line 86

def best_language(values)
  language.best_of(values)
end

#best_media_type(values) ⇒ Object

Determines the best media type to use in a response from the given media types, if any is acceptable. For more information on how this value is determined, see the documentation for Rack::Accept::Header::PublicInstanceMethods#sort.



62
63
64
# File 'lib/rack/accept/request.rb', line 62

def best_media_type(values)
  media_type.best_of(values)
end

#charset ⇒ Object

Provides access to the Rack::Accept::Charset instance for this request.



20
21
22
# File 'lib/rack/accept/request.rb', line 20

def charset
  @charset ||= Charset.new(env['HTTP_ACCEPT_CHARSET'])
end

#charset?(value) ⇒ Boolean

Returns true if the Accept-Charset request header indicates the given character set is acceptable, false otherwise.

Returns:

  • (Boolean)


42
43
44
# File 'lib/rack/accept/request.rb', line 42

def charset?(value)
  charset.accept?(value)
end

#encoding ⇒ Object

Provides access to the Rack::Accept::Encoding instance for this request.



25
26
27
# File 'lib/rack/accept/request.rb', line 25

def encoding
  @encoding ||= Encoding.new(env['HTTP_ACCEPT_ENCODING'])
end

#encoding?(value) ⇒ Boolean

Returns true if the Accept-Encoding request header indicates the given encoding is acceptable, false otherwise.

Returns:

  • (Boolean)


48
49
50
# File 'lib/rack/accept/request.rb', line 48

def encoding?(value)
  encoding.accept?(value)
end

#language ⇒ Object

Provides access to the Rack::Accept::Language instance for this request.



30
31
32
# File 'lib/rack/accept/request.rb', line 30

def language
  @language ||= Language.new(env['HTTP_ACCEPT_LANGUAGE'])
end

#language?(value) ⇒ Boolean

Returns true if the Accept-Language request header indicates the given language is acceptable, false otherwise.

Returns:

  • (Boolean)


54
55
56
# File 'lib/rack/accept/request.rb', line 54

def language?(value)
  language.accept?(value)
end

#media_type ⇒ Object

Provides access to the Rack::Accept::MediaType instance for this request.



15
16
17
# File 'lib/rack/accept/request.rb', line 15

def media_type
  @media_type ||= MediaType.new(env['HTTP_ACCEPT'])
end

#media_type?(value) ⇒ Boolean

Returns true if the Accept request header indicates the given media type is acceptable, false otherwise.

Returns:

  • (Boolean)


36
37
38
# File 'lib/rack/accept/request.rb', line 36

def media_type?(value)
  media_type.accept?(value)
end