class Rack::Shelf::EnvironmentBuilder

Builds up a Rack env hash.

Constants

DEFAULTS

Default values for the Rack environment.

Public Class Methods

new() click to toggle source

Creates the builder with reasonable defaults.

# File lib/rack/shelf/environment_builder.rb, line 30
def initialize
  @env = DEFAULTS.clone(freeze: false)
end

Public Instance Methods

application(prefix, name, value) click to toggle source

Defines a custom application value. @param prefix [String] Prefix of the key. @param name [String] Name of the application key. @param value [Object] Value of the key. @return [void] @raise [ArgumentError] The prefix can't be rack.

# File lib/rack/shelf/environment_builder.rb, line 144
def application(prefix, name, value)
  raise ArgumentError, "Prefix can't be 'rack'" if prefix == 'rack'

  key = [prefix, name].join('.')
  @env[key] = value
end
base64_body(content) click to toggle source

Specifies the client request body encoded in base-64. @param content [String] Base-64 encoded request body. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 110
def base64_body(content)
  decoded = Base64.decode64(content)
  body(decoded)
end
body(content) click to toggle source

Specifies the client request body. @param content [String] Request body. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 102
def body(content)
  io = StringIO.new(content)
  input_stream(io)
end
build() click to toggle source

Creates the Rack env hash. @return [Hash]

# File lib/rack/shelf/environment_builder.rb, line 153
def build
  @env.clone
end
error_stream(io) click to toggle source

Specifies the stream used to display errors. @param io [IO] Error stream. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 118
def error_stream(io)
  @env['rack.errors'] = io
end
header(header, value) click to toggle source

Defines an HTTP header in the request. @param header [String] HTTP header name. @param value [String] Value of the HTTP header. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 126
def header(header, value)
  name = header.upcase.gsub('-', '_')
  key  = case name
         when 'CONTENT_TYPE', 'CONTENT_LENGTH'
           name
         else
           'HTTP_' + name
         end

  @env[key] = value
end
input_stream(io) click to toggle source

Specifies the stream used for input (request body). @param io [IO] Input stream. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 95
def input_stream(io)
  @env['rack.input'] = io
end
path_info(path) click to toggle source

Specifies the path info (path after the mounting point). @param path [String] Path info. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 52
def path_info(path)
  @env['PATH_INFO'] = path
end
query_params(params) click to toggle source

Specifies the query parameters as a hash. @param params [Hash] Query parameters. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 66
def query_params(params)
  string = Rack::Utils.build_query(params)
  query_string(string)
end
query_string(string) click to toggle source

Specifies the entire query string. @param string [String] Pre-encoded query string. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 59
def query_string(string)
  @env['QUERY_STRING'] = string
end
request_method(method) click to toggle source

Specifies the request method. @param method [String] Must be one of:

+GET+, +POST+, +PUT+, +HEAD+, +DELETE+, +PATCH+, or +OPTIONS+.

@return [void]

# File lib/rack/shelf/environment_builder.rb, line 38
def request_method(method)
  @env['REQUEST_METHOD'] = method
end
script_name(name) click to toggle source

Specifies the name (or mounting point) of the application. @param name [String] Script name. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 45
def script_name(name)
  @env['SCRIPT_NAME'] = name
end
server_name(name) click to toggle source

Specifies the hostname of the server. @param name [String] Server name. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 74
def server_name(name)
  @env['SERVER_NAME'] = name
end
server_port(port) click to toggle source

Specifies the port the server is listening on. @param port [#to_s] Port number. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 81
def server_port(port)
  @env['SERVER_PORT'] = port.to_s
end
url_scheme(scheme) click to toggle source

Specifies the URL scheme for the request. @param scheme [String] Must be: http or https. @return [void]

# File lib/rack/shelf/environment_builder.rb, line 88
def url_scheme(scheme)
  @env['rack.url_scheme'] = scheme
end