class SpecViews::Support::SpecView

Attributes

body[R]
context[R]
directory_path[R]
example[R]
last_run_path[R]
response[R]
spec_name[R]
time[R]

Public Class Methods

new(context, example, response, time) click to toggle source
# File lib/spec_views/support.rb, line 15
def initialize(context, example, response, time)
  @context = context
  @response = response
  @example = example
  @spec_name = example.full_description.strip
                      .gsub(/[^0-9A-Za-z.\-]/, '_').gsub('__', '_')
  @spec_name = "#{@spec_name}__pdf" if pdf?
  @directory_path = Rails.root.join(Rails.configuration.spec_views.directory, spec_name)
  @last_run_path = directory_path.join('last_run.txt')
  @body = response.body
  @time = time
end

Public Instance Methods

changed?() click to toggle source
# File lib/spec_views/support.rb, line 77
def changed?
  return pdf_changed? if pdf?

  begin
    champion = read
  rescue ReadError
    champion = nil
  end
  sanitized_body != champion
end
delete_challenger() click to toggle source
# File lib/spec_views/support.rb, line 41
def delete_challenger
  FileUtils.rm_f(challenger_path) unless changed?
end
expect_eq() click to toggle source
# File lib/spec_views/support.rb, line 50
def expect_eq
  pdf? ? expect_eq_pdf : expect_eq_text
  delete_challenger
end
expect_eq_pdf() click to toggle source
# File lib/spec_views/support.rb, line 59
def expect_eq_pdf
  champion_md5 = nil
  begin
    champion_md5 = Digest::MD5.file(champion_path).hexdigest
  rescue Errno::ENOENT # rubocop:disable Lint/SuppressedException
  end
  current_md5 = Digest::MD5.hexdigest(sanitized_body)
  context.expect(current_md5).to context.eq(champion_md5), 'MD5s of PDF do not match'
end
expect_eq_text() click to toggle source
# File lib/spec_views/support.rb, line 55
def expect_eq_text
  context.expect(sanitized_body).to context.eq(read)
end
pdf_changed?() click to toggle source
# File lib/spec_views/support.rb, line 88
def pdf_changed?
  champion_md5 = Digest::MD5.file(champion_path).hexdigest
  current_md5 = Digest::MD5.hexdigest(sanitized_body)
  champion_md5 != current_md5
rescue Errno::ENOENT
  true
end
read() click to toggle source
# File lib/spec_views/support.rb, line 69
def read
  File.read(champion_path)
rescue Errno::ENOENT
  raise ReadError, "Cannot find view fixture #{champion_path.to_s.gsub(Rails.root.to_s, '')}\n" \
  "Create the file by adding the follwing to your spec:\n" \
  'spec_view.write'
end
write() click to toggle source
# File lib/spec_views/support.rb, line 28
def write
  FileUtils.mkdir_p(directory_path)
  write_to_path(champion_path, sanitized_body)
  put_write_instructions
end
write_challenger() click to toggle source
# File lib/spec_views/support.rb, line 34
def write_challenger
  return unless changed?

  FileUtils.mkdir_p(directory_path)
  write_to_path(challenger_path, sanitized_body)
end
write_last_run() click to toggle source
# File lib/spec_views/support.rb, line 45
def write_last_run
  FileUtils.mkdir_p(directory_path)
  write_to_path(last_run_path, time)
end

Private Instance Methods

challenger_path() click to toggle source
# File lib/spec_views/support.rb, line 102
def challenger_path
  pdf? ? directory_path.join('challenger.pdf') : directory_path.join('challenger.html')
end
champion_path() click to toggle source
# File lib/spec_views/support.rb, line 98
def champion_path
  pdf? ? directory_path.join('view.pdf') : directory_path.join('view.html')
end
pdf?() click to toggle source
# File lib/spec_views/support.rb, line 142
def pdf?
  response.content_type == 'application/pdf'
end
put_write_instructions() click to toggle source
# File lib/spec_views/support.rb, line 130
def put_write_instructions
  puts
  puts "\e[33mWarning:\e[0m Writing view fixture to #{champion_path.to_s.gsub(Rails.root.to_s, '')}"
  puts 'xdg-open "http://localhost:3100/spec_views/"'
end
remove_digests_from_body(body) click to toggle source
# File lib/spec_views/support.rb, line 114
def remove_digests_from_body(body)
  body.gsub(/(-[a-z0-9]{64})(\.css|\.js|\.ico|\.png|\.jpg|\.jpeg|\.svg|\.gif)/, '\2')
end
remove_headers_from_pdf(pdf) click to toggle source
# File lib/spec_views/support.rb, line 122
def remove_headers_from_pdf(pdf)
  pdf
    .force_encoding("BINARY")
    .gsub(/^\/CreationDate \(.*\)$/, '')
    .gsub(/^\/ModDate \(.*\)$/, '')
    .gsub(/^\/ID \[<.+><.+>\]$/, '')
end
remove_pack_digests_from_body(body) click to toggle source
# File lib/spec_views/support.rb, line 118
def remove_pack_digests_from_body(body)
  body.gsub(%r{(packs.*/js/[a-z0-9_]+)(-[a-z0-9]{20})(\.js)}, '\1\3')
end
sanitized_body() click to toggle source
# File lib/spec_views/support.rb, line 106
def sanitized_body
  return remove_headers_from_pdf(body) if pdf?

  remove_pack_digests_from_body(
    remove_digests_from_body(body),
  )
end
write_to_path(path, content) click to toggle source
# File lib/spec_views/support.rb, line 136
def write_to_path(path, content)
  File.open(path.to_s, pdf? ? 'wb' : 'w') do |file|
    file.write(content)
  end
end