Skip to content
This repository
Browse code

Prefer super to alias method chain.

WebMock was using alias method chain in lots of situations where it didn't need to, since most of the adapters subclass the HTTP client.
  • Loading branch information...
commit caeb0daeddf0ab6139f4269774d108ad084b0cd9 1 parent c31a3c0
Myron Marston authored August 12, 2012
94  lib/webmock/http_lib_adapters/curb_adapter.rb
@@ -170,117 +170,83 @@ def build_webmock_response
170 170
       ### Mocks of Curl::Easy methods below here.
171 171
       ###
172 172
 
173  
-      def http_with_webmock(method)
  173
+      def http(method)
174 174
         @webmock_method = method
175  
-          http_without_webmock(method)
  175
+        super
176 176
       end
177  
-      alias_method :http_without_webmock, :http
178  
-      alias_method :http, :http_with_webmock
179 177
 
180 178
       %w[ get head delete ].each do |verb|
181  
-        define_method "http_#{verb}_with_webmock" do
  179
+        define_method "http_#{verb}" do
182 180
           @webmock_method = verb
183  
-          send( "http_#{verb}_without_webmock" )
  181
+          super()
184 182
         end
185  
-
186  
-        alias_method "http_#{verb}_without_webmock", "http_#{verb}"
187  
-        alias_method "http_#{verb}", "http_#{verb}_with_webmock"
188 183
       end
189 184
 
190  
-      def http_put_with_webmock data = nil
  185
+      def http_put data = nil
191 186
         @webmock_method = :put
192 187
         @put_data = data if data
193  
-        http_put_without_webmock(data)
  188
+        super
194 189
       end
195  
-      alias_method :http_put_without_webmock, :http_put
196  
-      alias_method :http_put, :http_put_with_webmock
197 190
 
198  
-      def http_post_with_webmock *data
  191
+      def http_post *data
199 192
         @webmock_method = :post
200 193
         @post_body = data.join('&') if data && !data.empty?
201  
-        http_post_without_webmock(*data)
  194
+        super
202 195
       end
203  
-      alias_method :http_post_without_webmock, :http_post
204  
-      alias_method :http_post, :http_post_with_webmock
205  
-
206 196
 
207  
-      def perform_with_webmock
  197
+      def perform
208 198
         @webmock_method ||= :get
209  
-        curb_or_webmock do
210  
-          perform_without_webmock
211  
-        end
  199
+        curb_or_webmock { super }
212 200
       end
213  
-      alias :perform_without_webmock :perform
214  
-      alias :perform :perform_with_webmock
215 201
 
216  
-      def put_data_with_webmock= data
  202
+      def put_data= data
217 203
         @webmock_method = :put
218 204
         @put_data = data
219  
-        self.put_data_without_webmock = data
  205
+        super
220 206
       end
221  
-      alias_method :put_data_without_webmock=, :put_data=
222  
-      alias_method :put_data=, :put_data_with_webmock=
223 207
 
224  
-      def post_body_with_webmock= data
  208
+      def post_body= data
225 209
         @webmock_method = :post
226  
-        self.post_body_without_webmock = data
  210
+        super
227 211
       end
228  
-      alias_method :post_body_without_webmock=, :post_body=
229  
-      alias_method :post_body=, :post_body_with_webmock=
230 212
 
231  
-      def delete_with_webmock= value
  213
+      def delete= value
232 214
         @webmock_method = :delete if value
233  
-        self.delete_without_webmock = value
  215
+        super
234 216
       end
235  
-      alias_method :delete_without_webmock=, :delete=
236  
-      alias_method :delete=, :delete_with_webmock=
237 217
 
238  
-      def head_with_webmock= value
  218
+      def head= value
239 219
         @webmock_method = :head if value
240  
-        self.head_without_webmock = value
  220
+        super
241 221
       end
242  
-      alias_method :head_without_webmock=, :head=
243  
-      alias_method :head=, :head_with_webmock=
244 222
 
245  
-      def body_str_with_webmock
246  
-        @body_str || body_str_without_webmock
  223
+      def body_str
  224
+        @body_str || super
247 225
       end
248  
-      alias :body_str_without_webmock :body_str
249  
-      alias :body_str :body_str_with_webmock
250 226
 
251  
-      def response_code_with_webmock
252  
-        @response_code || response_code_without_webmock
  227
+      def response_code
  228
