Skip to content

Commit 07c8213

Browse files
committed
Birthday
0 parents  commit 07c8213

18 files changed

+715
-0
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.bundle/
2+
.envrc
3+
*.sw?
4+
errors.err
5+
last_run
6+
loop/
7+
log/
8+
corundum/

.rspec

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--format documentation
2+
--out last_run
3+
--color
4+
--format documentation
5+
-I spec_help/interpose
6+
-I lib
7+
-I spec_help
8+
--require simplecov
9+
--require spec_helper
10+
--pattern *.rb

.simplecov

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SimpleCov.start do
2+
coverage_dir "corundum/docs/coverage"
3+
add_filter "./spec"
4+
add_filter "/vendor/bundle/"
5+
end

.travis.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: ruby
2+
script: bundle exec rake ci

Gemfile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source "https://rubygems.org"
2+
3+
gem 'rspec', "< 3.0"
4+
gem 'cadre'
5+
gem 'rack'
6+
gem 'corundum'
7+
gem 'stack_loop'
8+
gem 'fuubar'
9+
gem 'rails', "~> 4.0.0"
10+
gemspec :name => "fluoride-collector" #points to default 'gem.gemspec'

Gemfile.lock

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
PATH
2+
remote: .
3+
specs:
4+
fluoride-collector (0.0.1)
5+
6+
GEM
7+
remote: https://rubygems.org/
8+
specs:
9+
actionmailer (4.0.5)
10+
actionpack (= 4.0.5)
11+
mail (~> 2.5.4)
12+
actionpack (4.0.5)
13+
activesupport (= 4.0.5)
14+
builder (~> 3.1.0)
15+
erubis (~> 2.7.0)
16+
rack (~> 1.5.2)
17+
rack-test (~> 0.6.2)
18+
activemodel (4.0.5)
19+
activesupport (= 4.0.5)
20+
builder (~> 3.1.0)
21+
activerecord (4.0.5)
22+
activemodel (= 4.0.5)
23+
activerecord-deprecated_finders (~> 1.0.2)
24+
activesupport (= 4.0.5)
25+
arel (~> 4.0.0)
26+
activerecord-deprecated_finders (1.0.3)
27+
activesupport (4.0.5)
28+
i18n (~> 0.6, >= 0.6.9)
29+
minitest (~> 4.2)
30+
multi_json (~> 1.3)
31+
thread_safe (~> 0.1)
32+
tzinfo (~> 0.3.37)
33+
arel (4.0.2)
34+
builder (3.1.4)
35+
cadre (0.2.3)
36+
thor (>= 0.18.1, < 1.0)
37+
tilt (~> 1.0)
38+
valise (~> 1.0)
39+
chunky_png (1.3.1)
40+
compass (0.12.6)
41+
chunky_png (~> 1.2)
42+
fssm (>= 0.2.7)
43+
sass (~> 3.2.19)
44+
corundum (0.3.6)
45+
bundler
46+
compass (>= 0.12.1)
47+
mailfactory (~> 1.4.0)
48+
mattock (~> 0.7.1)
49+
nokogiri
50+
paint (~> 0.8.7)
51+
rdoc
52+
rspec (>= 2.0)
53+
simplecov (>= 0.5.4)
54+
yard
55+
diff-lcs (1.2.5)
56+
docile (1.1.3)
57+
erubis (2.7.0)
58+
fssm (0.2.10)
59+
fuubar (1.3.3)
60+
rspec (>= 2.14.0, < 3.1.0)
61+
ruby-progressbar (~> 1.4)
62+
hike (1.2.3)
63+
i18n (0.6.9)
64+
json (1.8.1)
65+
mail (2.5.4)
66+
mime-types (~> 1.16)
67+
treetop (~> 1.4.8)
68+
mailfactory (1.4.0)
69+
mime-types (>= 1.13.1)
70+
mattock (0.7.1)
71+
rake (~> 10.0)
72+
tilt (> 0)
73+
valise (~> 1.1.1)
74+
mime-types (1.25.1)
75+
mini_portile (0.6.0)
76+
minitest (4.7.5)
77+
multi_json (1.10.1)
78+
nokogiri (1.6.2.1)
79+
mini_portile (= 0.6.0)
80+
paint (0.8.7)
81+
polyglot (0.3.5)
82+
rack (1.5.2)
83+
rack-test (0.6.2)
84+
rack (>= 1.0)
85+
rails (4.0.5)
86+
actionmailer (= 4.0.5)
87+
actionpack (= 4.0.5)
88+
activerecord (= 4.0.5)
89+
activesupport (= 4.0.5)
90+
bundler (>= 1.3.0, < 2.0)
91+
railties (= 4.0.5)
92+
sprockets-rails (~> 2.0.0)
93+
railties (4.0.5)
94+
actionpack (= 4.0.5)
95+
activesupport (= 4.0.5)
96+
rake (>= 0.8.7)
97+
thor (>= 0.18.1, < 2.0)
98+
rake (10.3.2)
99+
rdoc (4.1.1)
100+
json (~> 1.4)
101+
rspec (2.99.0)
102+
rspec-core (~> 2.99.0)
103+
rspec-expectations (~> 2.99.0)
104+
rspec-mocks (~> 2.99.0)
105+
rspec-core (2.99.0)
106+
rspec-expectations (2.99.0)
107+
diff-lcs (>= 1.1.3, < 2.0)
108+
rspec-mocks (2.99.0)
109+
ruby-progressbar (1.5.1)
110+
sass (3.2.19)
111+
simplecov (0.8.2)
112+
docile (~> 1.1.0)
113+
multi_json
114+
simplecov-html (~> 0.8.0)
115+
simplecov-html (0.8.0)
116+
sprockets (2.12.1)
117+
hike (~> 1.2)
118+
multi_json (~> 1.0)
119+
rack (~> 1.0)
120+
tilt (~> 1.1, != 1.3.0)
121+
sprockets-rails (2.0.1)
122+
actionpack (>= 3.0)
123+
activesupport (>= 3.0)
124+
sprockets (~> 2.8)
125+
stack_loop (0.1.0)
126+
thor (0.19.1)
127+
thread_safe (0.3.4)
128+
tilt (1.4.1)
129+
treetop (1.4.15)
130+
polyglot
131+
polyglot (>= 0.3.1)
132+
tzinfo (0.3.39)
133+
valise (1.1.1)
134+
yard (0.8.7.4)
135+
136+
PLATFORMS
137+
ruby
138+
139+
DEPENDENCIES
140+
cadre
141+
corundum
142+
fluoride-collector!
143+
fuubar
144+
rack
145+
rails (~> 4.0.0)
146+
rspec (< 3.0)
147+
stack_loop

