Module: Jets::Controller::Params

Extended by:
Memoist
Included in:
Base
Defined in:
lib/jets/controller/params.rb

Instance Method Summary collapse

Instance Method Details

#body_paramsObject Also known as: body_parameters



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/jets/controller/params.rb', line 61

def body_params
  body = event['isBase64Encoded'] ? base64_decode(event["body"]) : event["body"]
  return {} if body.nil?

  parsed_json = parse_json(body)
  return parsed_json if parsed_json

  headers = event["headers"] || {}
  # API Gateway seems to use either: content-type or Content-Type
  headers = headers.transform_keys { |key| key.downcase }
  content_type = headers["content-type"]

  if content_type.to_s.include?("application/x-www-form-urlencoded")
    return ::Rack::Utils.parse_nested_query(body)
  elsif content_type.to_s.include?("multipart/form-data")
    return parse_multipart(body)
  end

  {} # fallback to empty Hash
end

#filtered_parameters(**kwargs) ⇒ Object



38
39
40
# File 'lib/jets/controller/params.rb', line 38

def filtered_parameters(**kwargs)
  parameter_filter.filter params(**kwargs, raw: true) # Always filter raw hash
end

#params(raw: false, path_parameters: true, body_parameters: true) ⇒ Object

Merge all the parameters together for convenience. Users still have access via events.

Precedence:

1. path parameters have highest precdence
2. query string parameters
3. body parameters


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jets/controller/params.rb', line 16

def params(raw: false, path_parameters: true, body_parameters: true)
  params = {}
  params = params.deep_merge(body_params) if body_parameters
  params = params.deep_merge(unescape_recursively(query_params)) # always
  params = params.deep_merge(path_params) if path_parameters

  if raw
    params
  else
    params = ActionDispatch::Request::Utils.normalize_encode_params(params) # for file uploads
    ActionController::Parameters.new(params)
  end
end

#path_paramsObject Also known as: path_parameters



42
43
44
45
# File 'lib/jets/controller/params.rb', line 42

def path_params
  path_params = event["pathParameters"] || {}
  path_params = path_params.map { |k, path| [k, CGI.unescape(path)] }.to_h
end

#query_paramsObject Also known as: query_parameters



48
49
50
# File 'lib/jets/controller/params.rb', line 48

def query_params
  event["queryStringParameters"] || {}
end

#request_paramsObject Also known as: request_parameters



53
54
55
56
57
58
# File 'lib/jets/controller/params.rb', line 53

def request_params
  {
    controller: controller_name,
    action: action_name,
  }
end

#unescape_recursively(obj) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/jets/controller/params.rb', line 30

def unescape_recursively(obj)
  case obj
  when Hash then obj.map { |k, v| [k, unescape_recursively(v)] }.to_h
  when Array then obj.map { |v| unescape_recursively(v) }
  else CGI.unescape(obj.to_s)
  end
end