Class: HttpStub::Configurator::Stub::Stub
- Inherits:
-
Object
- Object
- HttpStub::Configurator::Stub::Stub
- Defined in:
- lib/http_stub/configurator/stub/stub.rb
Instance Method Summary collapse
- #id ⇒ Object
-
#initialize(parent = nil, &block) ⇒ Stub
constructor
A new instance of Stub.
- #invoke(additional_arg = nil, &block) ⇒ Object
- #match_requests(args) ⇒ Object
- #merge!(stub) ⇒ Object
- #respond_with(args = {}, &block) ⇒ Object
- #schema(type, definition) ⇒ Object
- #to_hash ⇒ Object
- #trigger(args) ⇒ Object
Constructor Details
#initialize(parent = nil, &block) ⇒ Stub
Returns a new instance of Stub.
7 8 9 10 11 12 13 14 15 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 7 def initialize(parent=nil, &block) @hash = { match_rules: {}, response: { blocks: [] }, triggers: { scenario_names: [], stubs: [] } }.with_indifferent_access self.merge!(parent) if parent self.invoke(&block) if block_given? end |
Instance Method Details
#id ⇒ Object
51 52 53 54 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 51 def id basic_response = @hash[:response].clone.tap { |hash| hash[:blocks] = hash[:blocks].map(&:source_location) } Digest::MD5.hexdigest([ @hash[:match_rules], basic_response, @hash[:triggers] ].to_s) end |
#invoke(additional_arg = nil, &block) ⇒ Object
40 41 42 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 40 def invoke(additional_arg=nil, &block) block.arity.zero? ? self.instance_eval(&block) : yield(*[ self, additional_arg ].compact) end |
#match_requests(args) ⇒ Object
17 18 19 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 17 def match_requests(args) self.tap { @hash[:match_rules].deep_merge!(args) } end |
#merge!(stub) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 44 def merge!(stub) stub_hash = stub.to_hash self.match_requests(stub_hash[:match_rules]) self.respond_with(stub_hash[:response]) self.trigger(scenarios: stub_hash[:triggers][:scenario_names], stubs: stub_hash[:triggers][:stubs]) end |
#respond_with(args = {}, &block) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 25 def respond_with(args={}, &block) remaining_args = args.with_indifferent_access response_opts = @hash[:response] response_opts[:blocks].concat([ remaining_args.delete(:blocks), block ].flatten.compact) self.tap { response_opts.deep_merge!(remaining_args) } end |
#schema(type, definition) ⇒ Object
21 22 23 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 21 def schema(type, definition) { schema: { type: type, definition: definition } } end |
#to_hash ⇒ Object
56 57 58 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 56 def to_hash @hash.merge(id: id) end |
#trigger(args) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/http_stub/configurator/stub/stub.rb', line 32 def trigger(args) resolved_args = to_triggers(args) triggers_opts = @hash[:triggers] triggers_opts[:scenario_names].concat(resolved_args[:scenario_names]) triggers_opts[:stubs].concat(resolved_args[:stubs]) self end |