Class: MelissaData::Env

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

Overview

MelissaData::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::DEFAULT_SOCKET_FILE, Constants::DEFAULT_TCP_ADDRESS, Constants::DEFAULT_TCP_PORT, Constants::DEFAULT_VAGRANT_TCP_PORT

Instance Method Summary collapse

Constructor Details

#initializeMelissaData::Env

Create a new MelissaData::Env object



10
11
12
13
14
# File 'lib/melissadata/env.rb', line 10

def initialize
  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 MelissaData::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/melissadata/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

#loggerLogger

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

Returns:

  • (Logger)

    The logger object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/melissadata/env.rb', line 46

def logger
  return @logger if @logger

  # Figure out where the output should go to.
  output = nil
  if ENV["MELISSADATA_LOG"] == "STDOUT"
    output = STDOUT
  elsif ENV["MELISSADATA_LOG"] == "NULL"
    output = nil
  elsif ENV["MELISSADATA_LOG"]
    output = ENV["MELISSADATA_LOG"]
  else
    output = nil #log_path.join("#{Time.now.to_i}.log")
  end

  # Create the logger and custom formatter
  @logger = Logger.new(output)
  @logger.formatter = Proc.new do |severity, datetime, progname, msg|
    "#{datetime} - #{progname} - [#{resource}] #{msg}\n"
  end

  @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



72
73
74
75
76
# File 'lib/melissadata/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

#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 MelissaData::Env object was initialized.

Examples:

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

Parameters:

  • name (String)

    The name of the trace to add



26
27
28
29
# File 'lib/melissadata/env.rb', line 26

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.



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

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