Class: DecisionAgent::Web::RackRequestHelpers::RequestContext

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/web/rack_request_helpers.rb

Overview

Context object that provides convenience methods in route handlers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, route_params = {}) ⇒ RequestContext

Returns a new instance of RequestContext.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 18

def initialize(env, route_params = {})
  @env = env
  @request = Rack::Request.new(env)
  @route_params = route_params
  @response_status = 200
  @response_headers = { "Content-Type" => "text/html" }
  @response_body = []
  @halted = false
  @halted_response = nil
  @params_hybrid = nil

  # Merge route params, query params, and body params
  # Convert all keys to symbols for consistency
  route_params_sym = route_params.transform_keys(&:to_sym)
  query_params_sym = query_params.transform_keys(&:to_sym)
  body_params_sym = body_params.transform_keys(&:to_sym)
  @params = route_params_sym.merge(query_params_sym).merge(body_params_sym)

  # Handle multipart form data (file uploads)
  content_type_header = @env["CONTENT_TYPE"] || ""
  return unless content_type_header.include?("multipart/form-data")

  # Rack::Request handles multipart automatically
  multipart_params = @request.params
  multipart_params_sym = multipart_params.transform_keys(&:to_sym)
  @params.merge!(multipart_params_sym)
end

Instance Attribute Details

#current_sessionObject

Returns the value of attribute current_session.



16
17
18
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 16

def current_session
  @current_session
end

#current_userObject

Returns the value of attribute current_user.



16
17
18
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 16

def current_user
  @current_user
end

#envObject (readonly)

Returns the value of attribute env.



15
16
17
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 15

def env
  @env
end

#halted_responseObject (readonly)

Returns the value of attribute halted_response.



15
16
17
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 15

def halted_response
  @halted_response
end

#requestObject (readonly)

Returns the value of attribute request.



15
16
17
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 15

def request
  @request
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



15
16
17
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 15

def response_body
  @response_body
end

#response_headersObject (readonly)

Returns the value of attribute response_headers.



15
16
17
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 15

def response_headers
  @response_headers
end

#response_statusObject (readonly)

Returns the value of attribute response_status.



15
16
17
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 15

def response_status
  @response_status
end

Instance Method Details

#body(str = nil) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 79

def body(str = nil)
  if str
    @response_body = [str.to_s]
    str
  else
    @response_body
  end
end

#content_type(type) ⇒ Object



71
72
73
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 71

def content_type(type)
  @response_headers["Content-Type"] = type
end

#cookiesObject



133
134
135
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 133

def cookies
  @request.cookies
end

#halt(status_code, body = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 93

def halt(status_code, body = nil)
  @halted = true
  @response_status = status_code
  if body
    content_type "application/json" if @response_headers["Content-Type"] == "text/html"
    @halted_response = [status_code, @response_headers.dup, [body.to_s]]
  else
    @halted_response = [status_code, @response_headers.dup, []]
  end
end

#halted?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 104

def halted?
  @halted
end

#headersObject



75
76
77
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 75

def headers
  @response_headers
end

#json(obj) ⇒ Object



88
89
90
91
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 88

def json(obj)
  content_type "application/json"
  body(obj.to_json)
end

#paramsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 46

def params
  # Return params with support for both symbol and string key access
  @params ||= begin
    hash = @params.dup
    # Add string-key versions for all symbol keys
    @params.each do |k, v|
      hash[k.to_s] = v if k.is_a?(Symbol)
    end
    # Add symbol-key versions for all string keys
    hash.to_a.each do |k, v|
      hash[k.to_sym] = v if k.is_a?(String) && !hash.key?(k.to_sym)
    end
    # Create accessor that checks both
    def hash.[](key)
      super(key.to_sym) || super(key.to_s) || super
    end
    hash
  end
end

#path_infoObject



129
130
131
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 129

def path_info
  @request.path_info
end

#script_nameObject



125
126
127
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 125

def script_name
  @request.script_name
end

#send_file(filepath) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 108

def send_file(filepath)
  return unless File.exist?(filepath)

  content = File.read(filepath)
  ext = File.extname(filepath).downcase
  mime_types = {
    ".css" => "text/css",
    ".js" => "application/javascript",
    ".html" => "text/html",
    ".json" => "application/json",
    ".xml" => "application/xml",
    ".svg" => "image/svg+xml"
  }
  content_type(mime_types[ext] || "application/octet-stream")
  body(content)
end

#status(code) ⇒ Object



66
67
68
69
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 66

def status(code)
  @response_status = code
  code
end

#to_rack_responseObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/decision_agent/web/rack_request_helpers.rb', line 137

def to_rack_response
  if @halted && @halted_response
    @halted_response
  else
    # Ensure body is an array
    body_array = @response_body.is_a?(Array) ? @response_body : [@response_body.to_s]
    body_array = [""] if body_array.empty?
    [@response_status, @response_headers.dup, body_array]
  end
end