Class: Ocular::Inputs::HTTP::Input::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/ocular/inputs/http_input.rb

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

Instance Method Details

#acceptObject

Returns an array of acceptable media types for the response



82
83
84
85
86
87
88
89
90
91
# File 'lib/ocular/inputs/http_input.rb', line 82

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

Returns:

  • (Boolean)


93
94
95
# File 'lib/ocular/inputs/http_input.rb', line 93

def accept?(type)
    preferred_type(type).to_s.include?(type)
end

#forwarded?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/ocular/inputs/http_input.rb', line 110

def forwarded?
    @env.include? "HTTP_X_FORWARDED_HOST"
end

#idempotent?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/ocular/inputs/http_input.rb', line 118

def idempotent?
    safe? or put? or delete? or link? or unlink?
end

#link?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/ocular/inputs/http_input.rb', line 122

def link?
    request_method == "LINK"
end

#preferred_type(*types) ⇒ Object



97
98
99
100
101
102
103
104
105
106
# File 'lib/ocular/inputs/http_input.rb', line 97

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

Returns:

  • (Boolean)


114
115
116
# File 'lib/ocular/inputs/http_input.rb', line 114

def safe?
    get? or head? or options? or trace?
end

#unlink?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/ocular/inputs/http_input.rb', line 126

def unlink?
    request_method == "UNLINK"
end