Class: Request::Rack

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

Overview

Rack request

Constant Summary collapse

SERVER_PORT =
Key.new('SERVER_PORT')
REQUEST_METHOD =
Key.new('REQUEST_METHOD')
RACK_URL_SCHEME =
Key.new('rack.url_scheme')
IF_MODIFIED_SINCE =
Key.new('HTTP_IF_MODIFIED_SINCE')
CONTENT_LENGTH =
Key.new('CONTENT_LENGTH')
InvalidKeyError =

Error raised when an invalid rack env key is accessed

Class.new(RuntimeError)
CONTENT_LENGTH_REGEXP =
/\A[0-9]+\z/.freeze

Constants inherited from Request

KEYS, METHODS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Request

#absolute_uri, #absolute_uri_path, #host, #host_with_port, #path_info, #query_params_hash, #query_string, #root_uri, #uid

Constructor Details

#initialize(rack_env) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize object

Parameters:

  • rack_env (Hash)


22
23
24
# File 'lib/request/rack.rb', line 22

def initialize(rack_env)
  @rack_env = rack_env
end

Instance Attribute Details

#rack_envHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return rack env

Returns:

  • (Hash)


32
33
34
# File 'lib/request/rack.rb', line 32

def rack_env
  @rack_env
end

Instance Method Details

#content_lengthFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return content length

Returns:

  • (Fixnum)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/request/rack.rb', line 91

def content_length
  value = @rack_env.fetch(CONTENT_LENGTH) do
    return 0
  end

  unless value =~ CONTENT_LENGTH_REGEXP
    raise InvalidKeyError, 'invalid content length'
  end

  value.to_i
end

#if_modified_sinceTime?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return if modified since

Returns:

  • (Time)

    if present

  • (nil)

    otherwise



125
126
127
128
129
130
131
132
# File 'lib/request/rack.rb', line 125

def if_modified_since
  value = @rack_env.fetch(IF_MODIFIED_SINCE) { return }
  begin
    Time.httpdate(value) 
  rescue ArgumentError 
    nil
  end
end

#portFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return http port

Returns:

  • (Fixnum)


56
57
58
# File 'lib/request/rack.rb', line 56

def port
  @rack_env.fetch(SERVER_PORT).to_i(10)
end

#protocolProtocol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return request protocol

Returns:



67
68
69
# File 'lib/request/rack.rb', line 67

def protocol
  Protocol.get(access(RACK_URL_SCHEME))
end

#query_paramsArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return query params

Returns:

  • (Array)


110
111
112
# File 'lib/request/rack.rb', line 110

def query_params
  Addressable::URI.form_unencode(query_string)
end

#request_methodMethod

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return request method

Returns:



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

def request_method
  Method.get(access(REQUEST_METHOD))
end