Module: RocketPants::RSpecMatchers

Extended by:
RSpec::Matchers::DSL
Defined in:
lib/rocket_pants/rspec_matchers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.differObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/rocket_pants/rspec_matchers.rb', line 42

def self.differ
  return @_differ if instance_variable_defined?(:@_differ)
  if defined?(RSpec::Support::Differ)
    @_differ = RSpec::Support::Differ.new
  elsif defined?(RSpec::Expectations::Differ)
    @_differ = RSpec::Expectations::Differ.new
  else
    @_differ = nil
  end
end

.normalise_as_json(object, options = {}) ⇒ Object

Converts it to JSON and back again.



23
24
25
26
27
28
# File 'lib/rocket_pants/rspec_matchers.rb', line 23

def self.normalise_as_json(object, options = {})
  options = options.reverse_merge(:compact => true) if object.is_a?(Array)
  object = RocketPants::Respondable.normalise_object(object, options)
  j = ActiveSupport::JSON
  j.decode(j.encode({'response' => object}))['response']
end

.normalise_response(response, options = {}) ⇒ Object



30
31
32
# File 'lib/rocket_pants/rspec_matchers.rb', line 30

def self.normalise_response(response, options = {})
  normalise_urls normalise_as_json response, options
end

.normalise_urls(object) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/rocket_pants/rspec_matchers.rb', line 13

def self.normalise_urls(object)
  if object.is_a?(Array)
    object.each { |o| o['url'] = nil }
  elsif object.is_a?(Hash) || (defined?(APISmith::Smash) && object.is_a?(APISmith::Smash))
    object['url'] = nil
  end
  object
end

.normalised_error(e) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/rocket_pants/rspec_matchers.rb', line 5

def self.normalised_error(e)
  if e.is_a?(String) || e.is_a?(Symbol)
    Errors[e]
  else
    e
  end
end

.valid_for?(response, allowed, disallowed) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/rocket_pants/rspec_matchers.rb', line 34

def self.valid_for?(response, allowed, disallowed)
  body = response.decoded_body
  return false if body.blank?
  body = body.to_hash
  return false if body.has_key?("error")
  allowed.all? { |f| body.has_key?(f) } && !disallowed.any? { |f| body.has_key?(f) }
end

Instance Method Details

#be_api_error(error = nil) ⇒ Object



123
124
125
# File 'lib/rocket_pants/rspec_matchers.rb', line 123

def be_api_error(error = nil)
  _be_api_error error
end