Class: EmailSpec::Matchers::HaveHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/email_spec/matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, value) ⇒ HaveHeader

Returns a new instance of HaveHeader.



303
304
305
# File 'lib/email_spec/matchers.rb', line 303

def initialize(name, value)
  @expected_name, @expected_value = name, value
end

Instance Method Details

#descriptionObject



307
308
309
310
311
312
313
# File 'lib/email_spec/matchers.rb', line 307

def description
  if @expected_value.is_a?(String)
    "have header #{@expected_name}: #{@expected_value}"
  else
    "have header #{@expected_name} with value matching #{@expected_value.inspect}"
  end
end

#failure_messageObject



324
325
326
327
328
329
330
# File 'lib/email_spec/matchers.rb', line 324

def failure_message
  if @expected_value.is_a?(String)
    "expected the headers to include '#{@expected_name}: #{@expected_value}' but they were #{mail_headers_hash(@given_header).inspect}"
  else
    "expected the headers to include '#{@expected_name}' with a value matching #{@expected_value.inspect} but they were #{mail_headers_hash(@given_header).inspect}"
  end
end

#mail_headers_hash(email_headers) ⇒ Object



340
341
342
# File 'lib/email_spec/matchers.rb', line 340

def mail_headers_hash(email_headers)
  email_headers.fields.inject({}) { |hash, field| hash[field.field.class::FIELD_NAME] = field.to_s; hash }
end

#matches?(email) ⇒ Boolean

Returns:

  • (Boolean)


315
316
317
318
319
320
321
322
# File 'lib/email_spec/matchers.rb', line 315

def matches?(email)
@given_header = email.header

if @expected_value.is_a?(String)
  @given_header[@expected_name].to_s == @expected_value
else
  @given_header[@expected_name].to_s =~ @expected_value
end      end

#negative_failure_messageObject



332
333
334
335
336
337
338
# File 'lib/email_spec/matchers.rb', line 332

def negative_failure_message
  if @expected_value.is_a?(String)
    "expected the headers not to include '#{@expected_name}: #{@expected_value}' but they were #{mail_headers_hash(@given_header).inspect}"
  else
    "expected the headers not to include '#{@expected_name}' with a value matching #{@expected_value.inspect} but they were #{mail_headers_hash(@given_header).inspect}"
  end
end