Class: Sinatra::Request
- Inherits:
-
Rack::Request
- Object
- Rack::Request
- Sinatra::Request
- Defined in:
- lib/sinatra/base.rb
Overview
The request object. See Rack::Request for more info: http://rack.rubyforge.org/doc/classes/Rack/Request.html
Defined Under Namespace
Classes: AcceptEntry
Constant Summary collapse
- HEADER_PARAM =
/\s*[\w.]+=(?:[\w.]+|"(?:[^"\\]|\\.)*")?\s*/
- HEADER_VALUE_WITH_PARAMS =
/(?:(?:\w+|\*)\/(?:\w+(?:\.|\-|\+)?|\*)*)\s*(?:;#{HEADER_PARAM})*/
Instance Method Summary collapse
-
#accept ⇒ Object
Returns an array of acceptable media types for the response.
- #accept?(type) ⇒ Boolean
- #forwarded? ⇒ Boolean
- #idempotent? ⇒ Boolean
- #link? ⇒ Boolean
- #preferred_type(*types) ⇒ Object
- #safe? ⇒ Boolean
- #unlink? ⇒ Boolean
Instance Method Details
#accept ⇒ Object
Returns an array of acceptable media types for the response
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sinatra/base.rb', line 23 def accept @env['sinatra.accept'] ||= begin if @env.include? 'HTTP_ACCEPT' and @env['HTTP_ACCEPT'].to_s != '' @env['HTTP_ACCEPT'].to_s.scan(HEADER_VALUE_WITH_PARAMS). map! { |e| AcceptEntry.new(e) }.sort else [AcceptEntry.new('*/*')] end end end |
#accept?(type) ⇒ Boolean
34 35 36 |
# File 'lib/sinatra/base.rb', line 34 def accept?(type) preferred_type(type).to_s.include?(type) end |
#forwarded? ⇒ Boolean
51 52 53 |
# File 'lib/sinatra/base.rb', line 51 def forwarded? @env.include? "HTTP_X_FORWARDED_HOST" end |
#idempotent? ⇒ Boolean
59 60 61 |
# File 'lib/sinatra/base.rb', line 59 def idempotent? safe? or put? or delete? or link? or unlink? end |
#link? ⇒ Boolean
63 64 65 |
# File 'lib/sinatra/base.rb', line 63 def link? request_method == "LINK" end |
#preferred_type(*types) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sinatra/base.rb', line 38 def preferred_type(*types) accepts = accept # just evaluate once return accepts.first if types.empty? types.flatten! return types.first if accepts.empty? accepts.detect do |pattern| type = types.detect { |t| File.fnmatch(pattern, t) } return type if type end end |
#safe? ⇒ Boolean
55 56 57 |
# File 'lib/sinatra/base.rb', line 55 def safe? get? or head? or or trace? end |
#unlink? ⇒ Boolean
67 68 69 |
# File 'lib/sinatra/base.rb', line 67 def unlink? request_method == "UNLINK" end |