Class: PrintMapService

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ PrintMapService

Returns a new instance of PrintMapService.



8
9
10
11
# File 'lib/print_map_service.rb', line 8

def initialize(opts = {})
  @worker = opts[:worker] ||= PrintMap::Worker.new
  @create_job = opts[:create_job] ||= proc { |opts| PrintMap::Job.new(opts) }
end

Instance Attribute Details

#create_jobObject (readonly)

Returns the value of attribute create_job.



6
7
8
# File 'lib/print_map_service.rb', line 6

def create_job
  @create_job
end

#workerObject (readonly)

Returns the value of attribute worker.



6
7
8
# File 'lib/print_map_service.rb', line 6

def worker
  @worker
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/print_map_service.rb', line 13

def call(env)
  puts 'calling print map'
  job = create_job.call(Rack::Request.new(env).params)
  if job.valid?
    puts 'valid job'
    pdf = worker.do_job(job)
    puts 'rendering to client'
    # now stream the pdf to the client
    [
      200, {
        "Content-Type" => "application/pdf", 
        "Content-Disposition" => "attachment; filename=\"#{job.title}.pdf\""
      },
      pdf.render
    ]
  else
    create_invalid_job_response(job)
  end

  rescue Exception => e
    create_error_response(e)
end

#create_error_response(e) ⇒ Object



43
44
45
46
47
48
# File 'lib/print_map_service.rb', line 43

def create_error_response(e)
  body = "An internal error occurred:\n#{e.message}"
  puts 'failed to generate print map:'
  puts body
  [500, { "Content-Type" => "text/plain" }, body]
end

#create_invalid_job_response(job) ⇒ Object



36
37
38
39
40
41
# File 'lib/print_map_service.rb', line 36

def create_invalid_job_response(job)
  body = "Print map failed because of the following errors:\n#{job.errors.full_messages.join("\n")}"
  puts 'failed to generate print map:'
  puts body
  [500, { "Content-Type" => "text/plain" }, body]
end