storyblok 2.0.4 → 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: c997a24b49f1144f4c6a4fef322cd10328627a4063bade8ac8fc8f5c73b352fa
4
- data.tar.gz: 155cff879f86f38ce3181e0053ee907ccb23afa2b08064427b5d21c8ace3a08d
3
+ metadata.gz: a7788a31d2120b18b1048f242d05f5d9f2c984344bf36e44bc9519e244dd15a3
4
+ data.tar.gz: b1858aa45ffad79e7088da5ff78cbc0fd7523f8b30e7525e91da72f6e8609e41
5
5
  SHA512:
6
- metadata.gz: a2820713268b897df31fc3705e5fef177f40719b7729ae3f1a2daed9f158220906834d3c875376daf1e6ddfe746dc190f3347987e6f088c7f17377fa4b5ab5e6
7
- data.tar.gz: 36a26ab2e9c1ec77fbd187f1b6e05c7fadde46c21d6c2a4e8afbb99fb27b1281f31aaa876f74ce1d9f07f31f3ee722e87c4c2fc4ae55ecedf5a05b350b2a5d9e
6
+ metadata.gz: 2d91bbcdc949b788375f0409560c843050c8166606d73b9ed6e59d6422d33c01c5dfbde90143268cc4fe5b6605cefba1a8f1ba494c0b06e68a493c07d5d0ab39
7
+ data.tar.gz: 0164c14e04e352a1f51f598592a25e6c9266a4a3eeb646cfaafdb51c1ea7ae4244c7774e4ae349f49ea7afac261dee007871cb788fa36978be2b9f2a5c27aec0
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ Gemfile.lock
6
6
  .idea
7
7
  *.DS_Store
8
8
  .ruby-version
9
+ *.gem
data/Gemfile CHANGED
@@ -3,3 +3,4 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'redis'
6
+ gem 'storyblok-richtext-renderer'
data/README.md CHANGED
@@ -27,7 +27,7 @@ client = Storyblok::Client.new(token: 'YOUR_TOKEN')
27
27
 
28
28
  # Optionally set a cache client
29
29
  redis = Redis.new(url: 'redis://localhost:6379')
30
- cache = Storyblok::Cache::Redis.new(redis: Redis.current)
30
+ cache = Storyblok::Cache::Redis.new(redis: redis)
31
31
  client = Storyblok::Client.new(cache: cache, token: 'YOUR_TOKEN')
32
32
 
33
33
  # Get a story
