martinbtt-net-http-spy 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/readme.markdown +60 -0
  2. data/spy.rb +68 -0
  3. metadata +74 -0
data/readme.markdown ADDED
@@ -0,0 +1,60 @@
1
+ ## About
2
+
3
+ Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party
4
+ API is making? Use HTTP Spy to see what is going on behind the scenes.
5
+
6
+ ## Installation
7
+
8
+ sudo gem install martinbtt-net-http-spy
9
+
10
+ ## Example Usage
11
+
12
+ require 'rubygems'
13
+ require 'twitter'
14
+ require 'http-spy'
15
+
16
+ Twitter::Search.new('httparty').each { |r| r }
17
+ # Outputs...
18
+ -- : CONNECT: ["search.twitter.com", 80]
19
+ -- : GET /search.json?q=httparty
20
+ -- : BODY: Net::HTTPOK
21
+
22
+
23
+ See the examples folder for more.
24
+
25
+ ## Further Options
26
+
27
+ Show the call trace to the originating line of code in the third party gem
28
+
29
+ Net::HTTP.http_logger_options = {:trace => true}
30
+
31
+ Output the body of the request
32
+
33
+ Net::HTTP.http_logger_options = {:body => true}
34
+
35
+ Show the full raw HTTP output
36
+
37
+ Net::HTTP.http_logger_options = {:verbose => true}
38
+
39
+ Change the logger. By default HTTP spy logs to STDOUT
40
+
41
+ Net::HTTP.http_logger = Logger.new('twitter.log')
42
+
43
+ ## Bonus Points
44
+
45
+ Use it to grab sample data for FakeWeb = testing goodness.
46
+
47
+ ## Notes
48
+
49
+ This is a pretty early release. I'm sure there is plenty that can be done to improve compatibility
50
+ as several libraries call Net::HTTP in a slightly different way.
51
+ Feel free to fork and send in pull requests/patches.
52
+
53
+ ## Find Me
54
+
55
+ Martin Sadler (martin -- at -- beyondthetype.com)
56
+
57
+ * Blog: http://www.beyondthetype.com
58
+ * Follow: http://twitter.com/martinbtt
59
+ * Code: http://github.com/martinbtt
60
+ * Recommend: http://www.workingwithrails.com/person/5152-martin-sadler
data/spy.rb ADDED
@@ -0,0 +1,68 @@
1
+ # require 'net/https'
2
+ # require 'logger'
3
+ # require 'cgi'
4
+ #
5
+ # # HTTP SPY
6
+ # module Net
7
+ # class HTTP
8
+ # alias :old_initialize :initialize
9
+ # alias :old_post :post
10
+ # # alias :old_get :get
11
+ # alias :old_request :request
12
+ #
13
+ # class << self
14
+ # attr_accessor :http_logger
15
+ # attr_accessor :http_logger_options
16
+ # end
17
+ #
18
+ # def initialize(*args, &block)
19
+ # @logger_options = self.class.http_logger_options ||= {:hide_body => true}
20
+ #
21
+ #
22
+ # self.class.http_logger.info "CONNECT: #{args.inspect}" if !@logger_options[:verbose]
23
+ #
24
+ # old_initialize(*args, &block)
25
+ # @debug_output = self.class.http_logger if @logger_options[:verbose]
26
+ #
27
+ # end
28
+ #
29
+ #
30
+ # # def get(*args, &block)
31
+ # # self.class.http_logger.info "GET: #{args}"# if self.class.http_logger_only.include?(:get)
32
+ # # result = old_get(*args, &block)
33
+ # # self.class.http_logger.info "BODY: #{result.body}" #if self.class.http_logger_only.include?(:body)
34
+ # # result
35
+ # # end
36
+ # #
37
+ # # def post(*args, &block)
38
+ # # self.class.http_logger.info "POST: #{args}" #if self.class.http_logger_only.include?(:post)
39
+ # # result = old_post(*args, &block)
40
+ # # self.class.http_logger.info "BODY: #{result.body}" #if self.class.http_logger_only.include?(:body)
41
+ # # result
42
+ # # end
43
+ #
44
+ #
45
+ # def request(*args, &block)
46
+ # unless started? || @logger_options[:verbose]
47
+ # req = args[0].class::METHOD
48
+ # self.class.http_logger.info "#{req} #{args[0].path}"
49
+ # self.class.http_logger.info "PARAMS #{CGI.parse(args[0].body).inspect} " if args[0].body && req != 'CONNECT'
50
+ # self.class.http_logger.info "TRACE: #{caller.reverse}" if @logger_options[:calltrace]
51
+ # end
52
+ #
53
+ # result = old_request(*args, &block)
54
+ #
55
+ # unless started? || @logger_options[:verbose]
56
+ # self.class.http_logger.info "BODY: #{ @logger_options[:hide_body] ? result.class.name : result.body}"
57
+ # end
58
+ # result
59
+ # end
60
+ #
61
+ #
62
+ # end
63
+ #
64
+ # end
65
+ #
66
+ # # Usage
67
+ # # Great for testing
68
+ # Net::HTTP.http_logger = Logger.new(STDOUT)
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: martinbtt-net-http-spy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Sadler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-07 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: twitter
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description:
36
+ email: martin@beyondthetype.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - readme.markdown
43
+ files:
44
+ - readme.markdown
45
+ - spy.rb
46
+ has_rdoc: false
47
+ homepage:
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - readme.markdown
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: net-http-spy
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Ever wondered what HTTP requests the Ruby gem you are using to connect to a third party API is making? Use HTTP Spy to see what is going on behind the scenes.
73
+ test_files: []
74
+