Class: Goliath::Env

Inherits:
Hash
  • Object
show all
Includes:
Constants
Defined in:
lib/goliath/env.rb

Overview

Holds information for the current request.

Goliath::Env also provides access to the logger, configuration information and anything else set into the config data during initialization.

Constant Summary

Constants included from Constants

Constants::ASYNC_BODY, Constants::ASYNC_CALLBACK, Constants::ASYNC_CLOSE, Constants::ASYNC_HEADERS, Constants::CONFIG, Constants::CONNECTION, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::FRAGMENT, Constants::GOLIATH_ENV, Constants::GOLIATH_SIGNATURE, Constants::HTTP_PREFIX, Constants::HTTP_VERSION, Constants::INITIAL_BODY, Constants::LOCALHOST, Constants::OPTIONS, Constants::PATH_INFO, Constants::QUERY_STRING, Constants::RACK_ERRORS, Constants::RACK_EXCEPTION, Constants::RACK_INPUT, Constants::RACK_LOGGER, Constants::RACK_MULTIPROCESS, Constants::RACK_MULTITHREAD, Constants::RACK_RUN_ONCE, Constants::RACK_URL_SCHEME, Constants::RACK_VERSION, Constants::RACK_VERSION_NUM, Constants::REMOTE_ADDR, Constants::REQUEST_METHOD, Constants::REQUEST_PATH, Constants::REQUEST_URI, Constants::SCRIPT_NAME, Constants::SERVER, Constants::SERVER_NAME, Constants::SERVER_PORT, Constants::SERVER_SOFTWARE, Constants::STATUS, Constants::STREAM_CLOSE, Constants::STREAM_SEND, Constants::STREAM_START, Constants::UPGRADE_DATA

Instance Method Summary collapse

Constructor Details

#initializeGoliath::Env

Create a new Goliath::Env object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/goliath/env.rb', line 14

def initialize
  self[SERVER_SOFTWARE]   = SERVER
  self[SERVER_NAME]       = LOCALHOST
  self[RACK_VERSION]      = RACK_VERSION_NUM
  self[RACK_ERRORS]       = STDERR
  self[RACK_MULTITHREAD]  = false
  self[RACK_MULTIPROCESS] = false
  self[RACK_RUN_ONCE]     = false

  self[:start_time] = Time.now.to_f
  self[:time] = Time.now.to_f
  self[:trace] = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object

The Goliath::Env will provide any of it’s keys as a method. It will also provide any of the keys in the config object as methods. The methods will return the value of the key. If the key doesn’t exist in either hash this will fall back to the standard method_missing implementation.

Parameters:

  • name (Symbol)

    The method to look for

  • args

    The arguments

  • blk

    A block



136
137
138
139
140
# File 'lib/goliath/env.rb', line 136

def method_missing(name, *args, &blk)
  return self[name.to_s] if has_key?(name.to_s)
  return self['config'][name.to_s] if self['config'] && self['config'].has_key?(name.to_s)
  super(name, *args, &blk)
end

Instance Method Details

#chunked_stream_closeObject

Sends the terminating chunk in a chunked transfer encoding stream, and closes the stream.

The last chunk is a zero-length chunk, with the chunk size coded as 0, but
without any chunk data section.  The final chunk may be followed by an
optional trailer of additional entity header fields that are normally
delivered in the HTTP header to allow the delivery of data that can only
be computed after all chunk data has been generated. The sender may
indicate in a Trailer header field which additional fields it will send
in the trailer after the chunks.

Note: trailer headers aren’t provided yet



104
105
106
107
# File 'lib/goliath/env.rb', line 104

def chunked_stream_close
  stream_send([0, "\r\n", "\r\n"].join)
  stream_close
end

#chunked_stream_send(chunk) ⇒ Object

Sends a chunk in a Chunked transfer encoding stream.

Each chunk starts with the number of octets of the data it embeds expressed
in hexadecimal followed by optional parameters (chunk extension) and a
terminating CRLF (carriage return and line feed) sequence, followed by the
chunk data. The chunk is terminated by CRLF. If chunk extensions are
provided, the chunk size is terminated by a semicolon followed with the
extension name and an optional equal sign and value

Note: chunk extensions aren’t provided yet

This will do nothing if the chunk is empty – sending a zero-length chunk signals the end of a stream.



84
85
86
87
88
89
# File 'lib/goliath/env.rb', line 84

def chunked_stream_send(chunk)
  return if chunk.empty?
  chunk_len_in_hex = chunk.bytesize.to_s(16)
  body = [chunk_len_in_hex, "\r\n", chunk, "\r\n"].join
  stream_send(body)
end

#defer_stackObject



124
125
126
# File 'lib/goliath/env.rb', line 124

def defer_stack
  @defer_stack ||= []
end

#loggerLogger

Convenience method for accessing the rack.logger item in the environment.

Returns:

  • (Logger)

    The logger object



112
113
114
# File 'lib/goliath/env.rb', line 112

def logger
  self[RACK_LOGGER]
end

#respond_to?(name) ⇒ Boolean

Returns True if the Env responds to the method, false otherwise.

Parameters:

  • name (Symbol)

    The method to check if we respond to it.

Returns:

  • (Boolean)

    True if the Env responds to the method, false otherwise



118
119
120
121
122
# File 'lib/goliath/env.rb', line 118

def respond_to?(name)
  return true if has_key?(name.to_s)
  return true if self['config'] && self['config'].has_key?(name.to_s)
  super
end

#stream_close(*args) ⇒ Object

If the API is a streaming API this will be executed by the API to signal that the stream is complete. This will close the connection with the client.



66
67
68
# File 'lib/goliath/env.rb', line 66

def stream_close(*args)
  self[STREAM_CLOSE].call(args)
end

#stream_send(data) ⇒ Object

If the API is a streaming API this will send the provided data to the client. There will be no processing done on the data when this is called so it’s the APIs responsibility to have the data formatted as needed.

Parameters:

  • data (String)

    The data to send to the client.



60
61
62
# File 'lib/goliath/env.rb', line 60

def stream_send(data)
  self[STREAM_SEND].call(data)
end

#trace(name) ⇒ Object

Add a trace timer with the given name into the environment. The tracer will provide information on the amount of time since the previous call to #trace or since the Goliath::Env object was initialized.

Examples:

trace("initialize hash")
....
trace("Do something else")

Parameters:

  • name (String)

    The name of the trace to add



38
39
40
41
# File 'lib/goliath/env.rb', line 38

def trace(name)
  self[:trace].push([name, "%.2f" % ((Time.now.to_f - self[:time]) * 1000)])
  self[:time] = Time.now.to_f
end

#trace_statsArray

Retrieve the tracer stats for this request environment. This can then be returned in the headers hash to in development to provide some simple timing information for the various API components.

Examples:

[200, {}, {:meta => {:trace => env.trace_stats}}, {}]

Returns:

  • (Array)

    Array of [name, time] pairs with a Total entry added.



51
52
53
# File 'lib/goliath/env.rb', line 51

def trace_stats
  self[:trace] + [['total', self[:trace].collect { |s| s[1].to_f }.inject(:+).to_s]]
end