Class: Rodolfo::Renderer
- Inherits:
-
Object
- Object
- Rodolfo::Renderer
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#validation_errors ⇒ Object
readonly
Returns the value of attribute validation_errors.
Instance Method Summary collapse
-
#initialize(path, data, strict = false) ⇒ Renderer
constructor
A new instance of Renderer.
-
#json_schema ⇒ Object
Get the recipe json schema.
- #pdf_meta ⇒ Object
-
#render ⇒ Object
Render the template.
-
#validated_data ⇒ Object
Validates the data against the json schema.
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
21 22 23 |
# File 'lib/rodolfo/renderer.rb', line 21 def data @data end |
#validation_errors ⇒ Object (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_schema ⇒ Object
Get the recipe json schema
31 32 33 |
# File 'lib/rodolfo/renderer.rb', line 31 def json_schema @schema.to_s end |
#pdf_meta ⇒ Object
40 41 42 43 44 45 |
# File 'lib/rodolfo/renderer.rb', line 40 def { CreationDate: Time.now.iso8601, JsonSchema: @schema.to_h, Payload: validated_data, Renderer: "Rodolfo v#{VERSION}" } end |
#render ⇒ Object
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: ).render rescue NoMethodError msg = 'Missing or incorrect data, template can\'t be rendered' raise RenderError, [msg] end |
#validated_data ⇒ Object
Validates the data against the json schema
36 37 38 |
# File 'lib/rodolfo/renderer.rb', line 36 def validated_data @schema.validate(@data) end |