jahuty 3.1.0 → 3.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 88cfffd7646bfc5849b4f45076f7dba175a65b8a5a89734e00db137dc49ee6e4
4
- data.tar.gz: 85d9a607c72e94f041adc4fe22da7309f6228f6c07b50b49a1ad3f6c768be67c
3
+ metadata.gz: b7f9dd15bdc516d3e7249bba915822f742642f59c7209f47151b963ba34b53d1
4
+ data.tar.gz: 11bb09b250f358f13d5cafb3a585a1db525ddf988b6fea70dd19c7586a5a77a3
5
5
  SHA512:
6
- metadata.gz: d0292d5ee64c8d715a9506ef3586c0bcacddacf9c5b97a384b6bc608b119152220f3eda45f6a4b35792a7aab0bfa6895db0ed747b842ab4d45d561d0bdd7a271
7
- data.tar.gz: dba24f33415884a663f959c9a8ebf7b443773b88ade368d46a2c7026c8cc1ead6b60c481b4d5fb9f4e956ba6ef2d41dbdf12b20fb9a333a4e75544cdd9c00759
6
+ metadata.gz: 42ee47a4e2e936a10c4e14bee9990db9c170e53e852d257190cf7636d8c27ce6e7f4da04eeb1cf1ea333e00f65ffc7ed1847b0a47ec4c7ea024d067ee9bca196
7
+ data.tar.gz: e1ec48bb180b4f81145f4a6b939bc3a9fe64a792bbb7a513b705b49e61b4ac379dfbc0c38fb12880122282d12816d90c8798ca6b283bb8fce6befe5777eb3174
data/.rubocop.yml CHANGED
@@ -4,6 +4,8 @@ require:
4
4
  AllCops:
5
5
  TargetRubyVersion: 2.6
6
6
  NewCops: enable
7
+ Exclude:
8
+ - vendor/bundle/**/*
7
9
  Metrics/BlockLength:
8
10
  Exclude:
9
11
  - 'jahuty.gemspec'
@@ -13,3 +15,9 @@ Metrics/BlockLength:
13
15
  Metrics/ModuleLength:
14
16
  Exclude:
15
17
  - 'spec/**/*.rb'
