Class: Whoosh::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/whoosh/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

Returns a new instance of Request.



12
13
14
15
16
# File 'lib/whoosh/request.rb', line 12

def initialize(env)
  @env = env
  @rack_request = Rack::Request.new(env)
  @path_params = {}
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#path_paramsObject

Returns the value of attribute path_params.



9
10
11
# File 'lib/whoosh/request.rb', line 9

def path_params
  @path_params
end

Instance Method Details

#bodyObject



34
35
36
# File 'lib/whoosh/request.rb', line 34

def body
  @body ||= parse_body
end

#content_typeObject



46
47
48
# File 'lib/whoosh/request.rb', line 46

def content_type
  @rack_request.content_type
end

#cookiesObject



57
58
59
60
61
62
63
64
65
# File 'lib/whoosh/request.rb', line 57

def cookies
  @cookies ||= begin
    raw = @env["HTTP_COOKIE"] || ""
    raw.split(";").each_with_object({}) do |pair, h|
      k, v = pair.strip.split("=", 2)
      h[k] = v if k
    end
  end
end

#filesObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/whoosh/request.rb', line 67

def files
  @files ||= begin
    storage = @env["whoosh.storage"]
    form_data = @rack_request.params
    form_data.each_with_object({}) do |(key, value), hash|
      next unless value.is_a?(Hash) && value[:tempfile]
      hash[key] = UploadedFile.new(value, storage: storage)
    end
  end
end

#headersObject



38
39
40
# File 'lib/whoosh/request.rb', line 38

def headers
  @headers ||= extract_headers
end

#idObject



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

def id
  @id ||= @env["whoosh.request_id"] || headers["X-Request-Id"] || SecureRandom.uuid
end

#loggerObject



50
51
52
53
54
55
# File 'lib/whoosh/request.rb', line 50

def logger
  @logger ||= begin
    base_logger = @env["whoosh.logger"]
    base_logger&.with_context(request_id: id)
  end
end

#methodObject



18
19
20
# File 'lib/whoosh/request.rb', line 18

def method
  @rack_request.request_method
end

#paramsObject



26
27
28
# File 'lib/whoosh/request.rb', line 26

def params
  @params ||= query_params.merge(@path_params)
end

#pathObject



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

def path
  @rack_request.path_info
end

#query_paramsObject



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

def query_params
  @query_params ||= Rack::Utils.parse_query(@rack_request.query_string)
end