Sha256: 67063cc05af6587c533e95a277bff9e3828c6ea35bf6ef698ab00f06b315472d

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module UmbrellioUtils
  class RequestWrapper
    include Memery

    def initialize(request)
      self.request = request
    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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
umbrellio-utils-0.7.1 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.7.0 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.6.2 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.6.1 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.6.0 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.5.2 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.5.1 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.5.0 lib/umbrellio_utils/request_wrapper.rb
umbrellio-utils-0.4.3 lib/umbrellio_utils/request_wrapper.rb