+        @response_code || super
253 229
       end
254  
-      alias :response_code_without_webmock :response_code
255  
-      alias :response_code :response_code_with_webmock
256 230
 
257  
-      def header_str_with_webmock
258  
-        @header_str || header_str_without_webmock
  231
+      def header_str
  232
+        @header_str || super
259 233
       end
260  
-      alias :header_str_without_webmock :header_str
261  
-      alias :header_str :header_str_with_webmock
262 234
 
263  
-      def last_effective_url_with_webmock
264  
-        @last_effective_url || last_effective_url_without_webmock
  235
+      def last_effective_url
  236
+        @last_effective_url || super
265 237
       end
266  
-      alias :last_effective_url_without_webmock :last_effective_url
267  
-      alias :last_effective_url :last_effective_url_with_webmock
268 238
 
269  
-      def content_type_with_webmock
270  
-        @content_type || content_type_without_webmock
  239
+      def content_type
  240
+        @content_type || super
271 241
       end
272  
-      alias :content_type_without_webmock :content_type
273  
-      alias :content_type :content_type_with_webmock
274 242
 
275 243
       %w[ success failure header body complete progress ].each do |callback|
276 244
         class_eval <<-METHOD, __FILE__, __LINE__
277  
-          def on_#{callback}_with_webmock &block
  245
+          def on_#{callback} &block
278 246
             @on_#{callback} = block
279  
-            on_#{callback}_without_webmock &block
  247
+            super
280 248
           end
281 249
         METHOD
282  
-        alias_method "on_#{callback}_without_webmock", "on_#{callback}"
283  
-        alias_method "on_#{callback}", "on_#{callback}_with_webmock"
284 250
       end
285 251
     end
286 252
   end
8  lib/webmock/http_lib_adapters/em_http_request/em_http_request_0_x.rb
@@ -47,7 +47,7 @@ def close_connection
47 47
         end
48 48
       end
49 49
 
50  
-      def send_request_with_webmock(&block)
  50
+      def send_request(&block)
51 51
         request_signature = build_request_signature
52 52
 
53 53
         WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
@@ -61,7 +61,7 @@ def send_request_with_webmock(&block)
61 61
             webmock_response.should_timeout ? "WebMock timeout error" : nil)
62 62
           client
63 63
         elsif WebMock.net_connect_allowed?(request_signature.uri)
64  
-          http = send_request_without_webmock(&block)
  64
+          http = super
65 65
           http.callback {
66 66
             if WebMock::CallbackRegistry.any_callbacks?
67 67
               webmock_response = build_webmock_response(http)
@@ -76,10 +76,6 @@ def send_request_with_webmock(&block)
76 76
         end
77 77
       end
78 78
 
79  
-      alias_method :send_request_without_webmock, :send_request
80  
-      alias_method :send_request, :send_request_with_webmock
81  
-
82  
-
83 79
       private
84 80
 
85 81
       def build_webmock_response(http)
13  lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb
@@ -48,7 +48,7 @@ def #{type}(options = {}, &blk)
48 48
     end
49 49
 
50 50
     class WebMockHttpConnection < HttpConnection
51  
-      def webmock_activate_connection(client)
  51
+      def activate_connection(client)
52 52
         request_signature = client.request_signature
53 53
 
54 54
         if client.stubbed_webmock_response
@@ -65,13 +65,11 @@ def webmock_activate_connection(client)
65 65
           finalize_request(client)
66 66
           @conn.set_deferred_status :succeeded
67 67
         elsif WebMock.net_connect_allowed?(request_signature.uri)
68  
-          real_activate_connection(client)
  68
+          super
69 69
         else
70 70
           raise WebMock::NetConnectNotAllowedError.new(request_signature)
71 71
         end
72 72
       end
73  
-      alias_method :real_activate_connection, :activate_connection
74  
-      alias_method :activate_connection, :webmock_activate_connection
75 73
     end
76 74
 
77 75
     class WebMockHttpClient < EventMachine::HttpClient
@@ -92,7 +90,7 @@ def setup(response, uri, error = nil)
92 90
         end
93 91
       end
94 92
 
95  
-      def send_request_with_webmock(head, body)
  93
+      def send_request(head, body)
96 94
         WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
97 95
 
98 96
         if stubbed_webmock_response
@@ -104,15 +102,12 @@ def send_request_with_webmock(head, body)
104 102
           }
