Module: WebmockMethod

Extended by:
WebmockMethod
Includes:
MetaMethods::Core
Included in:
WebmockMethod
Defined in:
lib/webmock_method/version.rb,
lib/webmock_method/webmock_method.rb

Constant Summary collapse

VERSION =
"1.1.0"

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/webmock_method/webmock_method.rb', line 10

def url
  @url
end

Instance Method Details

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



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
43
44
45
46
47
48
49
50
# File 'lib/webmock_method/webmock_method.rb', line 13

def webmock_method(method_name, param_names, response_proc, url = nil)
  object = self

  define_method("#{method_name}_with_webmock_method") do |*args|
    param_names.each_with_index do |param_name, index|
      object.define_attribute(self, param_name, args[index])
    end

    yield(self, *args) if block_given?

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

      throw "Url is not defined." unless request_url

      if defined?(error)
        WebMock.stub_request(:any, request_url).to_raise(error)
      else
        response = response_proc.call(binding)

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

      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