18
+ RSpec/ExampleLength:
19
+ Exclude:
20
+ - 'spec/jahuty/system_spec.rb'
21
+ RSpec/MultipleExpectations:
22
+ Exclude:
23
+ - 'spec/jahuty/system_spec.rb'
data/CHANGELOG.md CHANGED
@@ -5,7 +5,33 @@ 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.1.0 - 2020-01-04
8
+ ## 3.3.1 - 2021-05-02
9
+
10
+ ### Fixed
11
+
12
+ - Fixed [#19](https://github.com/jahuty/jahuty-ruby/issues/19) where renders were cached without regard for their content version.
13
+
14
+ ## 3.3.0 - 2021-04-29
15
+
16
+ - 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.
17
+
18
+ ## 3.2.1 - 2021-03-16
19
+
20
+ - Fix issue with double-enveloping the `params` query string parameter.
21
+ - Add system tests for params, caching, problems, and collections.
22
+
23
+ ## 3.2.0 - 2021-03-08
24
+
25
+ - Added collections to the library with `all_renders` method. This was a rather large change and required adding new objects like `Action::Show`, refactoring old ones like `Resource::Factory`, and removing some objects like `Cache::Manager` and `Service::Factory` which added unnecessary complexity.
26
+ - Added `snippet_id` to `Resource::Render` to help keep track of a render's parent snippet.
27
+
28
+ ## 3.1.1 - 2021-02-26
29
+
30
+ - Add support for extra, unused attributes returned by the API to support evolution.
31
+ - Fix the `method redefined` warnings in `cache/manager_spec.rb` and `cache/facade_spec.rb`.
32
+ - Fix the `expect { }.not_to raise_error(SpecificErrorClass)` false positives warnings in `resource/render_spec.rb`.
33
+
34
+ ## 3.1.0 - 2021-01-04
9
35
 
10
36
  - Add caching support for any cache implementation that supports `get/set` or `read/write` methods.
11
37
  - Default to using in-memory [mini-cache](https://github.com/derrickreimer/mini_cache) storage.
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.1'
16
+ gem 'jahuty', '~> 3.3'
17
17
  ```
18
18
 
19
19
  ## Usage
@@ -26,7 +26,7 @@ jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
26
26
  puts jahuty.snippets.render YOUR_SNIPPET_ID
27
27
  ```
28
28
 
29
- You can also access the render's content with `to_s` or `content`:
29
+ You can access the render's content with `to_s` or `content`:
30
30
 
31
31
  ```ruby
32
32
  jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
@@ -54,9 +54,45 @@ In an HTML view:
54
54
  </body>
55
55
  ```
56
56
 
57
- ## Parameters
57
+ You can also use tags to render a collection of snippets with the `snippets.all_renders` method:
58
58
 
59
- You can [pass parameters](https://docs.jahuty.com/liquid/parameters) into your snippet using the `params` option:
59
+ ```ruby
60
+ jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
61
+
62
+ renders = jahuty.snippets.all_renders 'YOUR_TAG'
63
+
64
+ renders.each { |render| puts render }
65
+ ```
66
+
67
+ ## Rendering content
68
+
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:
60
96
 
61
97
  ```ruby
62
98
  jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
@@ -64,13 +100,41 @@ jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
64
100
  jahuty.snippets.render YOUR_SNIPPET_ID, params: { foo: 'bar' }
65
101
  ```
66
102
 
67
- The parameters above would be equivalent to [assigning the variable](https://docs.jahuty.com/liquid/variables) below in your snippet:
103
+ The parameters above would be equivalent to assigning the following variable in your snippet:
68
104
 
69
105
  ```html
70
106
  {% assign foo = "bar" %}
71
107
  ```
72
108
 
73
- ## Caching
109
+ ### Collection parameters
110
+
111
+ Collection parameters use a slightly different syntax.
112
+
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.
114
+
115
+ ```ruby
116
+ jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
117
+
118
+ jahuty.snippets.all_renders 'YOUR_TAG', params: {
119
+ '*' => { foo: 'bar' },
120
+ '1' => { baz: 'qux' }
121
+ }
122
+ ```
123
+
124
+ This will pass the params `{ foo: 'bar' }` to all snippets, except for snippet `1`, which will be passed `{ foo: 'bar', baz: 'qux' }`.
125
+
126
+ The two parameter lists will be merged recursively, and parameters for a specific snippet will take precedence over parameters for all snippets. For example, the parameter `foo` will be assigned the value `"bar"` for all snippets, except for snippet `1`, where it will be assigned the value `"qux"`:
127
+
128
+ ```ruby
129
+ jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY')
130
+
131
+ jahuty.snippets.all_renders 'YOUR_TAG', params: {
132
+ '*' => { foo: 'bar' },
133
+ '1' => { foo: 'qux' }
134
+ }
135
+ ```
136
+
137
+ ## Caching for performance
74
138
 
75
139
  You can use caching to control how frequently this library requests the latest content from Jahuty's API.
76
140
 
@@ -133,22 +197,44 @@ jahuty = Jahuty::Client.new(
133
197
  )
134
198
  ```
135
199
 
136
- If this library's default `:expires_in` is set, it will take precedence over the default `:expires_is` of the caching implementation.
200
+ If this library's default `:expires_in` is set, it will take precedence over the default `:expires_in` of the caching implementation.
137
201
 
138
202
  #### Configuring a render's `:expires_in`
139
203
 
140
- You can configure a single render's `:expires_in` by passing an integer number of seconds via its `:expires_in` configuration option:
204
+ You can configure `:expires_in` for individual renders by passing an integer number of seconds via the render method's `:expires_in` configuration option:
141
205
 
142
206
  ```ruby
143
- # Default to the caching implementation's :expires_in for all renders.
144
- jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY', cache: cache)
207
+ # Cache all renders 300 seconds (five minutes).
208
+ jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY', cache: cache, expires_in: 300)
145
209
 
146
210
  # Except, cache this render for 60 seconds.
147
- render = jahuty.snippets.render(1, expires_in: 60)
211
+ render = jahuty.snippets.render YOUR_SNIPPET_ID, expires_in: 60
212
+
213
+ # Except, cache the renders in this collection for 120 seconds.
214
+ render = jahuty.snippets.all_renders 'YOUR_TAG', expires_in: 120
148
215
  ```
149
216
 
150
217
  If a render's `:expires_in` is set, it will take precedence over the library's default `:expires_in` and the caching implementation's `:expires_in`.
151
218
 
219
+ ### Caching collections
220
+
221
+ By default, this library will cache each render returned by `all_renders`:
222
+
223
+ ```ruby
224
+ jahuty = Jahuty::Client.new(api_key: 'YOUR_API_KEY', cache: cache)
225
+
226
+ # Sends a network request, caches each render, and returns the collection.
227
+ jahuty.snippets.all_renders 'YOUR_TAG';
228
+
229
+ # If this reder exists in the collection, the cached value will be used instead
230
+ # of sending a network request for the latest version.
231
+ jahuty.snippets.render YOUR_SNIPPET_ID;
232
+ ```
233
+
234
+ This is a powerful feature, especially when combined with a persistent cache. Using the `all_renders` method, you can render and cache an arbitrarily large chunk of content with a single network request. Because any subsequent call to `render` a snippet in the collection will use its cached version, you can reduce the number of network requests to load your content.
235
+
236
+ This method is even more powerful when combined with an asynchronous background job. When `all_renders` can be called outside your request cycle periodically, you can turn your cache into your content storage mechanism. You can render and cache dynamic content as frequently as you like without any hit to your application's response time.
237
+
152
238
  ### Disabling caching
153
239
 
154
240
  You can disable caching, even the default in-memory caching, by passing an `:expires_in` of zero (`0`) or a negative integer (e.g., `-1`) via any of the methods described above. For example:
@@ -159,10 +245,10 @@ jahuty1 = Jahuty::Client.new(api_key: 'YOUR_API_KEY', expires_in: 0)
159
245
 
160
246
  # Disable caching for this render.
161
247
  jahuty2 = Jahuty::Client.new(api_key: 'YOUR_API_KEY', expires_in: 60)
162
- jahuty2.snippets.render(1, expires_in: 0)
248
+ jahuty2.snippets.render 1, expires_in: 0
163
249
  ```
164
250
 
165
- ## Errors
251
+ ## Handling errors
166
252
 
167
253
  If an error occurs with [Jahuty's API](https://docs.jahuty.com/api#errors), a `Jahuty::Exception::Error` will be raised:
168
254
 
data/lib/jahuty.rb CHANGED
@@ -3,12 +3,12 @@
3
3
  require 'jahuty/version'
4
4
 
5
5
  require 'jahuty/action/base'
6
+ require 'jahuty/action/index'
6
7
  require 'jahuty/action/show'
7
8
 
8
9
  require 'jahuty/api/client'
9
10
 
10
11
  require 'jahuty/cache/facade'
11
- require 'jahuty/cache/manager'
12
12
 
13
13
  require 'jahuty/exception/error'
14
14
 
@@ -19,12 +19,15 @@ require 'jahuty/resource/problem'
19
19
  require 'jahuty/resource/render'
20
20
  require 'jahuty/resource/factory'
21
21
 
22
+ require 'jahuty/response/handler'
23
+
22
24
  require 'jahuty/service/base'
23
25
  require 'jahuty/service/snippet'
24
- require 'jahuty/service/factory'
25
26
 
26
27
  require 'jahuty/client'
27
28
 
29
+ require 'jahuty/util'
30
+
28
31
  module Jahuty
29
32
  BASE_URI = 'https://api.jahuty.com'
30
33
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jahuty
4
+ module Action
5
+ # Displays a collection of resources.
6
+ class Index < Base
7
+ end
8
+ end
9
+ end
@@ -20,10 +20,11 @@ module Jahuty
20
20
  def send(request)
21
21
  @client ||= Faraday.new(url: ::Jahuty::BASE_URI, headers: headers)
22
22
 
23
+ # Cnvert the action's string method to Faraday's verb-based methods.
23
24
  @client.send(
24
25
  request.method.to_sym,
25
26
  request.path,
26
- { params: request.params }
27
+ request.params
27
28
  )
28
29
  end
29
30
 
data/lib/jahuty/client.rb CHANGED
@@ -5,32 +5,28 @@ 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 = api_key
10
- @cache = cache || ::MiniCache::Store.new
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 = Service::Factory.new(client: self)
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).
16
17
  def method_missing(name, *args, &block)
17
- if args.empty? && @services.respond_to?(name)
18
- @services.send(name)
18
+ if args.empty? && name == :snippets
19
+ unless @services.key?(name)
20
+ @services[name] = Service::Snippet.new(
21
+ client: self, cache: @cache, expires_in: @expires_in, prefer_latest: @prefer_latest
22
+ )
23
+ end
24
+ @services[name]
19
25
  else
20
26
  super
21
27
  end
22
28
  end
23
29
 
24
- def fetch(action, expires_in: nil)
25
- @manager ||= Cache::Manager.new(
26
- client: self,
27
- cache: @cache,
28
- expires_in: expires_in || @expires_in
29
- )
30
-
31
- @manager.fetch(action)
32
- end
33
-
34
30
  def request(action)
35
31
  @requests ||= Request::Factory.new
36
32
 
@@ -40,17 +36,17 @@ module Jahuty
40
36
 
41
37
  response = @client.send(request)
42
38
 
43
- @resources ||= Resource::Factory.new
39
+ @responses ||= Response::Handler.new
44
40
 
45
- resource = @resources.call(action, response)
41
+ result = @responses.call(action, response)
46
42
 
47
- raise Exception::Error.new(resource), 'API responded with a problem' if resource.is_a?(Resource::Problem)
43
+ raise Exception::Error.new(result), 'API problem' if result.is_a?(Resource::Problem)
48
44
 
49
- resource
45
+ result
50
46
  end
51
47
 
52
48
  def respond_to_missing?(name, include_private = false)
53
- @services.respond_to?(name, include_private) || super
49
+ name == :snippets || super
54
50
  end
55
51
  end
56
52
  end
@@ -2,16 +2,28 @@
2
2
 
3
3
  module Jahuty
4
4
  module Request
5
- # Instantiates a request from an action. Currently, this is elementary. As
6
- # we add actions, it will become more complicated.
5
+ # Instantiates a request from an action.
7
6
  class Factory
8
7
  def call(action)
9
8
  Base.new(
10
9
  method: 'get',
11
- path: "snippets/#{action.id}/render",
10
+ path: path(action),
12
11
  params: action.params
13
12
  )
14
13
  end
14
+
15
+ private
16
+
17
+ def path(action)
18
+ case action
19
+ when ::Jahuty::Action::Show
20
+ "snippets/#{action.id}/render"
21
+ when ::Jahuty::Action::Index
22
+ 'snippets/render'
23
+ else
24
+ raise ArgumentError, 'Action is not supported'
25
+ end
26
+ end
15
27
  end
16
28
  end
17
29
  end
@@ -1,31 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
4
-
5
3
  module Jahuty
6
4
  module Resource
7
- # Negotiates the resource to return given the requested action and the
8
- # server's response.
5
+ # Instantiates and returns a resource.
9
6
  class Factory
10
7
  CLASSES = {
11
8
  problem: Problem.name,
12
9
  render: Render.name
13
10
  }.freeze
14
11
 
15
- def call(action, response)
16
- if success? response
17
- resource_name = action.resource
18
- elsif problem? response
19
- resource_name = 'problem'
20
- else
21
- raise ArgumentError, 'Unexpected response'
22
- end
23
-
24
- resource_class = class_name(resource_name.to_sym)
12
+ def call(resource_name, payload)
13
+ klass = class_name(resource_name.to_sym)
25
14
 
26
- payload = parse(response)
15
+ raise ArgumentError, "#{resource_name} missing" if klass.nil?
27
16
 
28
- Object.const_get(resource_class).send(:new, **payload)
17
+ Object.const_get(klass).send(:from, **payload)
29
18
  end
30
19
 
31
20
  private
@@ -33,18 +22,6 @@ module Jahuty
33
22
  def class_name(resource_name)
34
23
  CLASSES[resource_name.to_sym]
35
24
  end
36
-
37
- def problem?(response)
38
- response.headers['Content-Type'] == 'application/problem+json'
39
- end
40
-
41
- def parse(response)
42
- JSON.parse(response.body, symbolize_names: true)
43
- end
44
-
45
- def success?(response)
46
- response.status.between?(200, 299)
47
- end
48
25
  end
49
26
  end
50
27
  end
@@ -12,6 +12,14 @@ module Jahuty
12
12
  @type = type
13
13
  @detail = detail
14
14
  end
15
+
16
+ def self.from(data)
17
+ raise ArgumentError.new, 'Key :status missing' unless data.key?(:status)
18
+ raise ArgumentError.new, 'Key :type missing' unless data.key?(:type)
19
+ raise ArgumentError.new, 'Key :detail missing' unless data.key?(:detail)
20
+
21
+ Problem.new(data.slice(:status, :type, :detail))
22
+ end
15
23
  end
16
24
  end
17
25
  end
@@ -4,10 +4,18 @@ module Jahuty
4
4
  module Resource
5
5
  # A snippet's rendered content.
6
6
  class Render
7
- attr_accessor :content
7
+ attr_accessor :content, :snippet_id
8
8
 
9
- def initialize(content:)
9
+ def initialize(content:, snippet_id:)
10
10
  @content = content
11
+ @snippet_id = snippet_id
12
+ end
13
+
14
+ def self.from(data)
15
+ raise ArgumentError.new, 'Key :content missing' unless data.key?(:content)
16
+ raise ArgumentError.new, 'Key :snippet_id missing' unless data.key?(:snippet_id)
17
+
18
+ Render.new(data.slice(:content, :snippet_id))
11
19
  end
12
20
 
13
21
  def to_s
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Jahuty
6
+ module Response
7
+ # Inspects the response and returns the appropriate resource or collection.
8
+ class Handler
9
+ def call(action, response)
10
+ resource_name = name_resource action, response
11
+
12
+ payload = parse response
13
+
14
+ @resources ||= ::Jahuty::Resource::Factory.new
15
+
16
+ if collection?(payload)
17
+ payload.map { |data| @resources.call resource_name, data }
18
+ elsif resource?(payload)
19
+ @resources.call resource_name, payload
20
+ else
21
+ raise ArgumentError, 'Action and payload mismatch'
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def collection?(payload)
28
+ payload.is_a?(::Array)
29
+ end
30
+
31
+ def name_resource(action, response)
32
+ if success? response
33
+ action.resource
34
+ elsif problem? response
35
+ 'problem'
36
+ else
37
+ raise ArgumentError, 'Unexpected response'
38
+ end
39
+ end
40
+
41
+ def parse(response)
42
+ JSON.parse(response.body, symbolize_names: true)
43
+ end
44
+
45
+ def problem?(response)
46
+ response.headers['Content-Type'].include?('application/problem+json') &&
47
+ (response.status < 200 || response.status >= 300)
48
+ end
49
+
50
+ def resource?(payload)
51
+ payload.is_a?(::Object)
52
+ end
53
+
54
+ def success?(response)
55
+ response.headers['Content-Type'].include?('application/json') &&
56
+ response.status.between?(200, 299)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -4,12 +4,95 @@ module Jahuty
4
4
  module Service
5
5
  # A service for interacting with snippets.
6
6
  class Snippet < Base
7
- def render(id, params: {}, expires_in: nil)
8
- params = { params: params.to_json } unless params.empty?
7
+ def initialize(client:, cache:, expires_in: nil, prefer_latest: false)
8
+ super(client: client)
9
9
 
10
- action = ::Jahuty::Action::Show.new(id: id, resource: 'render', params: params)
10
+ @cache = cache
11
+ @expires_in = expires_in
12
+ @prefer_latest = prefer_latest
13
+ end
14
+
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
17
+
18
+ cache_renders renders: renders, params: params, expires_in: expires_in, latest: prefer_latest
19
+
20
+ renders
21
+ end
22
+
23
+ def render(snippet_id, params: {}, expires_in: @expires_in, prefer_latest: @prefer_latest)
24
+ key = cache_key snippet_id: snippet_id, params: params, latest: prefer_latest
25
+
26
+ render = @cache.read(key)
27
+
28
+ @cache.delete key unless render.nil? || cacheable?(expires_in)
29
+
30
+ if render.nil?
31
+ render = show_render snippet_id: snippet_id, params: params, prefer_latest: prefer_latest
32
+
33
+ @cache.write key, render, expires_in: expires_in if cacheable?(expires_in)
34
+ end
35
+
36
+ render
37
+ end
38
+
39
+ private
40
+
41
+ def cache_key(snippet_id:, params: {}, latest: false)
42
+ fingerprint = Digest::MD5.new
43
+ fingerprint << "snippets/#{snippet_id}/render/"
44
+ fingerprint << params.to_json
45
+ fingerprint << '/latest' if latest
46
+
47
+ "jahuty_#{fingerprint.hexdigest}"
48
+ end
49
+
50
+ def cache_renders(renders:, params:, expires_in: @expires_in, latest: false)
51
+ return if renders.nil?
52
+
53
+ return unless cacheable?(expires_in)
54
+
55
+ global_params = params['*'] || {}
56
+
57
+ renders.each do |render|
58
+ local_params = params[render.snippet_id.to_s] || {}
59
+ render_params = ::Jahuty::Util.deep_merge global_params, local_params
60
+
61
+ key = cache_key snippet_id: render.snippet_id, params: render_params, latest: latest
62
+
63
+ @cache.write key, render, expires_in: expires_in
64
+ end
65
+ end
66
+
67
+ def cacheable?(expires_in)
68
+ expires_in.nil? || expires_in.positive?
69
+ end
70
+
71
+ def index_renders(tag:, params: {}, prefer_latest: false)
72
+ request_params = { tag: tag }
73
+ request_params[:params] = params.to_json unless params.empty?
74
+ request_params[:latest] = 1 if prefer_latest
75
+
76
+ action = ::Jahuty::Action::Index.new(
77
+ resource: 'render',
78
+ params: request_params
79
+ )
80
+
81
+ @client.request action
82
+ end
83
+
84
+ def show_render(snippet_id:, params: {}, prefer_latest: false)
85
+ request_params = {}
86
+ request_params[:params] = params.to_json unless params.empty?
87
+ request_params[:latest] = 1 if prefer_latest
88
+
89
+ action = ::Jahuty::Action::Show.new(
90
+ id: snippet_id,
91
+ resource: 'render',
92
+ params: request_params
93
+ )
11
94
 
12
- @client.fetch action, expires_in: expires_in
95
+ @client.request action
13
96
  end
14
97
  end
15
98
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jahuty
4
+ # Utility methods.
5
+ class Util
6
+ # Deeply merges two hashes like Rails.
7
+ #
8
+ # Ideally, the API and this library could use the same method to merge
9
+ # parameters. This library's method just needs to be deterministic and not
10
+ # collide distinct combinations.
11
+ #
12
+ # @see https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/hash/deep_merge.rb
13
+ def self.deep_merge(first_hash, other_hash, &block)
14
+ first_hash.merge!(other_hash) do |key, first_val, other_val|
15
+ if first_val.is_a?(Hash) && other_val.is_a?(Hash)
16
+ deep_merge(first_val, other_val, &block)
17
+ elsif block
18
+ yield(key, first_val, other_val)
19
+ else
20
+ other_val
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Jahuty
4
- VERSION = '3.1.0'
4
+ VERSION = '3.3.1'
5
5
  end
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.1.0
4
+ version: 3.3.1
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-01-09 00:00:00.000000000 Z
11
+ date: 2021-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -199,10 +199,10 @@ files:
199
199
  - jahuty.gemspec
200
200
  - lib/jahuty.rb
201
201
  - lib/jahuty/action/base.rb
202
+ - lib/jahuty/action/index.rb
202
203
  - lib/jahuty/action/show.rb
203
204
  - lib/jahuty/api/client.rb
204
205
  - lib/jahuty/cache/facade.rb
205
- - lib/jahuty/cache/manager.rb
206
206
  - lib/jahuty/client.rb
207
207
  - lib/jahuty/exception/error.rb
208
208
  - lib/jahuty/request/base.rb
@@ -210,9 +210,10 @@ files:
210
210
  - lib/jahuty/resource/factory.rb
211
211
  - lib/jahuty/resource/problem.rb
212
212
  - lib/jahuty/resource/render.rb
213
+ - lib/jahuty/response/handler.rb
213
214
  - lib/jahuty/service/base.rb
214
- - lib/jahuty/service/factory.rb
215
215
  - lib/jahuty/service/snippet.rb
216
+ - lib/jahuty/util.rb
216
217
  - lib/jahuty/version.rb
217
218
  homepage: https://www.jahuty.com
218
219
  licenses:
@@ -237,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
238
  - !ruby/object:Gem::Version
238
239
  version: '0'
239
240
  requirements: []
240
- rubygems_version: 3.1.4
241
+ rubygems_version: 3.1.6
241
242
  signing_key:
242
243
  specification_version: 4
243
244
  summary: Jahuty's Ruby SDK.
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jahuty
4
- module Cache
5
- # Fetches the requested action from the cache or API.
6
- class Manager
7
- def initialize(cache:, client:, expires_in: nil)
8
- @client = client
9
- @cache = Facade.new(cache)
10
- @expires_in = expires_in
11
- end
12
-
13
- def fetch(action, expires_in: nil)
14
- key = key action
15
- value = @cache.read key
16
-
17
- @cache.delete key unless value.nil? || cacheable(expires_in)
18
-
19
- if value.nil?
20
- value = @client.request action
21
- @cache.write key, value, expires_in: expires_in || @expires_in if cacheable(expires_in)
22
- end
23
-
24
- value
25
- end
26
-
27
- private
28
-
29
- def cacheable(expires_in)
30
- expires_in.nil? || expires_in.positive?
31
- end
32
-
33
- def key(action)
34
- # We only build cache keys for show-render actions at this time.
35
- unless action.is_a?(::Jahuty::Action::Show) && action.resource == 'render'
36
- raise ArgumentError, 'Action must be show render'
37
- end
38
-
39
- fingerprint = Digest::MD5.new
40
- fingerprint << "snippets/#{action.id}/render/"
41
- fingerprint << action.params.to_json
42
-
43
- "jahuty_#{fingerprint.hexdigest}"
44
- end
45
- end
46
- end
47
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jahuty
4
- module Service
5
- # Instantiates the requested service and memoizes it for subsequent
6
- # requests.
7
- class Factory
8
- CLASSES = {
9
- snippets: Snippet.name
10
- }.freeze
11
-
12
- def initialize(client:)
13
- @client = client
14
- @services = {}
15
- end
16
-
17
- def method_missing(name, *args, &block)
18
- if args.empty? && class_name?(name)
19
- unless @services.key?(name)
20
- klass = class_name(name)
21
- service = Object.const_get(klass).send(:new, client: @client)
22
- @services[name] = service
23
- end
24
-
25
- @services[name]
26
- else
27
- super
28
- end
29
- end
30
-
31
- def respond_to_missing?(name, include_private = false)
32
- class_name(name) || super
33
- end
34
-
35
- private
36
-
37
- def class_name(service_name)
38
- CLASSES[service_name]
39
- end
40
-
41
- def class_name?(service_name)
42
- CLASSES.key?(service_name)
43
- end
44
- end
45
- end
46
- end