Rakefile

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# vim: set ft=ruby :
2+
require 'corundum/tasklibs'
3+
4+
module Corundum
5+
Corundum::register_project(__FILE__)
6+
7+
core = Core.new
8+
9+
core.in_namespace do
10+
GemspecFiles.new(core)
11+
12+
#Also available: 'unfinished': TODO and XXX
13+
["debug", "profanity", "ableism", "racism"].each do |type|
14+
QuestionableContent.new(core) do |content|
15+
content.type = type
16+
end
17+
end
18+
rspec = RSpec.new(core)
19+
cov = SimpleCov.new(core, rspec) do |cov|
20+
cov.threshold = 70
21+
end
22+
23+
gem = GemBuilding.new(core)
24+
cutter = GemCutter.new(core,gem)
25+
email = Email.new(core)
26+
vc = Git.new(core) do |vc|
27+
vc.branch = "master"
28+
end
29+
30+
yd = YARDoc.new(core)
31+
32+
docs = DocumentationAssembly.new(core, yd, rspec, cov)
33+
34+
pages = GithubPages.new(docs)
35+
end
36+
end
37+
38+
task :default => [:release, :publish_docs]

fluoride-collector.gemspec

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Gem::Specification.new do |spec|
2+
spec.name = "fluoride-collector"
3+
spec.version = "0.0.1"
4+
author_list = {
5+
"Judson Lester" => '[email protected]'
6+
}
7+
spec.authors = author_list.keys
8+
spec.email = spec.authors.map {|name| author_list[name]}
9+
spec.summary = "Middleware to collect request and reponse pairs"
10+
spec.description = <<-EndDescription
11+
Part of the Fluoride suite - tools for making your black box a bit whiter
12+
EndDescription
13+
14+
spec.rubyforge_project= spec.name.downcase
15+
spec.homepage = "http://nyarly.github.com/#{spec.name.downcase}"
16+
spec.required_rubygems_version = Gem::Requirement.new(">= 0") if spec.respond_to? :required_rubygems_version=
17+
18+
# Do this: y$@"
19+
# !!find lib bin doc spec spec_help -not -regex '.*\.sw.' -type f 2>/dev/null
20+
spec.files = %w[
21+
]
22+
23+
spec.test_file = "spec_help/gem_test_suite.rb"
24+
spec.licenses = ["MIT"]
25+
spec.require_paths = %w[lib/]
26+
spec.rubygems_version = "1.3.5"
27+
28+
spec.has_rdoc = true
29+
spec.extra_rdoc_files = Dir.glob("doc/**/*")
30+
spec.rdoc_options = %w{--inline-source }
31+
spec.rdoc_options += %w{--main doc/README }
32+
spec.rdoc_options += ["--title", "#{spec.name}-#{spec.version} Documentation"]
33+
34+
#spec.add_dependency("", "> 0")
35+
36+
#spec.post_install_message = "Thanks for installing my gem!"
37+
end

