Sha256: 2a00e1f9f26aa246be396ba74e62aa592829235065a69ed0ec3c3bd456d93c87

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module UmbrellioUtils
  class RequestWrapper
    include Memery

    def initialize(request, remove_xml_attributes: true)
      self.request = request
      self.remove_xml_attributes = remove_xml_attributes
    end

    memoize def params
      parse_params
    end

    memoize def body
      request.body.read.dup.force_encoding("utf-8")
    end

    def [](key)
      params[key]
    end

    def rails_params
      request.params
    end

    def raw_request
      request
    end

    memoize def http_headers
      headers = request.headers.select do |key, _value|
        key.start_with?("HTTP_") || key.in?(ActionDispatch::Http::Headers::CGI_VARIABLES)
      end

      HTTP::Headers.coerce(headers.sort)
    end

    memoize def path_parameters
      request.path_parameters.except(:controller, :action).stringify_keys
    end

    def headers
      request.headers
    end

    def ip
      request.ip
    end

    private

    attr_accessor :request, :remove_xml_attributes

    def parse_params
      case request.media_type
      when "application/json", /\+json\z/
        Parsing.safely_parse_json(body)
      when "application/xml"
        Parsing.parse_xml(body, remove_attributes: remove_xml_attributes)
      else
        request.get? ? request.GET : request.POST
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
umbrellio-utils-1.0.0 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.7.5 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.7.4 lib/umbrellio_utils/request_wrapper.rb