class EmployeeWriter attr_accessor :employee, :output_file def initialize(employee, output_file) @employee = employee @output_file = output_file end def export_to_pdf Prawn::Document.generate(output_file) do bounding_box([0, cursor], width: 200) do text “Name: #{employee.name}” text “Employee id: #{employee.employee_id}” text “Occupation: #{employee.occupation}” stroke_bounds end end And we have Employee class
class Employee
attr_accessor :name, :employee_id, :occupation
def initialize(name, employee_id, occupation)
@name = name
@employee_id = employee_id
@occupation = occupation
end
end
Now
EmployeeWriter.new(
Employee.new("name", "employee_id", "occupation")
'filename.pdf'
).export_to_pdf
will give us the following pdf
Now let us write test which ensure pdf has right contents. We read the data using Pdfreader
First get the pdf-reader
gem install pdf-reader -v 1.4.0
RSpec.describe EmployeeWriter do
it "should have correct content" do
employee = Employee.new("employee_name", "employee_id", "developer")
EmployeeWriter.new(employee, 'employee.pdf').export_to_pdf
reader = PDF::Reader.new('employee.pdf')
pdf_content = reader.pages[0].to_s
expect(pdf_content).to include('Name: employee_name')
expect(pdf_content).to include('Employee id: employee_id')
expect(pdf_content).to include('Occupation: developer')
end
end
]]>