105 103
           self
106 104
         elsif WebMock.net_connect_allowed?(request_signature.uri)
107  
-          send_request_without_webmock(head, body)
  105
+          super
108 106
         else
109 107
           raise WebMock::NetConnectNotAllowedError.new(request_signature)
110 108
         end
111 109
       end
112 110
 
113  
-      alias_method :send_request_without_webmock, :send_request
114  
-      alias_method :send_request, :send_request_with_webmock
115  
-
116 111
       def set_deferred_status(status, *args)
117 112
         if status == :succeeded && !stubbed_webmock_response && WebMock::CallbackRegistry.any_callbacks?
118 113
           webmock_response = build_webmock_response
25  lib/webmock/http_lib_adapters/httpclient_adapter.rb
@@ -28,16 +28,18 @@ def self.disable!
28 28
 
29 29
 
30 30
   class WebMockHTTPClient < HTTPClient
  31
+    alias_method :do_get_block_without_webmock, :do_get_block
  32
+    alias_method :do_get_stream_without_webmock, :do_get_stream
31 33
 
32  
-    def do_get_block_with_webmock(req, proxy, conn, &block)
33  
-      do_get_with_webmock(req, proxy, conn, false, &block)
  34
+    def do_get_block(req, proxy, conn, &block)
  35
+      do_get(req, proxy, conn, false, &block)
34 36
     end
35 37
 
36  
-    def do_get_stream_with_webmock(req, proxy, conn, &block)
37  
-      do_get_with_webmock(req, proxy, conn, true, &block)
  38
+    def do_get_stream(req, proxy, conn, &block)
  39
+      do_get(req, proxy, conn, true, &block)
38 40
     end
39 41
 
40  
-    def do_get_with_webmock(req, proxy, conn, stream = false, &block)
  42
+    def do_get(req, proxy, conn, stream = false, &block)
41 43
       request_signature = build_request_signature(req, :reuse_existing)
42 44
 
43 45
       WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
@@ -72,27 +74,18 @@ def do_get_with_webmock(req, proxy, conn, stream = false, &block)
72 74
       end
73 75
     end
74 76
 
75  
-    def do_request_async_with_webmock(method, uri, query, body, extheader)
  77
+    def do_request_async(method, uri, query, body, extheader)
76 78
       req = create_request(method, uri, query, body, extheader)
77 79
       request_signature = build_request_signature(req)
78 80
       webmock_request_signatures << request_signature
79 81
 
80 82
       if webmock_responses[request_signature] || WebMock.net_connect_allowed?(request_signature.uri)
81  
-        do_request_async_without_webmock(method, uri, query, body, extheader)
  83
+        super
82 84
       else
83 85
         raise WebMock::NetConnectNotAllowedError.new(request_signature)
84 86
       end
85 87
     end
86 88
 
87  
-    alias_method :do_get_block_without_webmock, :do_get_block
88  
-    alias_method :do_get_block, :do_get_block_with_webmock
89  
-
90  
-    alias_method :do_get_stream_without_webmock, :do_get_stream
91  
-    alias_method :do_get_stream, :do_get_stream_with_webmock
92  
-
93  
-    alias_method :do_request_async_without_webmock, :do_request_async
94  
-    alias_method :do_request_async, :do_request_async_with_webmock
95  
-
96 89
     def build_httpclient_response(webmock_response, stream = false, &block)
97 90
       body = stream ? StringIO.new(webmock_response.body) : webmock_response.body
98 91
       response = HTTP::Message.new_response(body)
24  lib/webmock/http_lib_adapters/net_http.rb
@@ -32,11 +32,9 @@ def self.disable!
32 32
 
33 33
       @webMockNetHTTP = Class.new(Net::HTTP) do
34 34
         class << self
35  
-          def socket_type_with_webmock
  35
+          def socket_type
36 36
             StubSocket
37 37
           end
38  
-          alias_method :socket_type_without_webmock, :socket_type
39  
-          alias_method :socket_type, :socket_type_with_webmock
40 38
 
41 39
           if Module.method(:const_defined?).arity == 1
42 40
             def const_defined?(name)
@@ -63,7 +61,7 @@ def constants(inherit=true)
63 61
           end
64 62
         end
65 63
 
66  
-        def request_with_webmock(request, body = nil, &block)
  64
