rodolfo 1.0.1.pre.pre → 2.0.0.pre.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 68773cd4b01068d325a4be05c192b943fdbf6c5a
4
- data.tar.gz: 7927974f44f4e4cd7065efa3e3984f9163310a7d
3
+ metadata.gz: d2b0721e70e5346041cdfbf0c46ee84b67220cc5
4
+ data.tar.gz: 7a1f3b769ef3aed6dd24bffa95f18d03d86becb6
5
5
  SHA512:
6
- metadata.gz: 1054b5eebdb65342242d126f24e73493bb0818b407e2aa98cdb465a2fba07d9b288263cb4e5d0d978152ac89472d5ced1501454b45eb01e293a252a68cfe92e5
7
- data.tar.gz: acd180749aba6c8d747d422aa690c9fbbbffbe6ecb6fb17c30bb875cdbdc7c16b3f5175cc54cced180be982a65c3199f094d15fde97adb87fcb6fcba9a97632c
6
+ metadata.gz: acf315467d8d0de4ba117b64f05d6cec9f03169dbee28dd3b8f84cd6907cbe6abc5850750eb69c6bc517af8268378e71634467e964de1099eae2daf6a2db195f
7
+ data.tar.gz: 22f66c10d3c83ab6ce896bbedac939717c36a031e9522d46ddbcf3f1e7b047f6f5ea18f43534176c3477c7b18cc85805d12f2f21683e0ca8c8653c978f98e9c8
data/bin/rodolfo CHANGED
@@ -1,44 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  root_path = File.dirname __dir__
4
-
5
- require 'json'
6
- require 'slop'
7
- require File.join(root_path, 'lib', 'rodolfo')
8
-
9
- opts = Slop.parse do |o|
10
- o.string '-p', '--package', 'package name'
11
- o.on '--schema', 'get the package json schema'
12
- o.on '--skip-validation', 'skip json schema validation'
13
- o.on '--version', 'print the version' do
14
- require File.join(root_path, 'lib', 'meta')
15
- STDOUT.write Rodolfo::VERSION
16
- exit 0
17
- end
18
- end
19
-
20
- unless opts.package?
21
- puts opts
22
- exit 1
23
- end
24
-
25
- if opts.schema?
26
- STDOUT.write Rodolfo::Package.new(opts[:package], {}).schema
27
- exit 0
28
- end
29
-
30
- data = $stdin.tty? ? {} : JSON.parse($stdin.read, symbolize_names: true)
31
- package = Rodolfo::Package.new opts[:package], data
32
-
33
- unless opts.skip_validation? || package.valid?
34
- STDOUT.write JSON.dump(package.validation_errors)
35
- exit 2
36
- end
37
-
38
- begin
39
- STDOUT.write package.make
40
- exit 0
41
- rescue NoMethodError
42
- STDOUT.write 'Missing or incorrect data, template can\'t be rendered'
43
- exit 2
44
- end
4
+ rodolfo_path = File.join(root_path, 'lib', 'rodolfo')
5
+ require File.join(rodolfo_path, 'cli')
6
+ Rodolfo::CLI.start(ARGV)
@@ -0,0 +1,41 @@
1
+ require 'thor'
2
+ require_relative 'exceptions'
3
+ require_relative 'meta'
4
+ require_relative 'renderer'
5
+
6
+ module Rodolfo
7
+ # Rodolfo CLI
8
+ class CLI < Thor
9
+ def self.exit_on_failure?
10
+ true
11
+ end
12
+
13
+ map %w(--version -v) => :__print_version
14
+
15
+ desc '--version, -v', 'print the version'
16
+ def __print_version
17
+ puts VERSION
18
+ end
19
+
20
+ desc 'schema PACKAGE PATH', 'print the package json schema'
21
+ def schema(package_path)
22
+ puts Rodolfo::Renderer.new(package_path, {}).json_schema
23
+ end
24
+
25
+ desc 'render PACKAGE PATH', 'render a rodolfo package path'
26
+ def render(package_path)
27
+ data = $stdin.tty? ? {} : JSON.parse($stdin.read, symbolize_names: true)
28
+ package = Rodolfo::Renderer.new package_path, data
29
+ STDOUT.write package.render
30
+ exit 0
31
+ rescue RenderError, SchemaValidationError => error
32
+ STDOUT.write error.errors
33
+ exit 2
34
+ end
35
+
36
+ def help(*args, &block)
37
+ puts "Rodolfo v#{Rodolfo::VERSION}"
38
+ super(*args, &block)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ module Rodolfo
2
+ # Package rendering failed
3
+ class RenderError < RuntimeError
4
+ attr_reader :errors
5
+
6
+ def initialize(errors)
7
+ @errors = errors
8
+ end
9
+ end
10
+
11
+ # Schema validation failed
12
+ class SchemaValidationError < RuntimeError
13
+ attr_reader :errors
14
+
15
+ def initialize(errors)
16
+ @errors = errors
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+ require 'json-schema'
3
+ require_relative 'exceptions'
4
+
5
+ # Create PDFs from the CLI using Prawn
6
+ module Rodolfo
7
+ # Rodolfo Package JSON Schema
8
+ class JSONSchema
9
+ def initialize(path)
10
+ @path = path
11
+ end
12
+
13
+ # Validate the json schema
14
+ # May raise a SchemaValidationError
15
+ def validate(data)
16
+ opts = { errors_as_objects: true, insert_defaults: true,
17
+ strict: true }
18
+ errors = JSON::Validator.fully_validate json, data, opts
19
+ raise SchemaValidationError, errors unless errors.empty?
20
+ end
21
+
22
+ def json
23
+ @json ||= File.read @path
24
+ end
25
+
26
+ def to_s
27
+ json
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,4 @@
1
- ##
2
1
  # Create PDFs from the CLI using Prawn
