Navigation Menu

Skip to content

Commit

Permalink
WebMock module instance methods are deprecated but not removed comple…
Browse files Browse the repository at this point in the history
…tely to guarantee backwards compatibility.
  • Loading branch information
bblimke committed Oct 11, 2010
1 parent 9fded75 commit 2dd8f56
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions lib/webmock.rb
Expand Up @@ -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'
Expand Down
9 changes: 9 additions & 0 deletions lib/webmock/deprecation.rb
@@ -0,0 +1,9 @@
module WebMock
class Deprecation
class << self
def warning(message)
$stderr.puts "WebMock deprecation warning: #{message}"

This comment has been minimized.

Copy link
@myronmarston

myronmarston Oct 11, 2010

Collaborator

FWIW, Kernel provides a warn method:

http://ruby-doc.org/core/classes/Kernel.html#M005990

This comment has been minimized.

Copy link
@bblimke

bblimke Oct 11, 2010

Author Owner

Thanks. It's changed to warn now.

end
end
end
end
39 changes: 32 additions & 7 deletions 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
Expand Down Expand Up @@ -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.