Class: HttpStub::Configurer::DSL::StubBuilder
- Inherits:
-
Object
- Object
- HttpStub::Configurer::DSL::StubBuilder
- Defined in:
- lib/http_stub/configurer/dsl/stub_builder.rb
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#triggers ⇒ Object
readonly
Returns the value of attribute triggers.
Instance Method Summary collapse
- #build ⇒ Object
-
#initialize(parent_builder = nil, &block) ⇒ StubBuilder
constructor
A new instance of StubBuilder.
- #invoke(&block) ⇒ Object
- #match_requests(args) ⇒ Object
- #merge!(stub_builder) ⇒ Object
- #respond_with(args = {}, &_block) ⇒ Object
- #schema(type, definition) ⇒ Object
- #trigger(args) ⇒ Object
Constructor Details
#initialize(parent_builder = nil, &block) ⇒ StubBuilder
Returns a new instance of StubBuilder.
9 10 11 12 13 14 15 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 9 def initialize(parent_builder=nil, &block) @request = {} @response = {} @triggers = { scenarios: [], stubs: [] } self.merge!(parent_builder) if parent_builder self.invoke(&block) if block_given? end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
7 8 9 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 7 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 7 def response @response end |
#triggers ⇒ Object (readonly)
Returns the value of attribute triggers.
7 8 9 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 7 def triggers @triggers end |
Instance Method Details
#build ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 50 def build HttpStub::Configurer::Request::Stub.new( request: @request, response: @response, triggers: { scenario_names: @triggers[:scenarios], stubs: @triggers[:stubs].map(&:build) } ) end |
#invoke(&block) ⇒ Object
40 41 42 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 40 def invoke(&block) block.arity.zero? ? self.instance_eval(&block) : (yield self) end |
#match_requests(args) ⇒ Object
17 18 19 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 17 def match_requests(args) self.tap { @request.deep_merge!(args) } end |
#merge!(stub_builder) ⇒ Object
44 45 46 47 48 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 44 def merge!(stub_builder) self.match_requests(stub_builder.request) self.respond_with(stub_builder.response) self.trigger(stub_builder.triggers) end |
#respond_with(args = {}, &_block) ⇒ Object
25 26 27 28 29 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 25 def respond_with(args={}, &_block) resolved_args = args resolved_args = yield HttpStub::Configurer::DSL::RequestReferencer.new if block_given? self.tap { @response.deep_merge!(resolved_args) } end |
#schema(type, definition) ⇒ Object
21 22 23 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 21 def schema(type, definition) { schema: { type: type, definition: definition } } end |
#trigger(args) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/http_stub/configurer/dsl/stub_builder.rb', line 31 def trigger(args) resolved_args = args.clone resolved_args[:scenarios] ||= resolved_args[:scenario] ? [ resolved_args[:scenario] ] : [] resolved_args[:stubs] ||= resolved_args[:stub] ? [ resolved_args[:stub] ] : [] @triggers[:scenarios].concat(resolved_args[:scenarios]) @triggers[:stubs].concat(resolved_args[:stubs]) self end |