3
2
  module Rodolfo
4
- VERSION = '1.0.1-pre'.freeze
3
+ VERSION = '2.0.0-pre'.freeze
5
4
  end
@@ -0,0 +1,46 @@
1
+ require 'pathname'
2
+ require 'prawn'
3
+ require 'prawn/measurements'
4
+ require 'prawn/measurement_extensions'
5
+ require 'prawn/table'
6
+
7
+ require_relative 'json_schema'
8
+ require_relative 'exceptions.rb'
9
+
10
+ # Create PDFs from the CLI using Prawn
11
+ module Rodolfo
12
+ ##
13
+ # Requires a filesystem folder path which should contain
14
+ # - schema.json to perform the validation
15
+ # - data.json as example data (for testing purposes only and valid example)
16
+ # - template.rb the pdf generator itself
17
+ #
18
+ # It renders the the given template validating
19
+ # the data against that json schema
20
+ class Renderer
21
+ attr_reader :data, :validation_errors
22
+
23
+ def initialize(path, data)
24
+ @path = Pathname.new(path).absolute? ? path : File.join(Dir.pwd, path)
25
+ @data = data
26
+ @schema = JSONSchema.new File.join(@path, 'schema.json')
27
+ end
28
+
29
+ # Get the package json schema
30
+ def json_schema
31
+ @schema.to_s
32
+ end
33
+
34
+ # Render the template
35
+ # May raises a ValidationError
36
+ def render
37
+ @schema.validate(@data)
38
+ require File.join @path, 'template'
39
+ Rodolfo::Template.new(@data).render
40
+
41
+ rescue NoMethodError
42
+ msg = 'Missing or incorrect data, template can\'t be rendered'
43
+ raise RenderError, [msg]
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rodolfo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1.pre.pre
4
+ version: 2.0.0.pre.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Initios
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-10 00:00:00.000000000 Z
11
+ date: 2016-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -59,25 +59,19 @@ dependencies:
59
59
  - !ruby/object:Gem::Version
60
60
  version: 0.2.2
61
61
  - !ruby/object:Gem::Dependency
62
- name: slop
62
+ name: thor
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: 4.4.1
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: 4.4.1
67
+ version: 0.19.1
71
68
  type: :runtime
72
69
  prerelease: false
73
70
  version_requirements: !ruby/object:Gem::Requirement
74
71
  requirements:
75
72
  - - "~>"
76
73
  - !ruby/object:Gem::Version
77
- version: 4.4.1
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: 4.4.1
74
+ version: 0.19.1
81
75
  - !ruby/object:Gem::Dependency
82
76
  name: aruba
83
77
  requirement: !ruby/object:Gem::Requirement
@@ -246,8 +240,11 @@ extensions: []
246
240
  extra_rdoc_files: []
247
241
  files:
248
242
  - bin/rodolfo
249
- - lib/meta.rb
250
- - lib/rodolfo.rb
243
+ - lib/rodolfo/cli.rb
244
+ - lib/rodolfo/exceptions.rb
245
+ - lib/rodolfo/json_schema.rb
246
+ - lib/rodolfo/meta.rb
247
+ - lib/rodolfo/renderer.rb
251
248
  homepage: https://github.com/initios/rodolfo
252
249
  licenses:
253
250
  - MIT
data/lib/rodolfo.rb DELETED
@@ -1,44 +0,0 @@
1
- require 'json'
2
- require 'json-schema'
3
- require 'pathname'
4
- require 'prawn'
5
- require 'prawn/measurements'
6
- require 'prawn/measurement_extensions'
7
- require 'prawn/table'
8
-
9
- ##
10
- # Create PDFs from the CLI using Prawn
11
- module Rodolfo
12
- ##
13
- # Represents a filesystem folder which should contain
14
- # - schema.json to perform the validation
15
- # - data.json as example data
16
- # - template.rb the pdf generator itself
17
- #
18
- # It handles methods for manipulate those files and the pdf generation
19
- class Package
20
- attr_reader :data, :validation_errors
21
-
22
- def initialize(path, data)
23
- @path = Pathname.new(path).absolute? ? path : File.join(Dir.pwd, path)
24
- opts = { errors_as_objects: true, insert_defaults: true }
25
- @validation_errors = JSON::Validator.fully_validate(schema, data, opts)
26
- @data = data
27
- end
28
-
29
- def schema
30
- json_file_path = File.join @path, 'schema.json'
31
- File.read json_file_path
32
- end
33
-
34
- def valid?
35
- @validation_errors.empty?
36
- end
37
-
38
- def make
39
- template_file_path = File.join @path, 'template'
40
- require template_file_path
41
- Rodolfo::Template.new(@data).render
42
- end
43
- end
44
- end