Class: Rodolfo::Renderer

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

Overview

Requires a filesystem folder path which should contain

  • schema.json to perform the validation

  • data.json as example data (for testing purposes only and valid example)

  • template.rb the pdf generator itself

It renders the the given template validating the data against that json schema

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, data, strict = false) ⇒ Renderer

Returns a new instance of Renderer.



23
24
25
26
27
28
# File 'lib/rodolfo/renderer.rb', line 23

def initialize(path, data, strict = false)
  @path = Pathname.new(path).absolute? ? path : File.join(Dir.pwd, path)
  @data = data
  schema_path = File.join(@path, 'schema.json')
  @schema = JSONSchema.new schema_path, strict
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/rodolfo/renderer.rb', line 21

def data
  @data
end

#validation_errorsObject (readonly)

Returns the value of attribute validation_errors.



21
22
23
# File 'lib/rodolfo/renderer.rb', line 21

def validation_errors
  @validation_errors
end

Instance Method Details

#json_schemaObject

Get the recipe json schema



31
32
33
# File 'lib/rodolfo/renderer.rb', line 31

def json_schema
  @schema.to_s
end

#pdf_metaObject



40
41
42
43
44
45
# File 'lib/rodolfo/renderer.rb', line 40

def pdf_meta
  { CreationDate: Time.now.iso8601,
    JsonSchema: @schema.to_h,
    Payload: validated_data,
    Renderer: "Rodolfo v#{VERSION}" }
end

#renderObject

Render the template



48
49
50
51
52
53
54
# File 'lib/rodolfo/renderer.rb', line 48

def render
  require File.join @path, 'template'
  Rodolfo::Template.new(validated_data, info: pdf_meta).render
rescue NoMethodError
  msg = 'Missing or incorrect data, template can\'t be rendered'
  raise RenderError, [msg]
end

#validated_dataObject

Validates the data against the json schema



36
37
38
# File 'lib/rodolfo/renderer.rb', line 36

def validated_data
  @schema.validate(@data)
end