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.



335
336
337
# File 'lib/email_spec/matchers.rb', line 335

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

Instance Method Details

#descriptionObject



339
340
341
342
343
344
345
# File 'lib/email_spec/matchers.rb', line 339

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



356
357
358
359
360
361
362
# File 'lib/email_spec/matchers.rb', line 356

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

#failure_message_when_negatedObject Also known as: negative_failure_message



364
365
366
367
368
369
370
# File 'lib/email_spec/matchers.rb', line 364

def failure_message_when_negated
  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

#mail_headers_hash(email_headers) ⇒ Object



373
374
375
376
377
378
379
380
381
382
# File 'lib/email_spec/matchers.rb', line 373

def mail_headers_hash(email_headers)
  email_headers.fields.inject({}) do |hash, field|
    if field.field.class.const_defined?('FIELD_NAME')
      hash[field.field.class::FIELD_NAME] = field.to_s
    else
      hash[field.field.class::NAME.downcase] = field.to_s
    end
    hash
  end
end

#matches?(email) ⇒ Boolean

Returns:

  • (Boolean)


347
348
349
350
351
352
353
354
# File 'lib/email_spec/matchers.rb', line 347

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