Class: StubRequests::WebMockBuilder

Inherits:
Object
  • Object
show all
Includes:
HashUtil
Defined in:
lib/stub_requests/webmock_builder.rb

Overview

Note:

This module can either be used by its class methods or included in say RSpec

Module API abstraction to reduce the amount of WebMock.stub_request

Since:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashUtil

compact

Constructor Details

#initialize(verb, uri, options = {}) { ... } ⇒ WebMockBuilder

Initializes a new instance of

Parameters:

  • verb (Symbol)

    a HTTP verb/method

  • uri (String)

    a URI to call

  • options (Hash<Symbol>) (defaults to: {})

    request/response options for Webmock::RequestStub

Yields:

  • a block to eventually yield to the caller

Since:

  • 0.1.0



58
59
60
61
62
# File 'lib/stub_requests/webmock_builder.rb', line 58

def initialize(verb, uri, options = {}, &callback)
  @request_stub = WebMock::RequestStub.new(verb, uri)
  @options      = options
  @callback     = callback
end

Instance Attribute Details

#callbackObject (readonly)

Since:

  • 0.1.0



46
47
48
# File 'lib/stub_requests/webmock_builder.rb', line 46

def callback
  @callback
end

#optionsObject (readonly)

Since:

  • 0.1.0



42
43
44
# File 'lib/stub_requests/webmock_builder.rb', line 42

def options
  @options
end

#request_stubObject (readonly)

Since:

  • 0.1.0



38
39
40
# File 'lib/stub_requests/webmock_builder.rb', line 38

def request_stub
  @request_stub
end

Class Method Details

.build(verb, uri, options = {}) { ... } ⇒ WebMock::RequestStub

Builds and registers a WebMock::RequestStub

Parameters:

  • verb (Symbol)

    a HTTP verb/method

  • uri (String)

    a URI to call

  • options (Hash<Symbol>) (defaults to: {})

    request/response options for Webmock::RequestStub

Yields:

  • a callback to eventually yield to the caller

Returns:

  • (WebMock::RequestStub)

    the registered stub

Since:

  • 0.1.0



31
32
33
# File 'lib/stub_requests/webmock_builder.rb', line 31

def self.build(verb, uri, options = {}, &callback)
  new(verb, uri, options, &callback).build
end

Instance Method Details

#buildWebMock::RequestStub

Prepares a WebMock::RequestStub and registers it in WebMock

Returns:

  • (WebMock::RequestStub)

    the registered stub

Since:

  • 0.1.0



70
71
72
73
74
75
76
77
78
# File 'lib/stub_requests/webmock_builder.rb', line 70

def build
  if callback.present?
    Docile.dsl_eval(request_stub, &callback)
  else
    prepare_mock_request
  end

  WebMock::StubRegistry.instance.register_request_stub(request_stub)
end