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::FRAGMENT, Constants::GOLIATH_ENV, Constants::HTTP_PREFIX, Constants::HTTP_VERSION, Constants::INITIAL_BODY, Constants::LOCALHOST, Constants::LOGGER, Constants::OPTIONS, Constants::PATH_INFO, Constants::QUERY_STRING, Constants::RACK_ERRORS, Constants::RACK_INPUT, Constants::RACK_MULTIPROCESS, Constants::RACK_MULTITHREAD, Constants::RACK_RUN_ONCE, 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

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



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

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

#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



72
73
74
75
76
# File 'lib/goliath/env.rb', line 72

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_closeObject

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

#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.d



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