Permalink
Browse files

WebMock module instance methods are deprecated but not removed comple…

…tely to guarantee backwards compatibility.
  • Loading branch information...
1 parent 9fded75 commit 2dd8f567b54d74b2a748817caa1e8b148b4c98a9 @bblimke committed Oct 11, 2010
Showing with 45 additions and 9 deletions.
  1. +2 −2 CHANGELOG.md
  2. +2 −0 lib/webmock.rb
  3. +9 −0 lib/webmock/deprecation.rb
  4. +32 −7 lib/webmock/webmock.rb
View
4 CHANGELOG.md
@@ -9,9 +9,9 @@
* `WebMock::API#request` is renamed to `WebMock::API#a_request` to avoid method name conflicts with i.e. Rails controller specs.
WebMock.request is still available.
-* Removed `WebMock.response_for_request` and `WebMock.assertion_failure` which were only used internally and not documented.
+* Removed `WebMock.response_for_request` and `WebMock.assertion_failure` which were only used internally and were not documented.
-* Changed `WebMock#allow_net_connect!`, `WebMock#net_connect_allowed?`, `WebMock#registered_request?`, `WebMock#reset_callbacks`, `WebMock#after_request` instance methods to be WebMock class methods only.
+* Deprecated `WebMock#request`, `WebMock#allow_net_connect!`, `WebMock#net_connect_allowed?`, `WebMock#registered_request?`, `WebMock#reset_callbacks`, `WebMock#after_request` instance methods. They are still available, but only as WebMock class methods.
* :allow_localhost => true' now permits 0.0.0.0 in addition to 127.0.0.1 and 'localhost'. Thanks to Myron Marston and Mike Gehard for suggesting this.
View
2 lib/webmock.rb
@@ -3,6 +3,8 @@
require 'addressable/uri'
require 'crack'
+require 'webmock/deprecation'
+
require 'webmock/http_lib_adapters/net_http'
require 'webmock/http_lib_adapters/httpclient'
require 'webmock/http_lib_adapters/patron'
View
9 lib/webmock/deprecation.rb
@@ -0,0 +1,9 @@
+module WebMock
+ class Deprecation
+ class << self
+ def warning(message)
+ $stderr.puts "WebMock deprecation warning: #{message}"
@bblimke
bblimke Oct 11, 2010

Thanks. It's changed to warn now.

+ end
+ end
+ end
+end
View
39 lib/webmock/webmock.rb
@@ -1,18 +1,26 @@
module WebMock
-
+
def self.included(clazz)
- $stderr.puts "include WebMock is deprecated. Please include WebMock::API instead"
+ WebMock::Deprecation.warning("include WebMock is deprecated. Please include WebMock::API instead")
+ if clazz.instance_methods.map(&:to_s).include?('request')
+ $stderr.puts "WebMock#request was not included in #{clazz} to avoid name collision"
+ else
+ clazz.class_eval do
+ def request(method, uri)
+ WebMock::Deprecation.warning("WebMock#request is deprecated. Please use WebMock::API#a_request method instead")
+ WebMock.a_request(method, uri)
+ end
+ end
+ end
end
-
- extend self
include WebMock::API
extend WebMock::API
class << self
alias :request :a_request
end
-
+
def self.version
open(File.join(File.dirname(__FILE__), '../../VERSION')) { |f|
f.read.strip
@@ -45,13 +53,30 @@ def self.reset_webmock
def self.reset_callbacks
WebMock::CallbackRegistry.reset
end
-
+
def self.after_request(options={}, &block)
CallbackRegistry.add_callback(options, block)
end
-
+
def self.registered_request?(request_signature)
RequestRegistry.instance.registered_request?(request_signature)
end
+ %w(
+ allow_net_connect!
+ disable_net_connect!
+ net_connect_allowed?
+ reset_webmock
+ reset_callbacks
+ after_request
+ registered_request?
+ ).each do |method|
+ self.class_eval(%Q(
+ def #{method}(*args, &block)
+ WebMock::Deprecation.warning("WebMock##{method} instance method is deprecated. Please use WebMock.#{method} class method instead")
+ WebMock.#{method}(*args, &block)
+ end
+ ))
+ end
+
end

0 comments on commit 2dd8f56

Please sign in to comment.