Permalink
Browse files

moved env extract method to its own module

  • Loading branch information...
1 parent a47cbf6 commit f167ff9545829e39370464b3df1dbb8bc69995d8 @kornypoet kornypoet committed Feb 18, 2014
View
@@ -6,6 +6,7 @@
require 'goliath/chimp/plugins/activity_monitor'
+require 'goliath/chimp/rack/env_extractor'
require 'goliath/chimp/rack/api_version'
require 'goliath/chimp/rack/control_methods'
require 'goliath/chimp/rack/force_content_type'
@@ -0,0 +1,26 @@
+module Goliath::Chimp
+ module Rack
+ module EnvExtractor
+
+ # Helper method for extracting potentially nested
+ # values from the env hash. Also normalizes
+ # string vs. symbol access.
+ #
+ def extract_from_env(env, key, default = nil)
+ return default unless env.is_a? Hash
+ case key
+ when String, Symbol
+ env[key.to_s] || env[key.to_sym] || default
+ when Array
+ slice = env[key.shift]
+ key.empty? ? slice : extract_from_env(slice, key)
+ when Hash
+ extract_from_env(env, key.to_a.flatten)
+ else
+ default
+ end
+ end
+
+ end
+ end
+end
@@ -2,6 +2,7 @@ module Goliath::Chimp
module Rack
class ServerMetrics
include Goliath::Rack::AsyncMiddleware
+ include EnvExtractor
attr_reader :path, :env_key, :default
@@ -14,31 +15,16 @@ def initialize(app, options = {})
def call env
if env['PATH_INFO'] == path
- [ 200, {}, env.status ]
+ [ 200, {}, env['status'] ]
else
- super env
- end
- end
-
- def extract_from_env(env, key)
- return nil unless env.is_a? Hash
- case key
- when String, Symbol
- env[key]
- when Array
- slice = env[key.shift]
- key.empty? ? slice : extract_from_env(slice, key)
- when Hash
- extract_from_env(env, key.to_a.flatten)
- else
- nil
+ super
end
end
def post_process(env, status, headers, body)
base_metrics = { count: 0, total_millis: 0 }
- env.status[:requests] ||= Hash.new{ |h, k| h[k] = Hash.new{ |h, k| h[k] = base_metrics } }
- request_key = extract_from_env(env, env_key) || default
+ env['status'][:requests] ||= Hash.new{ |h, k| h[k] = Hash.new{ |h, k| h[k] = base_metrics } }
+ request_key = extract_from_env(env, env_key, default)
request_method = env['REQUEST_METHOD'].downcase.to_sym
metrics = env.status[:requests][request_key][request_method]
metrics[:count] += 1
@@ -0,0 +1,66 @@
+require 'spec_helper'
+
+describe Goliath::Chimp::Rack::EnvExtractor do
+
+ subject do
+ extractor_mod = described_class
+ example_class = Class.new{ include extractor_mod }
+ example_class.new
+ end
+
+ context '#extract_from_env' do
+ context 'default' do
+ let(:env){ Hash.new }
+
+ it 'returns the default value when key is not found' do
+ subject.extract_from_env(env, 'foo', 'default').should eq('default')
+ end
+ end
+
+ context String do
+ let(:env){ { 'foo' => 'bar' } }
+
+ it 'extracts the value using the string as a key' do
+ subject.extract_from_env(env, 'foo').should eq('bar')
+ subject.extract_from_env(env, 'bar').should be_nil
+ end
+
+ it 'extracts symbolic values' do
+ env = { foo: 'bar' }
+ subject.extract_from_env(env, 'foo').should eq('bar')
+ end
+ end
+
+ context Symbol do
+ let(:env){ { foo: 'bar' } }
+
+ it 'extracts the value using the symbol as a key' do
+ subject.extract_from_env(env, :foo).should eq('bar')
+ subject.extract_from_env(env, :bar).should be_nil
+ end
+
+ it 'extracts string values' do
+ env = { 'foo' => 'bar' }
+ subject.extract_from_env(env, :foo).should eq('bar')
+ end
+ end
+
+ context Array do
+ let(:env){ { foo: { bar: 'baz' } } }
+
+ it 'extracts the value using the array as a set of keys' do
+ subject.extract_from_env(env, [:foo, :bar]).should eq('baz')
+ subject.extract_from_env(env, [:bar, :foo]).should be_nil
+ end
+ end
+
+ context Hash do
+ let(:env){ { foo: { bar: 'baz' } } }
+
+ it 'extracts the value using the hash as a set of keys' do
+ subject.extract_from_env(env, { foo: :bar }).should eq('baz')
+ subject.extract_from_env(env, { bar: :foo }).should be_nil
+ end
+ end
+ end
+end

0 comments on commit f167ff9

Please sign in to comment.