lib/fluoride-collector.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'fluoride-collector/middleware'
2+
require 'fluoride-collector/rails' if defined?(Rails)

lib/fluoride-collector/middleware.rb

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
module Fluoride
2+
module Collector
3+
class Middleware
4+
def initialize(app, directory, tagging = nil)
5+
@app = app
6+
@directory = directory
7+
@tagging = tagging
8+
end
9+
10+
def call(env)
11+
@app.call(env).tap do |response|
12+
record_exchange(env, response)
13+
end
14+
rescue Object => ex
15+
record_exception(env, ex)
16+
raise
17+
end
18+
19+
private
20+
21+
def record_exchange(env, response)
22+
store(
23+
"type" => "normal_exchange",
24+
"tags" => @tagging,
25+
"request" => request_hash(env),
26+
"response" => response_hash(response)
27+
)
28+
end
29+
30+
def record_exception(env, ex)
31+
store(
32+
"type" => "exception_raised",
33+
"tags" => @tagging,
34+
"request" => request_hash(env),
35+
"response" => exception_hash(ex)
36+
)
37+
end
38+
39+
def request_hash(env)
40+
body = nil
41+
if env['rack.input'].respond_to? :read
42+
body = env['rack.input'].read
43+
env['rack.input'].rewind rescue nil
44+
end
45+
{
46+
"content_type" => env['CONTENT_TYPE'],
47+
"accept" => env["HTTP_ACCEPT_ENCODING"],
48+
"referer" => env["HTTP_REFERER"],
49+
"cookies" => env["HTTP_COOKIE"],
50+
"authorization" => env["HTTP_AUTHORIZATION"],
51+
"method" => env["REQUEST_METHOD"],
52+
"host" => env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}",
53+
"path" => env["SCRIPT_NAME"].to_s + env["PATH_INFO"].to_s,
54+
"query_string" => env["QUERY_STRING"].to_s,
55+
"body" => body,
56+
}
57+
end
58+
59+
def response_hash(response)
60+
status, headers, body = *response
61+
62+
{
63+
"status" => status,
64+
"headers" => headers,
65+
"body" => body.to_a.join("") #every body? all of it?
66+
}
67+
end
68+
69+
def exception_hash(ex)
70+
{
71+
"type" => ex.class.name,
72+
"message" => ex.message,
73+
"backtrace" => ex.backtrace[0..10]
74+
}
75+
end
76+
77+
def thread_locals
78+
Thread.current[:fluoride_collector] ||= {}
79+
end
80+
81+
def storage_path
82+
thread_locals[:storage_path] ||= File::join(@directory, "collection-#{Process.pid}-#{Thread.current.object_id}.yml")
83+
end
84+
85+
def storage_file
86+
File::open(storage_path, "a") do |file|
87+
yield file
88+
end
89+
end
90+
91+
def store(record)
92+
storage_file do |file|
93+
file.write(YAML::dump(record))
94+
end
95+
end
96+
end
97+
end
98+
end

lib/fluoride-collector/rails.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'fluoride-collector/rails/railtie'
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Fluoride
2+
module Collector
3+
class Railtie < ::Rails::Railtie
4+
config.fluoride = ActiveSupport::OrderedOptions.new
5+
config.fluoride.tags = nil
6+
config.fluoride.directory = "fluoride-collector"
7+
8+
initializer "fluoride-collector.add_middleware" do |app|
9+
app.middleware.insert_after("ActionDispatch::ShowExceptions",
10+
Fluoride::Collector::Middleware, config.fluoride.directory, config.fluoride.tags)
11+
end
12+
end
13+
end
14+
end

0 commit comments

Comments
 (0)