@@ -141,6 +141,48 @@ client.put("spaces/{space_id}/stories/{story_id}", {story: {name: 'new', slug: "
141
141
  client.delete("spaces/{space_id}/stories/{story_id}")
142
142
  ```
143
143
 
144
+ ## Rendering of richtext fields
145
+
146
+ This SDK comes with a rendering service for richtext fields of Storyblok to get html output.
147
+
148
+ ### Rendering a richtext field
149
+
150
+ ```ruby
151
+ client.render(data.richtext_field)
152
+ ```
153
+
154
+ ### Define a component renderer
155
+
156
+ Storyblok's richtext field also let's you insert content blocks. To render these blocks you can define a Lambda.
157
+
158
+ ```ruby
159
+ # Option 1: Define the resolver when initializing
160
+ client = Storyblok::Client.new(
161
+ component_resolver: ->(component, data) => {
162
+ case component
163
+ when 'button'
164
+ "<button>#{data['text']}</button>"
165
+ when 'your_custom_component'
166
+ "<div class="welcome">#{data['welcome_text']}</div>"
167
+ end
168
+ }
169
+ )
170
+
171
+ # Option 2: Define the resolver afterwards
172
+ client.set_component_resolver(->(component, data) {
173
+ "#{component}"
174
+ })
175
+ ```
176
+
177
+ ### Contribute
178
+
179
+ How to build a gem file.
180
+
181
+ ~~~
182
+ gem build storyblok.gemspec
183
+ gem push storyblok-2.0.X.gem
184
+ ~~~
185
+
144
186
 
145
187
  ### License
146
188
 
@@ -5,14 +5,14 @@ require 'storyblok'
5
5
  logger = Logger.new(STDOUT)
6
6
 
7
7
  client = Storyblok::Client.new(
8
- token: 'FtyUE8zLpZox3ptNYz3dgQtt',
8
+ token: 't618GfLe1YHICBioAHnMrwtt',
9
9
  api_url: 'localhost:3001',
10
10
  secure: false,
11
11
  logger: logger
12
12
  )
13
13
 
14
- p client.datasources
15
14
  p client.stories(starts_with: 'en/news')
16
15
  p client.story('demo1')
17
16
  p client.datasource_entries(datasource: 'labels', per_page: 10)
18
17
  p client.links
18
+ p client.datasources
@@ -0,0 +1,31 @@
1
+ # bundle exec ruby examples/renderer.rb
2
+
3
+ require_relative '../lib/storyblok'
4
+ require 'redis'
5
+
6
+ logger = Logger.new(STDOUT)
7
+
8
+ redis = Redis.new(url: 'redis://localhost:6379')
9
+ cache = Storyblok::Cache::Redis.new(redis: redis)
10
+
11
+ client = Storyblok::Client.new(
12
+ token: '6HMYdAjBoONyuS6GIf5PdAtt',
13
+ logger: logger,
14
+ component_resolver: ->(component, data) {
15
+ "Placeholder for #{component}: #{data['text']}"
16
+ },
17
+ api_url: 'api-testing.storyblok.com',
18
+ api_version: 2,
19
+ cache: cache
20
+ )
21
+
22
+
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'})
29
+
30
+ puts res['data']
31
+ #puts client.render(res['data']['story']['content']['intro'])
@@ -6,7 +6,7 @@ module Storyblok
6
6
  }
7
7
 
8
8
  def initialize(*args)
9
- options = args.extract_options!
9
+ options = args.last.is_a?(::Hash) ? args.pop : {}
10
10
 
11
11
  @redis = options.delete(:redis) || begin
12
12
  if defined?(::Redis)
@@ -48,4 +48,4 @@ module Storyblok
48
48
 
49
49
  end
50
50
  end
51
- end
51
+ end
@@ -1,6 +1,7 @@
1
1
  require_relative 'request'
2
2
  require_relative 'links'
3
3
 
4
+ require 'storyblok/richtext'
4
5
  require 'rest-client'
5
6
  require 'logger'
6
7
  require 'base64'
@@ -15,21 +16,24 @@ module Storyblok
15
16
  logger: false,
16
17
  log_level: Logger::INFO,
17
18
  version: 'draft',
18
- cache_version: Time.now.to_i,
19
- cache: nil,
19
+ component_resolver: ->(component, data) { '' },
20
+ cache: nil
20
21
  }
21
22
 
22
23
  attr_reader :configuration, :logger
24
+ attr_accessor :cache_version
23
25
 
24
26
  # @param [Hash] given_configuration
25
27
  # @option given_configuration [String] :token Required if oauth_token is not set
26
28
  # @option given_configuration [String] :oauth_token Required if token is not set
27
29
  # @option given_configuration [String] :api_url
30
+ # @option given_configuration [Proc] :component_resolver
28
31
  # @option given_configuration [Number] :api_version
29
32
  # @option given_configuration [false, ::Logger] :logger
30
33
  # @option given_configuration [::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR] :log_level
31
34
  def initialize(given_configuration = {})
32
35
  @configuration = default_configuration.merge(given_configuration)
36
+ @cache_version = '0'
33
37
  validate_configuration!
34
38
 
35
39
  if configuration[:oauth_token]
@@ -38,6 +42,8 @@ module Storyblok
38
42
  })
39
43
  end
40
44
 
45
+ @renderer = Richtext::HtmlRenderer.new
46
+ @renderer.set_component_resolver(@configuration[:component_resolver])
41
47
  setup_logger
42
48
  end
43
49
 
@@ -57,7 +63,7 @@ module Storyblok
57
63
  #
58
64
  # @return [Hash]
59
65
  def space(query = {})
60
- Request.new(self, '/cdn/spaces/me', query).get
66
+ Request.new(self, '/cdn/spaces/me', query, nil, true).get
61
67
  end
62
68
 
63
69
  # Gets a collection of stories
@@ -164,16 +170,15 @@ module Storyblok
164
170
  parse_result(res)
165
171
  end
166
172
 
167
- def cached_get(request)
173
+ def cached_get(request, bypass_cache = false)
168
174
  endpoint = base_url + request.url
169
175
  query = request_query(request.query)
170
176
  query_string = build_nested_query(query)
171
177
 
172
- if cache.nil?
178
+ if cache.nil? || bypass_cache || query[:version] == 'draft'
173
179
  result = run_request(endpoint, query_string)
174
180
  else
175
- version = cache.get('storyblok:' + configuration[:token] + ':version') || '0'
176
- 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)
177
182
 
178
183
  result = cache.cache(cache_key) do
179
184
  run_request(endpoint, query_string)
@@ -185,10 +190,28 @@ module Storyblok
185
190
 
186
191
  def flush
187
192
  unless cache.nil?
188
- cache.set('storyblok:' + configuration[:token] + ':version', Time.now.to_i.to_s)
193
+ cache.set('storyblok:' + configuration[:token] + ':version', space['data']['space']['version'])
189
194
  end
190
195
  end
191
196
 
197
+ # Returns html from richtext field data
198
+ #
199
+ # @param [Hash] :data
200
+ #
201
+ # @return [String]
202
+ def render data
203
+ @renderer.render(data)
204
+ end
205
+
206
+ # Sets component resolver
207
+ #
208
+ # @param [Proc] :component_resolver
209
+ #
210
+ # @return [nil]
211
+ def set_component_resolver component_resolver
212
+ @renderer.set_component_resolver(component_resolver)
213
+ end
214
+
192
215
  private
193
216
 
194
217
  def parse_result(res)
@@ -213,14 +236,27 @@ module Storyblok
213
236
  raise
214
237
  end
215
238
 
216
- {'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
217
247
  end
218
248
 
219
249
  # Patches a query hash with the client configurations for queries
220
250
  def request_query(query)
221
251
  query[:token] = configuration[:token] if query[:token].nil?
222
252
  query[:version] = configuration[:version] if query[:version].nil?
223
- 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
+
224
260
  query
225
261
  end
226
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.4'
3
+ VERSION = '2.1.0'
4
4
  end
@@ -10,12 +10,13 @@ Gem::Specification.new do |gem|
10
10
  gem.email = 'it@storyblok.com'
11
11
  gem.homepage = 'https://github.com/storyblok/storyblok-ruby'
12
12
 
13
- gem.files = Dir['{**/}{.*,*}'].select { |path| File.file?(path) && !path.start_with?('pkg') }
13
+ gem.files = Dir['{**/}{.*,*}'].select { |path| File.file?(path) && !path.start_with?('pkg') && !path.end_with?('.gem') }
14
14
  gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
15
15
  gem.test_files = gem.files.grep(%r{^spec/})
16
16
  gem.require_paths = ['lib']
17
17
 
18
18
  gem.add_dependency 'rest-client', '>= 1.8.0', '< 3'
19
+ gem.add_dependency 'storyblok-richtext-renderer', '>= 0.0.4', '< 1'
19
20
 
20
21
  gem.add_development_dependency 'bundler', '~> 1.5'
21
22
  gem.add_development_dependency 'rspec', '~> 3'
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.4
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: 2019-07-23 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
@@ -30,6 +30,26 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: storyblok-richtext-renderer
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 0.0.4
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '1'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.0.4
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '1'
33
53
  - !ruby/object:Gem::Dependency
34
54
  name: bundler
35
55
  requirement: !ruby/object:Gem::Requirement
@@ -75,14 +95,16 @@ files:
75
95
  - examples/cache.rb
76
96
  - examples/example_queries.rb
77
97
  - examples/management_api.rb
98
+ - examples/renderer.rb
78
99
  - examples/tree.rb
100
+ - lib/.DS_Store
79
101
  - lib/storyblok.rb
102
+ - lib/storyblok/.DS_Store
80
103
  - lib/storyblok/cache/redis.rb
81
104
  - lib/storyblok/client.rb
82
105
  - lib/storyblok/links.rb
83
106
  - lib/storyblok/request.rb
84
107
  - lib/storyblok/version.rb
85
- - storyblok-2.0.3.gem
86
108
  - storyblok.gemspec
87
109
  homepage: https://github.com/storyblok/storyblok-ruby
88
110
  licenses:
Binary file