module PdfReaderControllerHelper
  def content_type
    response.headers["Content-Type"]
  end

  def content_disposition
    response.headers["Content-Disposition"]
  end

  def pdf_filename
    content_disposition.scan(/filename="(.*)"/).last.first
  end

  def pdf_body
    temp_pdf = Tempfile.new('pdf')
    temp_pdf << response.body.force_encoding('UTF-8')
    reader = PDF::Reader.new(temp_pdf)
    reader.pages.map(&:text).join(" ").squish
  end
end

module PdfReaderFeatureHelper
  def content_type
    response_headers["Content-Type"]
  end

  def content_disposition
    response_headers["Content-Disposition"]
  end

  def pdf_filename
    content_disposition.scan(/filename="(.*)"/).last.first
  end

  def pdf_body
    temp_pdf = Tempfile.new('pdf')
    temp_pdf << page.source.force_encoding('UTF-8')
    reader = PDF::Reader.new(temp_pdf)
    reader.pages.map(&:text).join(" ").squish
  end
end

RSpec.configure do |config|
  config.include PdfReaderControllerHelper, type: :controller
  config.include PdfReaderFeatureHelper, type: :feature
end

