storyblok 3.0.1 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,14 +14,14 @@ spaces = client.get('spaces')['data']['spaces']
14
14
  space = spaces.first
15
15
 
16
16
  p client.get("spaces/#{space['id']}")['data']['space']
17
- story_res = client.post("spaces/#{space['id']}/stories", {story: {name: 'new', slug: "new"}})['data']
17
+ story_res = client.post("spaces/#{space['id']}/stories", { story: { name: 'new', slug: "new" } })['data']
18
18
 
19
19
  10.times do |index|
20
20
  client.get("spaces/#{space['id']}/stories/#{story_res['story']['id']}")
21
21
  puts index
22
22
  end
23
23
 
24
- p client.put("spaces/#{space['id']}/stories/#{story_res['story']['id']}", {story: {name: 'new123'}})['data']
24
+ p client.put("spaces/#{space['id']}/stories/#{story_res['story']['id']}", { story: { name: 'new123' } })['data']
25
25
 
26
26
  10.times do |index|
27
27
  client.story('new')
@@ -29,5 +29,3 @@ p client.put("spaces/#{space['id']}/stories/#{story_res['story']['id']}", {story
29
29
  end
30
30
 
31
31
  p client.delete("spaces/#{space['id']}/stories/#{story_res['story']['id']}")
32
-
33
-
data/examples/renderer.rb CHANGED
@@ -1,31 +1,30 @@
1
1
  # bundle exec ruby examples/renderer.rb
2
2
 
3
- require_relative '../lib/storyblok'
4
- require 'redis'
3
+ require_relative "../lib/storyblok"
4
+ require "redis"
5
5
 
6
6
  logger = Logger.new(STDOUT)
7
7
 
8
- redis = Redis.new(url: 'redis://localhost:6379')
8
+ redis = Redis.new(url: "redis://localhost:6379")
9
9
  cache = Storyblok::Cache::Redis.new(redis: redis)
10
10
 
11
11
  client = Storyblok::Client.new(
12
- token: '6HMYdAjBoONyuS6GIf5PdAtt',
12
+ token: "6HMYdAjBoONyuS6GIf5PdAtt",
13
13
  logger: logger,
14
14
  component_resolver: ->(component, data) {
15
15
  "Placeholder for #{component}: #{data['text']}"
16
16
  },
17
- api_url: 'api-testing.storyblok.com',
17
+ api_url: "api-testing.storyblok.com",
18
18
  api_version: 2,
19
19
  cache: cache
20
20
  )
21
21
 
22
-
23
22
  res = client.flush
24
- res = client.story('authors/page', {version: 'published'})
23
+ res = client.story("authors/page", { version: "published" })
25
24
  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'})
25
+ res = client.story("authors/page", { version: "published" })
26
+ res = client.story("authors/page", { version: "published" })
27
+ res = client.story("authors/page", { version: "published" })
29
28
 
30
- puts res['data']
31
- #puts client.render(res['data']['story']['content']['intro'])
29
+ puts res["data"]
30
+ # puts client.render(res['data']['story']['content']['intro'])
data/examples/tree.rb CHANGED
@@ -1,30 +1,30 @@
1
1
  # bundle exec ruby examples/tree.rb
2
2
 
3
- require 'storyblok'
3
+ require "storyblok"
4
4
 
5
5
  logger = Logger.new(STDOUT)
6
6
 
7
7
  client = Storyblok::Client.new(
8
- token: 't618GfLe1YHICBioAHnMrwtt',
9
- api_url: 'localhost:3001',
8
+ token: "t618GfLe1YHICBioAHnMrwtt",
9
+ api_url: "localhost:3001",
10
10
  secure: false,
11
11
  logger: logger
12
12
  )
13
13
 
14
14
  tree = client.tree
15
15
 
16
- puts '<ul>'
16
+ puts "<ul>"
17
17
  tree.each do |key, item|
18
- puts '<li>' + item['item']['name']
18
+ puts "<li>" + item["item"]["name"]
19
19
 
20
- if !item['children'].empty?
21
- puts '<ul>'
22
- item['children'].each do |key, inner_item|
23
- puts '<li>' + inner_item['item']['name'] + '</li>'
20
+ if !item["children"].empty?
21
+ puts "<ul>"
22
+ item["children"].each do |key, inner_item|
23
+ puts "<li>" + inner_item["item"]["name"] + "</li>"
24
24
  end
25
- puts '</ul>'
25
+ puts "</ul>"
26
26
  end
27
27
 
28
- puts '</li>'
28
+ puts "</li>"
29
29
  end
30
- puts '</ul>'
30
+ puts "</ul>"
@@ -47,7 +47,6 @@ module Storyblok
47
47
  @redis.set(key, value)
48
48
  end
49
49
  end
50
-
51
50
  end
52
51
  end
53
52
  end
@@ -11,7 +11,6 @@ module Storyblok
11
11
  class Client
12
12
  DEFAULT_CONFIGURATION = {
13
13
  secure: true,
14
- api_url: 'api.storyblok.com',
15
14
  api_version: 2,
16
15
  logger: false,
17
16
  log_level: Logger::INFO,
@@ -32,6 +31,7 @@ module Storyblok
32
31
  # @option given_configuration [String] :api_url
33
32
  # @option given_configuration [Proc] :component_resolver
34
33
  # @option given_configuration [Number] :api_version
34
+ # @option given_configuration [String] :api_region
35
35
  # @option given_configuration [false, ::Logger] :logger
36
36
  # @option given_configuration [::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR] :log_level
37
37
  def initialize(given_configuration = {})
@@ -40,9 +40,15 @@ module Storyblok
40
40
  validate_configuration!
41
41
 
42
42
  if configuration[:oauth_token]
43
- @rest_client = RestClient::Resource.new(base_url, :headers => {
44
- :authorization => configuration[:oauth_token]
45
- })
43
+ @configuration[:api_version] = 1
44
+ @rest_client = RestClient::Resource.new(
45
+ base_url,
46
+ headers: {
47
+ authorization: configuration[:oauth_token],
48
+ 'SB-Agent-Version': Storyblok::VERSION,
49
+ 'SB-Agent': 'SB-RB'
50
+ }
51
+ )
46
52
  end
47
53
 
48
54
  @renderer = Richtext::HtmlRenderer.new
@@ -197,7 +203,6 @@ module Storyblok
197
203
  result
198
204
  end
199
205
 
200
-
201
206
  def flush
202
207
  unless cache.nil?
203
208
  cache.set('storyblok:' + configuration[:token] + ':version', space['data']['space']['version'])
@@ -223,175 +228,188 @@ module Storyblok
223
228
  end
224
229
 
225
230
  private
231
+ def parse_result(res)
232
+ { 'headers' => res.headers, 'data' => JSON.parse(res.body) }
233
+ end
226
234
 
227
- def parse_result(res)
228
- {'headers' => res.headers, 'data' => JSON.parse(res.body)}
229
- end
230
-
231
- def run_request(endpoint, query_string)
232
- logger.info(request: { endpoint: endpoint, query: query_string }) if logger
233
-
234
- retries_left = 3
235
+ def run_request(endpoint, query_string)
236
+ logger.info(request: { endpoint: endpoint, query: query_string }) if logger
237
+
238
+ retries_left = 3
239
+
240
+ begin
241
+ # rubocop:disable Lint/UselessAssignment
242
+ res = RestClient.get(
243
+ "#{endpoint}?#{query_string}",
244
+ headers={
245
+ 'SB-Agent-Version': Storyblok::VERSION,
246
+ 'SB-Agent': 'SB-RB'
247
+ }
248
+ )
249
+ # rubocop:enable Lint/UselessAssignment
250
+ rescue RestClient::TooManyRequests
251
+ if retries_left != 0
252
+ retries_left -= 1
253
+ logger.info("Too many requests. Retry nr. #{(3 - retries_left).to_s} of max. 3 times.") if logger
254
+ sleep(0.5)
255
+ retry
256
+ end
235
257
 
236
- begin
237
- res = RestClient.get "#{endpoint}?#{query_string}"
238
- rescue RestClient::TooManyRequests
239
- if retries_left != 0
240
- retries_left -= 1
241
- logger.info("Too many requests. Retry nr. #{(3 - retries_left).to_s} of max. 3 times.") if logger
242
- sleep(0.5)
243
- retry
258
+ raise
244
259
  end
245
260
 
246
- raise
247
- end
261
+ body = JSON.parse(res.body)
262
+ self.cache_version = body['cv'] if body['cv']
248
263
 
249
- body = JSON.parse(res.body)
250
- self.cache_version = body['cv'] if body['cv']
264
+ unless cache.nil?
265
+ cache.set('storyblok:' + configuration[:token] + ':version', cache_version)
266
+ end
251
267
 
252
- unless cache.nil?
253
- cache.set('storyblok:' + configuration[:token] + ':version', cache_version)
268
+ { 'headers' => res.headers, 'data' => body }.to_json
254
269
  end
255
270
 
256
- {'headers' => res.headers, 'data' => body}.to_json
257
- end
271
+ # Patches a query hash with the client configurations for queries
272
+ def request_query(query)
273
+ query[:token] = configuration[:token] if query[:token].nil?
274
+ query[:version] = configuration[:version] if query[:version].nil?
258
275
 
259
- # Patches a query hash with the client configurations for queries
260
- def request_query(query)
261
- query[:token] = configuration[:token] if query[:token].nil?
262
- query[:version] = configuration[:version] if query[:version].nil?
276
+ unless cache.nil?
277
+ query[:cv] = (cache.get('storyblok:' + configuration[:token] + ':version') or cache_version) if query[:cv].nil?
278
+ else
279
+ query[:cv] = cache_version if query[:cv].nil?
280
+ end
263
281
 
264
- unless cache.nil?
265
- query[:cv] = (cache.get('storyblok:' + configuration[:token] + ':version') or cache_version) if query[:cv].nil?
266
- else
267
- query[:cv] = cache_version if query[:cv].nil?
282
+ query
268
283
  end
269
284
 
270
- query
271
- end
285
+ # Returns the base url for all of the client's requests
286
+ def base_url
287
+ if !configuration[:api_url]
288
+ region = configuration[:api_region] ? "-#{configuration[:api_region]}" : ""
289
+ "http#{configuration[:secure] ? 's' : ''}://api#{region}.storyblok.com/v#{configuration[:api_version]}"
290
+ else
291
+ "http#{configuration[:secure] ? 's' : ''}://#{configuration[:api_url]}/v#{configuration[:api_version]}"
292
+ end
293
+ end
272
294
 
273
- # Returns the base url for all of the client's requests
274
- def base_url
275
- "http#{configuration[:secure] ? 's' : ''}://#{configuration[:api_url]}/v#{configuration[:api_version]}"
276
- end
295
+ def default_configuration
296
+ DEFAULT_CONFIGURATION.dup
297
+ end
277
298
 
278
- def default_configuration
279
- DEFAULT_CONFIGURATION.dup
280
- end
299
+ def cache
300
+ configuration[:cache]
301
+ end
281
302
 
282
- def cache
283
- configuration[:cache]
284
- end
303
+ def setup_logger
304
+ @logger = configuration[:logger]
305
+ logger.level = configuration[:log_level] if logger
306
+ end
285
307
 
286
- def setup_logger
287
- @logger = configuration[:logger]
288
- logger.level = configuration[:log_level] if logger
289
- end
308
+ def validate_configuration!
309
+ fail ArgumentError, 'You will need to initialize a client with an :token or :oauth_token' if !configuration[:token] and !configuration[:oauth_token]
310
+ fail ArgumentError, 'The :api_version must be a positive number' unless configuration[:api_version].to_i >= 0
311
+ end
290
312
 
291
- def validate_configuration!
292
- fail ArgumentError, 'You will need to initialize a client with an :token or :oauth_token' if !configuration[:token] and !configuration[:oauth_token]
293
- fail ArgumentError, 'The client configuration needs to contain an :api_url' if configuration[:api_url].empty?
294
- fail ArgumentError, 'The :api_version must be a positive number' unless configuration[:api_version].to_i >= 0
295
- end
313
+ def build_nested_query(value, prefix = nil)
314
+ case value
315
+ when Array
316
+ value.map { |v|
317
+ build_nested_query(v, "#{prefix}[]")
318
+ }.join("&")
319
+ when Hash
320
+ value.map { |k, v|
321
+ build_nested_query(v,
322
+ prefix ? "#{prefix}[#{URI.encode_www_form_component(k)}]" : URI.encode_www_form_component(k))
323
+ }.reject(&:empty?).join('&')
324
+ when nil
325
+ prefix
326
+ else
327
+ raise ArgumentError, "value must be a Hash" if prefix.nil?
296
328
 
297
- def build_nested_query(value, prefix = nil)
298
- case value
299
- when Array
300
- value.map { |v|
301
- build_nested_query(v, "#{prefix}[]")
302
- }.join("&")
303
- when Hash
304
- value.map { |k, v|
305
- build_nested_query(v, prefix ? "#{prefix}[#{URI.encode_www_form_component(k)}]" : URI.encode_www_form_component(k))
306
- }.reject(&:empty?).join('&')
307
- when nil
308
- prefix
309
- else
310
- raise ArgumentError, "value must be a Hash" if prefix.nil?
311
- "#{prefix}=#{URI.encode_www_form_component(value)}"
329
+ "#{prefix}=#{URI.encode_www_form_component(value)}"
330
+ end
312
331
  end
313
- end
314
332
 
315
- def resolve_stories(result, params)
316
- data = result['data']
317
- rels = data['rels']
318
- links = data['links']
319
- resolve_relations = params[:resolve_relations] || params["resolve_relations"]
333
+ def resolve_stories(result, params)
334
+ data = result['data']
335
+ rels = data['rels']
336
+ links = data['links']
337
+ resolve_relations = params[:resolve_relations] || params["resolve_relations"]
320
338
 
321
- if data['stories'].nil?
322
- find_and_fill_relations(data.dig('story', 'content'), resolve_relations, rels)
323
- find_and_fill_links(data.dig('story', 'content'), links)
324
- else
325
- data['stories'].each do |story|
326
- find_and_fill_relations(story['content'], resolve_relations, rels)
327
- find_and_fill_links(story['content'], links)
339
+ if data['stories'].nil?
340
+ find_and_fill_relations(data.dig('story', 'content'), resolve_relations, rels)
341
+ find_and_fill_links(data.dig('story', 'content'), links)
342
+ else
343
+ data['stories'].each do |story|
344
+ find_and_fill_relations(story['content'], resolve_relations, rels)
345
+ find_and_fill_links(story['content'], links)
346
+ end
328
347
  end
329
- end
330
348
 
331
- result
332
- end
349
+ result
350
+ end
333
351
 
334
- def find_and_fill_links(content, links)
335
- return if content.nil? || links.nil? || links.size.zero?
352
+ def find_and_fill_links(content, links)
353
+ return if content.nil? || links.nil? || links.size.zero?
336
354
 
337
- if content.is_a? Array
338
- content.each do |item|
339
- find_and_fill_links(item, links)
340
- end
341
- elsif content.is_a? Hash
342
- content['story'] = nil
343
- content.each do |_k, value|
344
- if !content['fieldtype'].nil?
345
- if content['fieldtype'] == 'multilink' && content['linktype'] == 'story'
346
- id =
347
- if content['id'].is_a? String
348
- content['id']
349
- elsif content['uuid'].is_a? String
350
- content['uuid']
351
- end
355
+ if content.is_a? Array
356
+ content.each do |item|
357
+ find_and_fill_links(item, links)
358
+ end
359
+ elsif content.is_a? Hash
360
+ content['story'] = nil
361
+ content.each do |_k, value|
362
+ if !content['fieldtype'].nil?
363
+ if content['fieldtype'] == 'multilink' && content['linktype'] == 'story'
364
+ id =
365
+ if content['id'].is_a? String
366
+ content['id']
367
+ elsif content['uuid'].is_a? String
368
+ content['uuid']
369
+ end
352
370
 
353
- links.each do |link|
354
- if link['uuid'] == id
355
- content['story'] = link
356
- break
371
+ links.each do |link|
372
+ if link['uuid'] == id
373
+ content['story'] = link
374
+ break
375
+ end
357
376
  end
358
377
  end
359
378
  end
360
- end
361
379
 
362
- find_and_fill_links(value, links)
380
+ find_and_fill_links(value, links)
381
+ end
382
+ content.delete('story') if content['story'].nil?
363
383
  end
364
- content.delete('story') if content['story'].nil?
365
384
  end
366
- end
367
385
 
368
- def find_and_fill_relations(content, relation_params, rels)
369
- return if content.nil? || rels.nil? || rels.size.zero?
386
+ def find_and_fill_relations(content, relation_params, rels)
387
+ return if content.nil? || rels.nil? || rels.size.zero?
370
388
 
371
- if content.is_a? Array
372
- content.each do |item|
373
- find_and_fill_relations(item, relation_params, rels)
374
- end
375
- elsif content.is_a? Hash
376
- content.each do |_k, value|
377
- if !content['component'].nil? && !content['_uid'].nil?
378
- relation_params.split(',').each do |relation|
379
- component, field_name = relation.split('.')
380
-
381
- if (content['component'] == component) && !content[field_name].nil?
382
- rels.each do |rel|
383
- index = content[field_name].index(rel['uuid'])
384
- if !index.nil?
385
- content[field_name][index] = rel
389
+ if content.is_a? Array
390
+ content.each do |item|
391
+ find_and_fill_relations(item, relation_params, rels)
392
+ end
393
+ elsif content.is_a? Hash
394
+ content.each do |_k, value|
395
+ if !content['component'].nil? && !content['_uid'].nil?
396
+ relation_params.split(',').each do |relation|
397
+ component, field_name = relation.split('.')
398
+
399
+ if (content['component'] == component) && !content[field_name].nil?
400
+ rels.each do |rel|
401
+ index = content[field_name].index(rel['uuid'])
402
+ if !index.nil?
403
+ content[field_name][index] = rel
404
+ end
386
405
  end
387
406
  end
388
407
  end
389
408
  end
390
- end
391
409
 
392
- find_and_fill_relations(value, relation_params, rels)
410
+ find_and_fill_relations(value, relation_params, rels)
411
+ end
393
412
  end
394
413
  end
395
- end
396
414
  end
397
415
  end
@@ -1,42 +1,41 @@
1
1
  module Storyblok
2
2
  class Links
3
3
  def initialize(response_obj)
4
- @links = response_obj['data']['links']
4
+ @links = response_obj["data"]["links"]
5
5
  end
6
6
 
7
7
  def as_tree
8
8
  tree = {}
9
9
 
10
10
  @links.each do |key, item|
11
- if tree[item['parent_id']].nil?
12
- tree[item['parent_id']] = []
11
+ if tree[item["parent_id"]].nil?
12
+ tree[item["parent_id"]] = []
13
13
  end
14
14
 
15
- tree[item['parent_id']] << item
15
+ tree[item["parent_id"]] << item
16
16
  end
17
17
 
18
18
  generate_tree(0, tree)
19
19
  end
20
20
 
21
21
  private
22
+ def generate_tree(parent_id = 0, items)
23
+ tree = {}
22
24
 
23
- def generate_tree(parent_id = 0, items)
24
- tree = {}
25
+ if !items[parent_id].nil?
26
+ result = items[parent_id]
25
27
 
26
- if !items[parent_id].nil?
27
- result = items[parent_id]
28
+ result.each do |item|
29
+ if tree[item["id"]].nil?
30
+ tree[item["id"]] = {}
31
+ end
28
32
 
29
- result.each do |item|
30
- if tree[item['id']].nil?
31
- tree[item['id']] = {}
33
+ tree[item["id"]]["item"] = item
34
+ tree[item["id"]]["children"] = generate_tree(item["id"], items)
32
35
  end
33
-
34
- tree[item['id']]['item'] = item
35
- tree[item['id']]['children'] = generate_tree(item['id'], items)
36
36
  end
37
- end
38
37
 
39
- tree
40
- end
38
+ tree
39
+ end
41
40
  end
42
41
  end
@@ -1,4 +1,4 @@
1
1
  module Storyblok
2
2
  # Gem Version
3
- VERSION = '3.0.1'
3
+ VERSION = "3.2.0"
4
4
  end
data/lib/storyblok.rb CHANGED
@@ -1,3 +1,3 @@
1
- require 'storyblok/version'
2
- require 'storyblok/cache/redis'
3
- require 'storyblok/client'
1
+ require "storyblok/version"
2
+ require "storyblok/cache/redis"
3
+ require "storyblok/client"
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: 3.0.1
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Storyblok (Alexander Feiglstorfer)
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-07 00:00:00.000000000 Z
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -141,31 +141,28 @@ executables: []
141
141
  extensions: []
142
142
  extra_rdoc_files: []
143
143
  files:
144
- - ".DS_Store"
145
144
  - ".editorconfig"
146
145
  - ".gitignore"
147
- - ".ruby-version"
146
+ - ".rubocop.yml"
147
+ - ".rubocop_strict.yml"
148
+ - ".rubocop_todo.yml"
148
149
  - Gemfile
149
- - Gemfile.lock
150
150
  - README.md
151
151
  - examples/cache.rb
152
152
  - examples/example_queries.rb
153
153
  - examples/management_api.rb
154
154
  - examples/renderer.rb
155
155
  - examples/tree.rb
156
- - lib/.DS_Store
157
156
  - lib/storyblok.rb
158
- - lib/storyblok/.DS_Store
159
157
  - lib/storyblok/cache/redis.rb
160
158
  - lib/storyblok/client.rb
161
159
  - lib/storyblok/links.rb
162
160
  - lib/storyblok/request.rb
163
161
  - lib/storyblok/version.rb
164
162
  homepage: https://github.com/storyblok/storyblok-ruby
165
- licenses:
166
- - MIT
163
+ licenses: []
167
164
  metadata: {}
168
- post_install_message:
165
+ post_install_message:
169
166
  rdoc_options: []
170
167
  require_paths:
171
168
  - lib
@@ -180,9 +177,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
177
  - !ruby/object:Gem::Version
181
178
  version: '0'
182
179
  requirements: []
183
- rubyforge_project:
184
- rubygems_version: 2.7.6
185
- signing_key:
180
+ rubygems_version: 3.1.4
181
+ signing_key:
186
182
  specification_version: 4
187
183
  summary: storyblok
188
184
  test_files: []
data/.DS_Store DELETED
Binary file
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.5.1