storyblok 2.0.8 → 2.1.0

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
  SHA256:
3
- metadata.gz: 15b954d3cf0dc7ca6346d490dfe34ed0b676cbd3eef222d7091860d31c7103b4
4
- data.tar.gz: f5aaa23eac80dba3e1129c729c96aa3f57a4fcda746a6bdd14a6e914a25523fb
3
+ metadata.gz: a7788a31d2120b18b1048f242d05f5d9f2c984344bf36e44bc9519e244dd15a3
4
+ data.tar.gz: b1858aa45ffad79e7088da5ff78cbc0fd7523f8b30e7525e91da72f6e8609e41
5
5
  SHA512:
6
- metadata.gz: 69cdd57edaab0f030cd3018a0e27caa4d28dde70ca6e4b75d00bd773d71838d3e8de9346e05b53c1b8885f273037787c09a08937de6564193bde954831e91941
7
- data.tar.gz: 4680ba3b6697ad2e25bd50cb0039dc3b3a4c3652df680045e862a80a8d9625d057a388d3d0b448bf5c17c042c2cea158672f2144fde359311b8ece9eb68dc58e
6
+ metadata.gz: 2d91bbcdc949b788375f0409560c843050c8166606d73b9ed6e59d6422d33c01c5dfbde90143268cc4fe5b6605cefba1a8f1ba494c0b06e68a493c07d5d0ab39
7
+ data.tar.gz: 0164c14e04e352a1f51f598592a25e6c9266a4a3eeb646cfaafdb51c1ea7ae4244c7774e4ae349f49ea7afac261dee007871cb788fa36978be2b9f2a5c27aec0
@@ -1,23 +1,31 @@
1
1
  # bundle exec ruby examples/renderer.rb
2
2
 
3
3
  require_relative '../lib/storyblok'
4
+ require 'redis'
4
5
 
5
6
  logger = Logger.new(STDOUT)
6
7
 
8
+ redis = Redis.new(url: 'redis://localhost:6379')
9
+ cache = Storyblok::Cache::Redis.new(redis: redis)
10
+
7
11
  client = Storyblok::Client.new(
8
12
  token: '6HMYdAjBoONyuS6GIf5PdAtt',
9
13
  logger: logger,
10
14
  component_resolver: ->(component, data) {
11
15
  "Placeholder for #{component}: #{data['text']}"
12
- }
16
+ },
17
+ api_url: 'api-testing.storyblok.com',
18
+ api_version: 2,
19
+ cache: cache
13
20
  )
14
21
 
15
- puts client.render({'type' => 'doc', 'content' => [
16
- {'type' => 'paragraph', 'content' => [{'text' => 'Good', 'type' => 'text'}]},
17
- {'type' => 'blok', 'attrs' => {'body' => [{'component' => 'button', 'text' => 'Click me'}]}}
18
- ]})
19
22
 
20
- res = client.story('article/article-1')
23
+ res = client.flush
24
+ res = client.story('authors/page', {version: 'published'})
25
+ puts client.cache_version
26
+ res = client.story('authors/page', {version: 'published'})
27
+ res = client.story('authors/page', {version: 'published'})
28
+ res = client.story('authors/page', {version: 'published'})
21
29
 
22
- puts res['data']['story']['content']['intro']
23
- puts client.render(res['data']['story']['content']['intro'])
30
+ puts res['data']
31
+ #puts client.render(res['data']['story']['content']['intro'])
@@ -17,11 +17,11 @@ module Storyblok
17
17
  log_level: Logger::INFO,
18
18
  version: 'draft',
19
19
  component_resolver: ->(component, data) { '' },
20
- cache_version: Time.now.to_i,
21
20
  cache: nil
22
21
  }
23
22
 
24
23
  attr_reader :configuration, :logger
24
+ attr_accessor :cache_version
25
25
 
26
26
  # @param [Hash] given_configuration
27
27
  # @option given_configuration [String] :token Required if oauth_token is not set
@@ -33,6 +33,7 @@ module Storyblok
33
33
  # @option given_configuration [::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR] :log_level
34
34
  def initialize(given_configuration = {})
35
35
  @configuration = default_configuration.merge(given_configuration)
36
+ @cache_version = '0'
36
37
  validate_configuration!
37
38
 
38
39
  if configuration[:oauth_token]
@@ -62,7 +63,7 @@ module Storyblok
62
63
  #
63
64
  # @return [Hash]
