Sha256: bc4d3889a5d1e634ec0995b1f096d186a66976f91e8cd6c7d48b52acdb47e83f

Contents?: true

Size: 952 Bytes

Versions: 1

Compression:

Stored size: 952 Bytes

Contents

# frozen_string_literal: true

module Umbra
  class Encoder
    def self.call(env)
      new(env).call
    end

    def initialize(env)
      @env = env
    end

    def call
      @call ||= Pb::Message.new(
        method: rack_request.request_method,
        url: rack_request.url,
        body: request_body,
        headers: request_headers
      ).to_proto
    end

    def ignored_headers
      []
    end

    private

    def rack_request
      @rack_request ||= Rack::Request.new(@env)
    end

    def request_headers
      @request_headers ||=
        @env
          .select { |k, _| k.start_with?("HTTP_") && !ignored_headers.include?(k) }
          .merge(HEADER_KEY => HEADER_VALUE)
          .transform_keys { |k| to_http_header(k) }
    end

    def to_http_header(rack_header)
      rack_header.delete_prefix("HTTP_").downcase.split("_").join("-")
    end

    def request_body
      @env.fetch("umbra.request_body")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
umbra-rb-0.3.0 lib/umbra/encoder.rb