class HammerCLIForeman::Testing::APIExpectations::APICallMatcher

Attributes

block[RW]
expected_action[RW]
expected_params[RW]
expected_resource[RW]

Public Class Methods

new(resource=nil, action=nil, &block) click to toggle source
# File lib/hammer_cli_foreman/testing/api_expectations.rb, line 7
def initialize(resource=nil, action=nil, &block)
  @expected_resource = resource
  @expected_action = action
  @block = block if block_given?
  @expected_params = {}
end

Public Instance Methods

matches?(actual_parameters) click to toggle source
# File lib/hammer_cli_foreman/testing/api_expectations.rb, line 14
def matches?(actual_parameters)
  action, params, headers, options = actual_parameters.shift(4)
  action_name = action.name.to_s
  resource_name = action.resource.to_s

  result = true
  result &&= (resource_name == @expected_resource.to_s) unless @expected_resource.nil?
  result &&= (action_name == @expected_action.to_s) unless @expected_action.nil?
  result &&= @block.call(params) if @block
  result &&= assert_params(params)
  result
end
mocha_inspect() click to toggle source
# File lib/hammer_cli_foreman/testing/api_expectations.rb, line 27
def mocha_inspect
  res = @expected_resource.nil? ? 'any_resource' : ":#{@expected_resource}"
  act = @expected_action.nil? ? 'any_action' : ":#{@expected_action}"
  blk = @block ? '&block' : '*any_argument'
  "#{res}, #{act}, #{blk}"
end

Protected Instance Methods

assert_params(params) click to toggle source
# File lib/hammer_cli_foreman/testing/api_expectations.rb, line 35
def assert_params(params)
  stringify_keys(params) == deep_merge_hash(stringify_keys(params), stringify_keys(@expected_params))
end
deep_merge_hash(h, other_h) click to toggle source
# File lib/hammer_cli_foreman/testing/api_expectations.rb, line 39
def deep_merge_hash(h, other_h)
  h = h.clone
  h.merge!(other_h) do |key, old_val, new_val|
    if old_val.is_a?(Hash) && new_val.is_a?(Hash)
      deep_merge_hash(old_val, new_val)
    else
      new_val
    end
  end
end
stringify_keys(hash) click to toggle source
# File lib/hammer_cli_foreman/testing/api_expectations.rb, line 50
def stringify_keys(hash)
  hash.inject({}) do |stringified, (key, value)|
    if value.is_a?(Hash)
      value = stringify_keys(value)
    end
    stringified.update(key.to_s => value)
  end
end