jahuty 3.2.1 → 3.3.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +36 -6
- data/lib/jahuty/client.rb +6 -5
- data/lib/jahuty/response/handler.rb +6 -6
- data/lib/jahuty/service/snippet.rb +12 -9
- data/lib/jahuty/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93d706c72d12cb61d951cc71b8add1ac154ed8b8bc57374e3d558e2b4bd8d420
|
4
|
+
data.tar.gz: e64a1355073ba942c5aef652032e8268e4d3a065b8cb3f5a7594d590957edc18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33d77db35616e1ae68f3b1ad0063e51ccfc526e35f2ef04e07851f506e5103e73af9d0d27252abf817de4ce2664206cf3edd7f9972275b96aaa582c8c38d0a7b
|
7
|
+
data.tar.gz: b572333f5df0d13b18ecbf29f9d22b06400622cdf9ab5408a6963f21fe154ea0fbb67cee881b3efb89cc7417ef1ef1abcd733b0681f7d72261f8ac357e175bc1
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
+
## 3.3.0 - 2020-04-29
|
9
|
+
|
10
|
+
- Add the `prefer_latest` configuration option to the client and render methods. This setting allows you to render a snippet's _latest_ content instead of the default _published_ content.
|
11
|
+
|
8
12
|
## 3.2.1 - 2020-03-16
|
9
13
|
|
10
14
|
* Fix issue with double-enveloping the `params` query string parameter.
|
data/README.md
CHANGED
@@ -13,7 +13,7 @@ It is multi-platform, and we strive to make it run equally well on Windows, Linu
|
|
13
13
|
To install, add this line to your application's `Gemfile` and run `bundle install`:
|
14
14
|
|
15
15
|
```ruby
|
16
|
-
gem 'jahuty', '~> 3.
|
16
|
+
gem 'jahuty', '~> 3.3'
|
17
17
|
```
|
18
18
|
|
19
19
|
## Usage
|
@@ -64,9 +64,35 @@ renders = jahuty.snippets.all_renders 'YOUR_TAG'
|
|
64
64
|
renders.each { |render| puts render }
|
65
65
|
```
|
66
66
|
|
67
|
-
##
|
67
|
+
## Rendering content
|
68
68
|
|
69
|
-
You can
|
69
|
+
You can use the `prefer_latest` configuration option to render a snippet's _latest_ content to your team in _development_ and its _published_ content to your customers in _production_.
|
70
|
+
|
71
|
+
By default, Jahuty will render a snippet's _published_ content, the content that existed the last time someone clicked the "Publish" button, to avoid exposing your creative process to customers.
|
72
|
+
|
73
|
+
To render a snippet's _latest_ content, the content that currently exists in the editor, you can use the `prefer_latest` configuration option at the library or render level:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
jahuty = Jahuty::Client.new api_key: 'YOUR_API_KEY', prefer_latest: true
|
77
|
+
```
|
78
|
+
|
79
|
+
You can also prefer the latest content (or not) for a single render:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
# Render the _published_ content for all snippets...
|
83
|
+
jahuty = Jahuty::Client.new api_key: 'YOUR_API_KEY'
|
84
|
+
|
85
|
+
# ... except, render the _latest_ content for this one.
|
86
|
+
jahuty.snippets.render YOUR_SNIPPET_ID, prefer_latest: true
|
87
|
+
```
|
88
|
+
|
89
|
+
## Passing dynamic parameters
|
90
|
+
|
91
|
+
You can use the _same_ snippet to generate _different_ content by defining [variables](https://docs.jahuty.com/liquid/variables) in your snippets and setting their values via [parameters](https://docs.jahuty.com/liquid/parameters).
|
92
|
+
|
93
|
+
### Snippet parameters
|
94
|
+
|
95
|
+
Use the `params` option to pass parameters into your snippet:
|
70
96
|
|
71
97
|
```ruby
|
72
98
|
jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
|
@@ -74,12 +100,16 @@ jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
|
|
74
100
|
jahuty.snippets.render YOUR_SNIPPET_ID, params: { foo: 'bar' }
|
75
101
|
```
|
76
102
|
|
77
|
-
The parameters above would be equivalent to
|
103
|
+
The parameters above would be equivalent to assigning the following variable in your snippet:
|
78
104
|
|
79
105
|
```html
|
80
106
|
{% assign foo = "bar" %}
|
81
107
|
```
|
82
108
|
|
109
|
+
### Collection parameters
|
110
|
+
|
111
|
+
Collection parameters use a slightly different syntax.
|
112
|
+
|
83
113
|
If you're rendering a collection, the first dimension of the `params` key determines the parameters' scope. Use an asterisk key (`*`) to pass the same parameters to all snippets, or use a snippet id as key to pass parameters to a specific snippet.
|
84
114
|
|
85
115
|
```ruby
|
@@ -104,7 +134,7 @@ jahuty.snippets.all_renders 'YOUR_TAG', params: {
|
|
104
134
|
}
|
105
135
|
```
|
106
136
|
|
107
|
-
## Caching
|
137
|
+
## Caching for performance
|
108
138
|
|
109
139
|
You can use caching to control how frequently this library requests the latest content from Jahuty's API.
|
110
140
|
|
@@ -218,7 +248,7 @@ jahuty2 = Jahuty::Client.new(api_key: 'YOUR_API_KEY', expires_in: 60)
|
|
218
248
|
jahuty2.snippets.render 1, expires_in: 0
|
219
249
|
```
|
220
250
|
|
221
|
-
##
|
251
|
+
## Handling errors
|
222
252
|
|
223
253
|
If an error occurs with [Jahuty's API](https://docs.jahuty.com/api#errors), a `Jahuty::Exception::Error` will be raised:
|
224
254
|
|
data/lib/jahuty/client.rb
CHANGED
@@ -5,11 +5,12 @@ require 'mini_cache'
|
|
5
5
|
module Jahuty
|
6
6
|
# Executes requests against Jahuty's API and returns resources.
|
7
7
|
class Client
|
8
|
-
def initialize(api_key:, cache: nil, expires_in: nil)
|
9
|
-
@api_key
|
10
|
-
@cache
|
8
|
+
def initialize(api_key:, cache: nil, expires_in: nil, prefer_latest: false)
|
9
|
+
@api_key = api_key
|
10
|
+
@cache = Cache::Facade.new(cache || ::MiniCache::Store.new)
|
11
11
|
@expires_in = expires_in
|
12
|
-
@services
|
12
|
+
@services = {}
|
13
|
+
@prefer_latest = prefer_latest
|
13
14
|
end
|
14
15
|
|
15
16
|
# Allows services to be accessed as properties (e.g., jahuty.snippets).
|
@@ -17,7 +18,7 @@ module Jahuty
|
|
17
18
|
if args.empty? && name == :snippets
|
18
19
|
unless @services.key?(name)
|
19
20
|
@services[name] = Service::Snippet.new(
|
20
|
-
client: self, cache: @cache, expires_in: @expires_in
|
21
|
+
client: self, cache: @cache, expires_in: @expires_in, prefer_latest: @prefer_latest
|
21
22
|
)
|
22
23
|
end
|
23
24
|
@services[name]
|
@@ -13,9 +13,9 @@ module Jahuty
|
|
13
13
|
|
14
14
|
@resources ||= ::Jahuty::Resource::Factory.new
|
15
15
|
|
16
|
-
if collection?(
|
16
|
+
if collection?(payload)
|
17
17
|
payload.map { |data| @resources.call resource_name, data }
|
18
|
-
elsif resource?(
|
18
|
+
elsif resource?(payload)
|
19
19
|
@resources.call resource_name, payload
|
20
20
|
else
|
21
21
|
raise ArgumentError, 'Action and payload mismatch'
|
@@ -24,8 +24,8 @@ module Jahuty
|
|
24
24
|
|
25
25
|
private
|
26
26
|
|
27
|
-
def collection?(
|
28
|
-
|
27
|
+
def collection?(payload)
|
28
|
+
payload.is_a?(::Array)
|
29
29
|
end
|
30
30
|
|
31
31
|
def name_resource(action, response)
|
@@ -47,8 +47,8 @@ module Jahuty
|
|
47
47
|
(response.status < 200 || response.status >= 300)
|
48
48
|
end
|
49
49
|
|
50
|
-
def resource?(
|
51
|
-
|
50
|
+
def resource?(payload)
|
51
|
+
payload.is_a?(::Object)
|
52
52
|
end
|
53
53
|
|
54
54
|
def success?(response)
|
@@ -4,24 +4,23 @@ module Jahuty
|
|
4
4
|
module Service
|
5
5
|
# A service for interacting with snippets.
|
6
6
|
class Snippet < Base
|
7
|
-
def initialize(client:, cache:, expires_in: nil)
|
7
|
+
def initialize(client:, cache:, expires_in: nil, prefer_latest: false)
|
8
8
|
super(client: client)
|
9
9
|
|
10
10
|
@cache = cache
|
11
11
|
@expires_in = expires_in
|
12
|
+
@prefer_latest = prefer_latest
|
12
13
|
end
|
13
14
|
|
14
|
-
def all_renders(tag, params: {}, expires_in:
|
15
|
-
renders = index_renders tag: tag, params: params
|
15
|
+
def all_renders(tag, params: {}, expires_in: @expires_in, prefer_latest: @prefer_latest)
|
16
|
+
renders = index_renders tag: tag, params: params, prefer_latest: prefer_latest
|
16
17
|
|
17
18
|
cache_renders renders: renders, params: params, expires_in: expires_in
|
18
19
|
|
19
20
|
renders
|
20
21
|
end
|
21
22
|
|
22
|
-
def render(snippet_id, params: {}, expires_in:
|
23
|
-
expires_in ||= @expires_in
|
24
|
-
|
23
|
+
def render(snippet_id, params: {}, expires_in: @expires_in, prefer_latest: @prefer_latest)
|
25
24
|
key = cache_key snippet_id: snippet_id, params: params
|
26
25
|
|
27
26
|
render = @cache.read(key)
|
@@ -29,7 +28,7 @@ module Jahuty
|
|
29
28
|
@cache.delete key unless render.nil? || cacheable?(expires_in)
|
30
29
|
|
31
30
|
if render.nil?
|
32
|
-
render = show_render snippet_id: snippet_id, params: params
|
31
|
+
render = show_render snippet_id: snippet_id, params: params, prefer_latest: prefer_latest
|
33
32
|
|
34
33
|
@cache.write key, render, expires_in: expires_in if cacheable?(expires_in)
|
35
34
|
end
|
@@ -50,6 +49,8 @@ module Jahuty
|
|
50
49
|
def cache_renders(renders:, params:, expires_in: nil)
|
51
50
|
expires_in ||= @expires_in
|
52
51
|
|
52
|
+
return if renders.nil?
|
53
|
+
|
53
54
|
return unless cacheable?(expires_in)
|
54
55
|
|
55
56
|
global_params = params['*'] || {}
|
@@ -68,9 +69,10 @@ module Jahuty
|
|
68
69
|
expires_in.nil? || expires_in.positive?
|
69
70
|
end
|
70
71
|
|
71
|
-
def index_renders(tag:, params: {})
|
72
|
+
def index_renders(tag:, params: {}, prefer_latest: false)
|
72
73
|
request_params = { tag: tag }
|
73
74
|
request_params[:params] = params.to_json unless params.empty?
|
75
|
+
request_params[:latest] = 1 if prefer_latest
|
74
76
|
|
75
77
|
action = ::Jahuty::Action::Index.new(
|
76
78
|
resource: 'render',
|
@@ -80,9 +82,10 @@ module Jahuty
|
|
80
82
|
@client.request action
|
81
83
|
end
|
82
84
|
|
83
|
-
def show_render(snippet_id:, params: {})
|
85
|
+
def show_render(snippet_id:, params: {}, prefer_latest: false)
|
84
86
|
request_params = {}
|
85
87
|
request_params[:params] = params.to_json unless params.empty?
|
88
|
+
request_params[:latest] = 1 if prefer_latest
|
86
89
|
|
87
90
|
action = ::Jahuty::Action::Show.new(
|
88
91
|
id: snippet_id,
|
data/lib/jahuty/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jahuty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jack Clayton
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -238,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
238
238
|
- !ruby/object:Gem::Version
|
239
239
|
version: '0'
|
240
240
|
requirements: []
|
241
|
-
rubygems_version: 3.1.
|
241
|
+
rubygems_version: 3.1.6
|
242
242
|
signing_key:
|
243
243
|
specification_version: 4
|
244
244
|
summary: Jahuty's Ruby SDK.
|