+        def request(request, body = nil, &block)
67 65
           request_signature = WebMock::NetHTTPUtility.request_signature_from_request(self, request, body)
68 66
 
69 67
           WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
@@ -88,19 +86,17 @@ def request_with_webmock(request, body = nil, &block)
88 86
             response = if (started? && !WebMock::Config.instance.net_http_connect_on_start) || !started?
89 87
               @started = false #otherwise start_with_connect wouldn't execute and connect
90 88
               start_with_connect {
91  
-                response = request_without_webmock(request, nil)
  89
+                response = super(request, nil, &nil)
92 90
                 after_request.call(response)
93 91
               }
94 92
             else
95  
-              response = request_without_webmock(request, nil)
  93
+              response = super(request, nil, &nil)
96 94
               after_request.call(response)
97 95
             end
98 96
           else
99 97
             raise WebMock::NetConnectNotAllowedError.new(request_signature)
100 98
           end
101 99
         end
102  
-        alias_method :request_without_webmock, :request
103  
-        alias_method :request, :request_with_webmock
104 100
 
105 101
         def start_without_connect
106 102
           raise IOError, 'HTTP session already opened' if @started
@@ -116,15 +112,15 @@ def start_without_connect
116 112
           self
117 113
         end
118 114
 
119  
-        def start_with_conditional_connect(&block)
  115
+        alias_method :start_with_connect, :start
  116
+
  117
+        def start(&block)
120 118
           if WebMock::Config.instance.net_http_connect_on_start
121  
-            start_with_connect(&block)
  119
+            super(&block)
122 120
           else
123 121
             start_without_connect(&block)
124 122
           end
125 123
         end
126  
-        alias_method :start_with_connect, :start
127  
-        alias_method :start, :start_with_conditional_connect
128 124
 
129 125
         def build_net_http_response(webmock_response, &block)
130 126
           response = Net::HTTPResponse.send(:response_class, webmock_response.status[0].to_s).new("1.0", webmock_response.status[0].to_s, webmock_response.status[1])
@@ -204,7 +200,7 @@ def readuntil(*args)
204 200
 module Net  #:nodoc: all
205 201
 
206 202
   class WebMockNetBufferedIO < BufferedIO
207  
-    def initialize_with_webmock(io, debug_output = nil)
  203
+    def initialize(io, debug_output = nil)
208 204
       @read_timeout = 60
209 205
       @rbuf = ''
210 206
       @debug_output = debug_output
@@ -217,8 +213,6 @@ def initialize_with_webmock(io, debug_output = nil)
217 213
       end
218 214
       raise "Unable to create local socket" unless @io
219 215
     end
220  
-    alias_method :initialize_without_webmock, :initialize
221  
-    alias_method :initialize, :initialize_with_webmock
222 216
   end
223 217
 
224 218
 end
7  lib/webmock/http_lib_adapters/patron_adapter.rb
@@ -13,7 +13,7 @@ class PatronAdapter < ::WebMock::HttpLibAdapter
13 13
         OriginalPatronSession = ::Patron::Session unless const_defined?(:OriginalPatronSession)
14 14
 
15 15
         class WebMockPatronSession < ::Patron::Session
16  
-          def handle_request_with_webmock(req)
  16
+          def handle_request(req)
17 17
             request_signature =
18 18
               WebMock::HttpLibAdapters::PatronAdapter.build_request_signature(req)
19 19
 
@@ -28,7 +28,7 @@ def handle_request_with_webmock(req)
28 28
                 {:lib => :patron}, request_signature, webmock_response)
29 29
               res
30 30
             elsif WebMock.net_connect_allowed?(request_signature.uri)
31  
-              res = handle_request_without_webmock(req)
  31
+              res = super
32 32
               if WebMock::CallbackRegistry.any_callbacks?
33 33
                 webmock_response = WebMock::HttpLibAdapters::PatronAdapter.
34 34
                   build_webmock_response(res)
@@ -41,9 +41,6 @@ def handle_request_with_webmock(req)
41 41
               raise WebMock::NetConnectNotAllowedError.new(request_signature)
42 42
             end
43 43
           end
44  
-
45  
-          alias_method :handle_request_without_webmock, :handle_request
46  
-          alias_method :handle_request, :handle_request_with_webmock
47 44
         end
48 45
 
49 46
         def self.enable!

0 notes on commit caeb0da

Please sign in to comment.
Something went wrong with that request. Please try again.