Class: SerialSpec::RequestResponse::ProvideMatcher::Provide

Inherits:
Object
  • Object
show all
Defined in:
lib/serial_spec/request_response/provide_matcher.rb

Defined Under Namespace

Classes: SerializerNotFound

Constant Summary collapse

HYPERMEDIA_ATTRIBUTES =
["links", "includes"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, options = {}) ⇒ Provide



22
23
24
25
26
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 22

def initialize(expected,options={})
  @expected       = expected
  @as_serializer  = options[:as]
  @with_root      = options[:with_root]
end

Instance Attribute Details

#actualObject (readonly)

Returns the value of attribute actual.



18
19
20
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 18

def actual
  @actual
end

#as_serializerObject (readonly)

Returns the value of attribute as_serializer.



15
16
17
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 15

def as_serializer
  @as_serializer
end

#expectedObject (readonly)

Returns the value of attribute expected.



17
18
19
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 17

def expected
  @expected
end

#with_rootObject (readonly)

Returns the value of attribute with_root.



16
17
18
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 16

def with_root
  @with_root
end

Instance Method Details

#actual_to_hash(actual) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 28

def actual_to_hash(actual)
  if actual.kind_of? SerialSpec::ParsedBody 
    strip_hypermedia(actual.execute)
  else
    strip_hypermedia(actual)
  end
end

#collection_serializerObject



47
48
49
50
51
52
53
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 47

def collection_serializer
  if as_serializer
    ActiveModel::ArraySerializer.new(expected,serializer: as_serializer, root: with_root )
  else
    ActiveModel::ArraySerializer.new(expected, root: with_root)
  end
end

#expected_to_hashObject

to_json first to normalize hash and all it’s members the parse into JSON to compare to ParsedBody hash



57
58
59
60
61
62
63
64
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 57

def expected_to_hash
  if expected.kind_of?(Array)
    #hack
    JSON.parse(collection_serializer.as_json.to_json)
  else
    JSON.parse(resource_serializer.to_json)
  end
end

#failed_message(msg) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 102

def failed_message(msg) 
  case msg
  when :response_and_model_dont_match
    "Actual and Expected do not match.\nActual   #{actual}\nExpected #{expected}" 
  when :serializer_not_specified_on_class
    "'active_model_serializer' not implemented on expected, see ehttp://bit.ly/18TdmXs"
  when :response_not_valid
    "Actual not valid Hash or Array.\nActual: #{actual}"
  else
    "no failed_message found for #{msg}"
  end
end

#failure_messageObject



115
116
117
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 115

def failure_message
  @failure_message || "error should TO  implement"
end

#failure_message_when_negatedObject



119
120
121
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 119

def failure_message_when_negated
  @failure_message || "error should not  TO implement"
end

#matches?(actual) ⇒ Boolean Also known as: ==



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 80

def matches?(actual)

  failure = catch(:failed) do
    unless actual.kind_of?(Hash) || actual.kind_of?(Array) || actual.kind_of?(ParsedBody)
      throw(:failed, :response_not_valid)
    end
    @actual    = actual_to_hash(actual) 
    @expected  = expected_to_hash 

    if @actual == @expected
      #noop - specs pass
    else
      throw(:failed, :response_and_model_dont_match)
    end
  end
  @failure_message = failed_message(failure) if failure 
  !failure
end

#normalize_data(data) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 66

def normalize_data(data)
  if data.kind_of?(Array)
    data.each_with_index do |el,index|
      data[index] = normalize_data(el)
    end
  elsif data.kind_of?(Hash)
    data.deep_stringify_keys!.to_hash
  end
end

#resource_serializerObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 36

def resource_serializer
  if as_serializer
    as_serializer.new(expected, root: with_root)
  else
    unless expected.respond_to?(:active_model_serializer)
      throw(:failed, :serializer_not_specified_on_class)
    end
    expected.active_model_serializer.new(expected,root: with_root)
  end
end

#strip_hypermedia(actual) ⇒ Object



76
77
78
# File 'lib/serial_spec/request_response/provide_matcher.rb', line 76

def strip_hypermedia(actual)
  actual.delete_if {|k,v| HYPERMEDIA_ATTRIBUTES.include?(k) }
end