class HttpStub::Configurator::Stub::Stub

Public Class Methods

new(parent=nil, &block) click to toggle source
# 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

Public Instance Methods

id() click to toggle source
# 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) { |*[ self, additional_arg ].compact| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/http_stub/configurator/stub/stub.rb, line 21
def schema(type, definition)
  { schema: { type: type, definition: definition } }
end
to_hash() click to toggle source
# File lib/http_stub/configurator/stub/stub.rb, line 56
def to_hash
  @hash.merge(id: id)
end
trigger(args) click to toggle source
# 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

Private Instance Methods

to_triggers(args) click to toggle source
# File lib/http_stub/configurator/stub/stub.rb, line 62
def to_triggers(args)
  {}.tap do |resolved_args|
    resolved_args[:scenario_names] = args[:scenario] ? [ args[:scenario] ] : args[:scenarios] || []
    stubs = args[:stub] ? [ args[:stub] ] : args[:stubs] || []
    resolved_args[:stubs] = stubs.map(&:to_hash)
  end
end