response_logger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ spec/log
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Bigg (Mocra)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ = response_logger
2
+
3
+ Require this little gem (tee hee) into your application's environment and all requests made via Net::HTTP are logged into the log folder. This works with ActiveResource, Mechanize, Nokogiri and HTTParty.
4
+
5
+ Include it into your rails app like so:
6
+
7
+ config.gem 'response_logger'
8
+
9
+ And restart your rails server to load it in.
10
+
11
+ The next time you make a Net::HTTP request a folder will appear in the log folder like this:
12
+
13
+ <year>
14
+ \_<month>
15
+ \_<day>
16
+ \_<port number>
17
+ \_<domain>
18
+ \_<path>
19
+ \_<to>
20
+ \_<file>
21
+ \_<hour><minute><second>
22
+
23
+ The final file in that chain contains the response body.
24
+
25
+ == Note on Patches/Pull Requests
26
+
27
+ * Fork the project.
28
+ * Make your feature addition or bug fix.
29
+ * Add tests for it. This is important so I don't break it in a
30
+ future version unintentionally.
31
+ * Commit, do not mess with rakefile, version, or history.
32
+ (if you want to have your own version, that is fine but
33
+ bump version in a commit by itself I can ignore when I pull)
34
+ * Send me a pull request. Bonus points for topic branches.
35
+
36
+ == Copyright
37
+
38
+ Copyright (c) 2009 Ryan Bigg. See LICENSE for details.
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "response_logger"
8
+ gem.summary = %Q{Logs responses of Net::HTTP requests}
9
+ gem.description = %Q{Logs responses of Net::HTTP requests}
10
+ gem.email = "radarlistener@gmail.com"
11
+ gem.homepage = "http://github.com/radar/response_logger"
12
+ gem.authors = ["Ryan Bigg", "Bodaniel Jeanes"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION')
40
+ version = File.read('VERSION')
41
+ else
42
+ version = ""
43
+ end
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "response_logger #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,8 @@
1
+ # Override get method to store the files we request.
2
+ require 'net/http'
3
+ require 'net/https'
4
+ module Net
5
+ class HTTP
6
+ include ResponseLogger
7
+ end
8
+ end
@@ -0,0 +1,33 @@
1
+ module ResponseLogger
2
+ def self.included(base)
3
+ base.class_eval do
4
+ alias_method :original_request, :request
5
+
6
+ def request(req, body = nil, &block)
7
+ # SSL requests will call this method twice if the connection is not started.
8
+ # This is for your own safety.
9
+ if @already_called
10
+ res = original_request(req, body, &block)
11
+ else
12
+ @already_called = true
13
+ file = Time.now.strftime("%H%M%S")
14
+ file_path = File.join(RAILS_ROOT, "log", Date.today.strftime("%Y/%m/%d"), @port.to_s, @address, req.path.sub(/^\//, ''))
15
+
16
+ FileUtils.mkdir_p(file_path)
17
+ res = original_request(req, body, &block)
18
+ File.open(File.join(file_path, file), "w+") do |f|
19
+ f.write(res.body)
20
+ end
21
+ end
22
+ @already_called = false
23
+ res
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ require File.join(File.dirname(__FILE__), 'net/http_ext')
31
+
32
+
33
+
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{response_logger}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Bigg", "Bodaniel Jeanes"]
12
+ s.date = %q{2009-10-15}
13
+ s.description = %q{Logs responses of Net::HTTP requests}
14
+ s.email = %q{radarlistener@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/net/http_ext.rb",
27
+ "lib/response_logger.rb",
28
+ "response_logger.gemspec",
29
+ "spec/response_logger_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/radar/response_logger}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{Logs responses of Net::HTTP requests}
37
+ s.test_files = [
38
+ "spec/response_logger_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<rspec>, [">= 0"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 0"])
53
+ end
54
+ end
@@ -0,0 +1,71 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ResponseLogger" do
4
+ before do
5
+ @path = File.join(RAILS_ROOT, "log", Date.today.strftime("%Y/%m/%d"))
6
+ end
7
+
8
+ def logged_files
9
+ Dir["#{@path}/**/*"].select { |item| File.file?(item) }
10
+ end
11
+
12
+ def response_check(logged_files, response)
13
+ File.read(logged_files.first).should(eql(response.body), "The logged file does not contain the same text as response body")
14
+ end
15
+
16
+ describe "Logging" do
17
+
18
+ before do
19
+ logged_files.size.should eql(0)
20
+ end
21
+
22
+ describe "Net::HTTP" do
23
+ describe "#get" do
24
+ it "root requests" do
25
+ logged_files.size.should eql(0)
26
+ Net::HTTP.get(URI.parse("http://github.com/"))
27
+ logged_files.size.should eql(1)
28
+ logged_files.detect { |file| /github\.com\/.*?/.match(file) }.should_not be_nil
29
+ end
30
+ end
31
+
32
+ describe "#new" do
33
+ it "works with SSL" do
34
+ logged_files.size.should eql(0)
35
+ url = URI.parse('https://github.com/login')
36
+ http = Net::HTTP.new(url.host, 443)
37
+ req = Net::HTTP::Get.new(url.path)
38
+ http.use_ssl = true
39
+
40
+ text = "Log in - GitHub"
41
+
42
+ response = http.request(req)
43
+ response.class.should eql(Net::HTTPOK)
44
+
45
+ logged_files.size.should eql(1)
46
+ logged_files.detect { |file| /login\/\d+/.match(file) }.should_not be_nil
47
+ response_check(logged_files, response)
48
+ end
49
+
50
+ it "works without SSL" do
51
+ logged_files.size.should eql(0)
52
+ url = URI.parse('https://github.com/')
53
+ http = Net::HTTP.new(url.host, 80)
54
+ req = Net::HTTP::Get.new(url.path)
55
+
56
+ text = "Secure source code hosting and collaborative development - GitHub"
57
+
58
+ response = http.request(req)
59
+ response.class.should eql(Net::HTTPOK)
60
+
61
+ logged_files.size.should eql(1)
62
+ logged_files.detect { |file| /github\.com\/\d+/.match(file) }.should_not be_nil
63
+ response_check(logged_files, response)
64
+ end
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+
71
+ end
@@ -0,0 +1,20 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ begin
5
+ require 'rubygems'
6
+ rescue LoadError
7
+ puts "Alright, no rubygems? No worries!"
8
+ end
9
+ require 'response_logger'
10
+ require 'spec'
11
+ require 'spec/autorun'
12
+ require 'fileutils'
13
+
14
+ RAILS_ROOT = File.dirname(__FILE__)
15
+
16
+ Spec::Runner.configure do |config|
17
+ config.before(:each) do
18
+ FileUtils.rm_rf(File.join(RAILS_ROOT, "log"))
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: response_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Bigg
8
+ - Bodaniel Jeanes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-10-15 00:00:00 +10:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ type: :development
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ description: Logs responses of Net::HTTP requests
27
+ email: radarlistener@gmail.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.md
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - LICENSE
39
+ - README.md
40
+ - Rakefile
41
+ - VERSION
42
+ - lib/net/http_ext.rb
43
+ - lib/response_logger.rb
44
+ - response_logger.gemspec
45
+ - spec/response_logger_spec.rb
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/radar/response_logger
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Logs responses of Net::HTTP requests
75
+ test_files:
76
+ - spec/response_logger_spec.rb
77
+ - spec/spec_helper.rb