Module: WebmockMethod

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

Constant Summary collapse

VERSION =
"1.2.5"

Class Attribute Summary collapse

Instance Method Summary collapse

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

#define_attribute(object, key, value) ⇒ Object



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

def define_attribute object, key, value
  MetaMethods::Core.instance.define_attribute(object, key, value)
end

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



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
51
52
53
54
55
56
57
58
59
# File 'lib/webmock_method/webmock_method.rb', line 15

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::Core.instance.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 = self.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)

        #$responses ||= []
        #
        #$responses << response

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

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

      #$responses.pop
      #
      #previous_response = $responses.last
      #
      #stub_request(:any, StubWebMethod.stubbed_url(ignore_get_params, self.url)).to_return(:body => response) if previous_response
    end
  end

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