simple-liquid 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/Gemfile +2 -0
- data/LICENSE +2 -0
- data/README.md +31 -0
- data/Rakefile +8 -0
- data/bin/console +8 -0
- data/lib/simple/liquid.rb +55 -0
- data/simple-liquid.gemspec +14 -0
- data/test/simple/liquid_test.rb +52 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '093b30f70350434d5c195d1c510534c87c628a821580284f6cb56ce69715ba0c'
|
4
|
+
data.tar.gz: 0ee9883ced270bfff45af039211deda7137c38a7d8b92a50b105ade29ffa70ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e34a9aac5c46bd613761e86adbae4942c48693ddbb9fad3c4bd67e41e9fb7f1ad25f0dbc90cd9abc9567190ca76bf86ad6eb385a8331b65d344bfcb113563f8
|
7
|
+
data.tar.gz: ba03e07892d6f7b783834b674d3678717642b4cc9519b437d51f9294f9696857cb56befbf7fc60c9b5865ac4b461cffaf6e4b79912c6f91c333331204d35b15f
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Simple::Hash
|
2
|
+
|
3
|
+
safe simple way to render a liquid template
|
4
|
+
|
5
|
+
## installation
|
6
|
+
|
7
|
+
$ gem install simple-liquid
|
8
|
+
|
9
|
+
**or**
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'simple-liquid'
|
13
|
+
```
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
Simple::Liquid.render('hello {{ name | default: "?" }}', { name: nil })
|
21
|
+
# => hello ?
|
22
|
+
|
23
|
+
Simple::Liquid.render('hello {{ location }}', { name: "0xfabe" })
|
24
|
+
# => Simple::Liquid::Error (undefined variable location)
|
25
|
+
|
26
|
+
Simple::Liquid.render('hello {{ location }} {{ something }}', { name: "0xfabe" })
|
27
|
+
# => Simple::Liquid::Error (undefined variable location, undefined variable something)
|
28
|
+
|
29
|
+
Simple::Liquid.render("{{ a | oops }}", { a: 1 })
|
30
|
+
# => Simple::Liquid::Error (undefined filter oops)
|
31
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'liquid'
|
2
|
+
|
3
|
+
module Simple
|
4
|
+
end
|
5
|
+
|
6
|
+
class Simple::Liquid
|
7
|
+
class Error < RuntimeError
|
8
|
+
end
|
9
|
+
|
10
|
+
VERSION = '1.0.0'
|
11
|
+
|
12
|
+
attr_reader :template, :object
|
13
|
+
|
14
|
+
def initialize(template, object = {})
|
15
|
+
@template = template
|
16
|
+
@object = deep_stringify_keys(object.to_h)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.render(template, object = {})
|
20
|
+
new(template, object).render
|
21
|
+
end
|
22
|
+
|
23
|
+
def liquid_template
|
24
|
+
@liquid_template ||= ::Liquid::Template.parse(template, error_mode: :strict)
|
25
|
+
end
|
26
|
+
|
27
|
+
def render
|
28
|
+
liquid_template.render(
|
29
|
+
object,
|
30
|
+
strict_variables: true,
|
31
|
+
strict_filters: true
|
32
|
+
).tap { raise_if_errors! }
|
33
|
+
end
|
34
|
+
|
35
|
+
def raise_if_errors!
|
36
|
+
errors = liquid_template.errors
|
37
|
+
return if errors.empty?
|
38
|
+
messages = errors.map { |error| error.message.gsub('Liquid error:', '').strip }
|
39
|
+
raise Simple::Liquid::Error.new(messages.join(', '))
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def deep_stringify_keys(object)
|
45
|
+
if object.is_a?(Array)
|
46
|
+
object.map { |value| deep_stringify_keys(value) }
|
47
|
+
elsif object.is_a?(Hash)
|
48
|
+
object.transform_keys(&:to_s).transform_values do |value|
|
49
|
+
deep_stringify_keys(value)
|
50
|
+
end
|
51
|
+
else
|
52
|
+
object
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require_relative "lib/simple/liquid"
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "simple-liquid"
|
5
|
+
spec.version = Simple::Liquid::VERSION
|
6
|
+
spec.authors = ["localhostdotdev"]
|
7
|
+
spec.email = ["localhostdotdev@protonmail.com"]
|
8
|
+
spec.summary = "safe simple way to render a liquid template"
|
9
|
+
spec.homepage = "https://github.com/simple-updates/simple-liquid"
|
10
|
+
spec.license = "MIT"
|
11
|
+
spec.files = `git ls-files`.split("\n")
|
12
|
+
spec.require_paths = ["lib"]
|
13
|
+
spec.add_dependency "liquid", "~> 4.0"
|
14
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require_relative '../../lib/simple/liquid'
|
3
|
+
|
4
|
+
class TestSimpleLiquid < Minitest::Test
|
5
|
+
extend MiniTest::Spec::DSL
|
6
|
+
|
7
|
+
@@counter = 0
|
8
|
+
|
9
|
+
def self.it(&block)
|
10
|
+
@@counter += 1
|
11
|
+
|
12
|
+
define_method("test_#{@@counter}", &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def assert_raises_message(message, &block)
|
16
|
+
error = nil
|
17
|
+
|
18
|
+
begin
|
19
|
+
block.call
|
20
|
+
rescue => e
|
21
|
+
error = e
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_includes(error&.message.to_s, message)
|
25
|
+
end
|
26
|
+
|
27
|
+
before do
|
28
|
+
@template = '{{ a }} {{ b }} {{ c }}'
|
29
|
+
@object = { a: 1, b: 2, c: 3 }
|
30
|
+
end
|
31
|
+
|
32
|
+
it { assert_equal("1 2 3", Simple::Liquid.render(@template, @object)) }
|
33
|
+
it { assert_equal("a", Simple::Liquid.render("a", @object)) }
|
34
|
+
|
35
|
+
it do
|
36
|
+
assert_raises_message("undefined variable d") do
|
37
|
+
Simple::Liquid.render("{{ d }}", @object)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it do
|
42
|
+
assert_raises_message("undefined variable d, undefined variable e") do
|
43
|
+
Simple::Liquid.render("{{ d }} {{ e }}", @object)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it do
|
48
|
+
assert_raises(Simple::Liquid::Error) do
|
49
|
+
Simple::Liquid.render("{{ d | oops }}", @object)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple-liquid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- localhostdotdev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: liquid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- localhostdotdev@protonmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- bin/console
|
40
|
+
- lib/simple/liquid.rb
|
41
|
+
- simple-liquid.gemspec
|
42
|
+
- test/simple/liquid_test.rb
|
43
|
+
homepage: https://github.com/simple-updates/simple-liquid
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.0.3
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: safe simple way to render a liquid template
|
66
|
+
test_files: []
|