Class: Goliath::Env
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
ASYNC_BODY, ASYNC_CALLBACK, ASYNC_CLOSE, ASYNC_HEADERS, CONFIG, CONNECTION, CONTENT_LENGTH, CONTENT_TYPE, FRAGMENT, GOLIATH_ENV, HTTP_PREFIX, HTTP_VERSION, INITIAL_BODY, LOCALHOST, OPTIONS, PATH_INFO, QUERY_STRING, RACK_ERRORS, RACK_INPUT, RACK_LOGGER, RACK_MULTIPROCESS, RACK_MULTITHREAD, RACK_RUN_ONCE, RACK_VERSION, RACK_VERSION_NUM, REMOTE_ADDR, REQUEST_METHOD, REQUEST_PATH, REQUEST_URI, SCRIPT_NAME, SERVER, SERVER_NAME, SERVER_PORT, SERVER_SOFTWARE, STATUS, STREAM_CLOSE, STREAM_SEND, STREAM_START
Instance Method Summary (collapse)
-
- (Goliath::Env) initialize
constructor
Create a new Goliath::Env object.
-
- (Logger) logger
Convenience method for accessing the rack.logger item in the environment.
-
- (Object) method_missing(name, *args, &blk)
The Goliath::Env will provide any of it’s keys as a method.
-
- (Boolean) respond_to?(name)
True if the Env responds to the method, false otherwise.
-
- (Object) stream_close
If the API is a streaming API this will be executed by the API to signal that the stream is complete.
-
- (Object) stream_send(data)
If the API is a streaming API this will send the provided data to the client.
-
- (Object) trace(name)
Add a trace timer with the given name into the environment.
-
- (Array) trace_stats
Retrieve the tracer stats for this request environment.
Constructor Details
- (Goliath::Env) initialize
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
- (Object) method_missing(name, *args, &blk)
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.
93 94 95 96 97 |
# File 'lib/goliath/env.rb', line 93 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
- (Logger) logger
Convenience method for accessing the rack.logger item in the environment.
73 74 75 |
# File 'lib/goliath/env.rb', line 73 def logger self[RACK_LOGGER] end |
- (Boolean) respond_to?(name)
True if the Env responds to the method, false otherwise
79 80 81 82 83 |
# File 'lib/goliath/env.rb', line 79 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 |
- (Object) stream_close
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 self[STREAM_CLOSE].call end |
- (Object) stream_send(data)
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.
60 61 62 |
# File 'lib/goliath/env.rb', line 60 def stream_send(data) self[STREAM_SEND].call(data) end |
- (Object) trace(name)
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.
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 |
- (Array) trace_stats
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.
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 |