64
65
  def space(query = {})
65
- Request.new(self, '/cdn/spaces/me', query).get
66
+ Request.new(self, '/cdn/spaces/me', query, nil, true).get
66
67
  end
67
68
 
68
69
  # Gets a collection of stories
@@ -169,20 +170,15 @@ module Storyblok
169
170
  parse_result(res)
170
171
  end
171
172
 
172
- def cached_get(request)
173
+ def cached_get(request, bypass_cache = false)
173
174
  endpoint = base_url + request.url
175
+ query = request_query(request.query)
176
+ query_string = build_nested_query(query)
174
177
 
175
- if cache.nil?
176
- query = request_query(request.query)
177
- query_string = build_nested_query(query)
178
+ if cache.nil? || bypass_cache || query[:version] == 'draft'
178
179
  result = run_request(endpoint, query_string)
179
180
  else
180
- version = cache.get('storyblok:' + configuration[:token] + ':version') || '0'
181
-
182
- query = query = request_query({ cache_version: version }.merge(request.query))
183
- query_string = build_nested_query(query)
184
-
185
- cache_key = 'storyblok:' + configuration[:token] + ':v:' + version + ':' + request.url + ':' + Base64.encode64(query_string)
181
+ cache_key = 'storyblok:' + configuration[:token] + ':v:' + query[:cv] + ':' + request.url + ':' + Base64.encode64(query_string)
186
182
 
187
183
  result = cache.cache(cache_key) do
188
184
  run_request(endpoint, query_string)
@@ -194,7 +190,7 @@ module Storyblok
194
190
 
195
191
  def flush
196
192
  unless cache.nil?
197
- cache.set('storyblok:' + configuration[:token] + ':version', Time.now.to_i.to_s)
193
+ cache.set('storyblok:' + configuration[:token] + ':version', space['data']['space']['version'])
198
194
  end
199
195
  end
200
196
 
@@ -240,14 +236,27 @@ module Storyblok
240
236
  raise
241
237
  end
242
238
 
243
- {'headers' => res.headers, 'data' => JSON.parse(res.body)}.to_json
239
+ body = JSON.parse(res.body)
240
+ self.cache_version = body['cv'] if body['cv']
241
+
242
+ unless cache.nil?
243
+ cache.set('storyblok:' + configuration[:token] + ':version', cache_version)
244
+ end
245
+
246
+ {'headers' => res.headers, 'data' => body}.to_json
244
247
  end
245
248
 
246
249
  # Patches a query hash with the client configurations for queries
247
250
  def request_query(query)
248
251
  query[:token] = configuration[:token] if query[:token].nil?
249
252
  query[:version] = configuration[:version] if query[:version].nil?
250
- query[:cv] = configuration[:cache_version] if query[:cache_version].nil?
253
+
254
+ unless cache.nil?
255
+ query[:cv] = (cache.get('storyblok:' + configuration[:token] + ':version') or cache_version) if query[:cv].nil?
256
+ else
257
+ query[:cv] = cache_version if query[:cv].nil?
258
+ end
259
+
251
260
  query
252
261
  end
253
262
 
@@ -5,14 +5,15 @@ module Storyblok
5
5
  class Request
6
6
  attr_reader :client, :type, :query, :id, :endpoint
7
7
 
8
- def initialize(client, endpoint, query = {}, id = nil)
8
+ def initialize(client, endpoint, query = {}, id = nil, bypass_cache = false)
9
9
  @client = client
10
10
  @endpoint = endpoint
11
11
  @query = query
12
+ @bypass_cache = bypass_cache
12
13
 
13
14
  if id
14
15
  @type = :single
15
- @id = URI.escape(id)
16
+ @id = URI.encode_www_form_component(id)
16
17
  else
17
18
  @type = :multi
18
19
  @id = nil
@@ -26,7 +27,7 @@ module Storyblok
26
27
 
27
28
  # Delegates the actual HTTP work to the client
28
29
  def get
29
- client.cached_get(self)
30
+ client.cached_get(self, @bypass_cache)
30
31
  end
31
32
 
32
33
  # Returns a new Request object with the same data
@@ -1,4 +1,4 @@
1
1
  module Storyblok
2
2
  # Gem Version
3
- VERSION = '2.0.8'
3
+ VERSION = '2.1.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storyblok
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.8
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Storyblok (Alexander Feiglstorfer)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-17 00:00:00.000000000 Z
11
+ date: 2020-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client