Sha256: 3fce15657c24fca696a849862e327fae0302ccc833de366a523c08483ef715d0

Contents?: true

Size: 930 Bytes

Versions: 1

Compression:

Stored size: 930 Bytes

Contents

require 'forwardable'
require 'rack/request'

module Hi
  # Wrapper for rack request that exposes headers and body string
  class Request
    extend Forwardable

    ATTRIBUTES = [
      :host,
      :ip,
      :port,
      :request_method,
      :scheme,
      :url,
      :query_string,
      :body,
      :content_length,
      :media_type,
      :referer,
      :user_agent,
      :xhr?
    ]

    def_delegators :request, *ATTRIBUTES
    attr_reader :env, :request

    def initialize(env)
      @env = env
      @request = Rack::Request.new(env)
    end

    def headers
      env.select { |key| key.start_with? 'HTTP_' }
    end

    def body_string
      body.string if body
    end

    def to_h
      request_hash.merge({
        body: body_string,
        headers: headers,
      })
    end

    private

    def request_hash
      ATTRIBUTES.inject({}) { |hash, attr| hash[attr] = send(attr); hash }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hi-1.2.2 lib/hi/request.rb