Sha256: a4009842f2bcff2e6b29656b6a3002d6d00a533989e65e683eba715f77467319

Contents?: true

Size: 1.48 KB

Versions: 18

Compression:

Stored size: 1.48 KB

Contents

require "action_controller/metal/strong_parameters"

class Jets::Controller
  module Params
    # 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
    def params(raw: false, path_parameters: true)
      query_string_params = event["queryStringParameters"] || {}
      path_params = event["pathParameters"] || {}
      params = body_params
                .deep_merge(query_string_params)
      params = params.deep_merge(path_params) if path_parameters

      if raw
        params
      else
        ActionController::Parameters.new(params)
      end
    end

  private
    def body_params
      body = event["body"]
      return {} if body.nil?

      # Try json parsing
      parsed_json = parse_json(body)
      return parsed_json if parsed_json


      # For content-type application/x-www-form-urlencoded CGI.parse the body
      headers = event["headers"] || {}
      headers = headers.transform_keys { |key| key.downcase }
      # API Gateway seems to use either: content-type or Content-Type
      content_type = headers["content-type"]
      if content_type.to_s.include?("application/x-www-form-urlencoded")
        return Rack::Utils.parse_nested_query(body)
      end

      {} # fallback to empty Hash
    end

    def parse_json(text)
      JSON.parse(text)
    rescue JSON::ParserError
      nil
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
jets-1.0.18 lib/jets/controller/params.rb
jets-1.0.17 lib/jets/controller/params.rb
jets-1.0.16 lib/jets/controller/params.rb
jets-1.0.15 lib/jets/controller/params.rb
jets-1.0.13 lib/jets/controller/params.rb
jets-1.0.12 lib/jets/controller/params.rb
jets-1.0.11 lib/jets/controller/params.rb
jets-1.0.10 lib/jets/controller/params.rb
jets-1.0.9 lib/jets/controller/params.rb
jets-1.0.8 lib/jets/controller/params.rb
jets-1.0.7 lib/jets/controller/params.rb
jets-1.0.6 lib/jets/controller/params.rb
jets-1.0.5 lib/jets/controller/params.rb
jets-1.0.4 lib/jets/controller/params.rb
jets-1.0.3 lib/jets/controller/params.rb
jets-1.0.2 lib/jets/controller/params.rb
jets-1.0.1 lib/jets/controller/params.rb
jets-1.0.0 lib/jets/controller/params.rb