Module: WebmockMethod

Includes:
MetaMethods
Defined in:
lib/webmock_method/version.rb,
lib/webmock_method/webmock_method.rb

Constant Summary collapse

VERSION =
"1.0.1"

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from MetaMethods

#define_attribute, #define_attributes

Class Attribute Details

.urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/webmock_method/webmock_method.rb', line 8

def url
  @url
end

Instance Method Details

#webmock_method(method_name, param_names, response_proc, url = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/webmock_method/webmock_method.rb', line 11

def webmock_method(method_name, param_names, response_proc, url = nil)
  define_method("#{method_name}_with_webmock_method") do |*args|
    param_names.each_with_index do |param_name, index|
      MetaMethods.define_attribute(self, param_name, args[index])
    end

    yield(self, *args) if block_given?

    begin
      response = response_proc.call(binding)

      request_url = url
      request_url = @url if url.nil?
      request_url = WebmockMethod.url if request_url.nil?

      throw "Url is not defined." unless request_url

      WebMock.stub_request(:any, request_url).to_return(:body => response)

      send("#{method_name}_without_webmock_method", *args)
    rescue Exception => e
      puts e.message
      raise e
    ensure
      WebMock.reset!
    end
  end

  alias_method :"#{method_name}_without_webmock_method", method_name
  alias_method method_name, :"#{method_name}_with_webmock_method"

end