Class: Storyblok::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/storyblok/client.rb

Constant Summary collapse

DEFAULT_CONFIGURATION =
{
  secure: true,
  api_url: 'api.storyblok.com',
  api_version: 1,
  logger: false,
  log_level: Logger::INFO,
  version: 'draft',
  component_resolver: ->(component, data) { '' },
  cache: nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_configuration = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • given_configuration (Hash) (defaults to: {})

Options Hash (given_configuration):

  • :token (String)

    Required if oauth_token is not set

  • :oauth_token (String)

    Required if token is not set

  • :api_url (String)
  • :component_resolver (Proc)
  • :api_version (Number)
  • :logger (false, ::Logger)
  • :log_level (::Logger::DEBUG, ::Logger::INFO, ::Logger::WARN, ::Logger::ERROR)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/storyblok/client.rb', line 34

def initialize(given_configuration = {})
  @configuration = default_configuration.merge(given_configuration)
  @cache_version = '0'
  validate_configuration!

  if configuration[:oauth_token]
    @rest_client = RestClient::Resource.new(base_url, :headers => {
      :authorization => configuration[:oauth_token]
    })
  end

  @renderer = Richtext::HtmlRenderer.new
  @renderer.set_component_resolver(@configuration[:component_resolver])
  setup_logger
end

Instance Attribute Details

#cache_versionObject

Returns the value of attribute cache_version.



24
25
26
# File 'lib/storyblok/client.rb', line 24

def cache_version
  @cache_version
end

#configurationObject (readonly)

Returns the value of attribute configuration.



23
24
25
# File 'lib/storyblok/client.rb', line 23

def configuration
  @configuration
end

#loggerObject (readonly)

Returns the value of attribute logger.



23
24
25
# File 'lib/storyblok/client.rb', line 23

def logger
  @logger
end

Instance Method Details

#cached_get(request, bypass_cache = false) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/storyblok/client.rb', line 173

def cached_get(request, bypass_cache = false)
  endpoint = base_url + request.url
  query = request_query(request.query)
  query_string = build_nested_query(query)

  if cache.nil? || bypass_cache || query[:version] == 'draft'
    result = run_request(endpoint, query_string)
  else
    cache_key = 'storyblok:' + configuration[:token] + ':v:' + query[:cv] + ':' + request.url + ':' + Base64.encode64(query_string)

    result = cache.cache(cache_key) do
      run_request(endpoint, query_string)
    end
  end

  JSON.parse(result)
end

#datasource_entries(query = {}) ⇒ Hash

Gets a collection of datasource entries

Parameters:

  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


93
94
95
# File 'lib/storyblok/client.rb', line 93

def datasource_entries(query = {})
  Request.new(self, '/cdn/datasource_entries', query).get
end

#datasources(query = {}) ⇒ Hash

Gets a collection of datasources

Parameters:

  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


102
103
104
# File 'lib/storyblok/client.rb', line 102

def datasources(query = {})
  Request.new(self, '/cdn/datasources', query).get
end

#delete(path, additional_headers = {}) ⇒ Object



141
142
143
# File 'lib/storyblok/client.rb', line 141

def delete(path, additional_headers = {})
  run_management_request(:delete, path, nil, additional_headers)
end

#flushObject



191
192
193
194
195
# File 'lib/storyblok/client.rb', line 191

def flush
  unless cache.nil?
    cache.set('storyblok:' + configuration[:token] + ':version', space['data']['space']['version'])
  end
end

#get(path, additional_headers = {}) ⇒ Object



145
146
147
# File 'lib/storyblok/client.rb', line 145

def get(path, additional_headers = {})
  run_management_request(:get, path, nil, additional_headers)
end

#get_from_cdn(slug, query = {}, id = nil) ⇒ Hash

Dynamic cdn endpoint call

Parameters:

  • id (String) (defaults to: nil)
  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


56
57
58
# File 'lib/storyblok/client.rb', line 56

def get_from_cdn(slug, query = {}, id = nil)
  Request.new(self, "/cdn/#{slug}", query, id).get
end

Gets a collection of links

Parameters:

  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


120
121
122
# File 'lib/storyblok/client.rb', line 120

def links(query = {})
  Request.new(self, '/cdn/links', query).get
end

#post(path, payload, additional_headers = {}) ⇒ Object



133
134
135
# File 'lib/storyblok/client.rb', line 133

def post(path, payload, additional_headers = {})
  run_management_request(:post, path, payload, additional_headers)
end

#put(path, payload, additional_headers = {}) ⇒ Object



137
138
139
# File 'lib/storyblok/client.rb', line 137

def put(path, payload, additional_headers = {})
  run_management_request(:put, path, payload, additional_headers)
end

#render(data) ⇒ String

Returns html from richtext field data

Parameters:

  • :data (Hash)

Returns:

  • (String)


202
203
204
# File 'lib/storyblok/client.rb', line 202

def render data
  @renderer.render(data)
end

#run_management_request(action, path, payload = {}, additional_headers = {}) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/storyblok/client.rb', line 149

def run_management_request(action, path, payload = {}, additional_headers = {})
  logger.info(request: { path: path, action: action }) if logger
  retries_left = 3

  begin
    if [:post, :put].include?(action)
      res = @rest_client[path].send(action, payload, additional_headers)
    else
      res = @rest_client[path].send(action, additional_headers)
    end
  rescue RestClient::TooManyRequests
    if retries_left != 0
      retries_left -= 1
      logger.info("Too many requests. Retry nr. #{(3 - retries_left).to_s} of max. 3 times.") if logger
      sleep(0.5)
      retry
    end

    raise
  end

  parse_result(res)
end

#set_component_resolver(component_resolver) ⇒ nil

Sets component resolver

Parameters:

  • :component_resolver (Proc)

Returns:

  • (nil)


211
212
213
# File 'lib/storyblok/client.rb', line 211

def set_component_resolver component_resolver
  @renderer.set_component_resolver(component_resolver)
end

#space(query = {}) ⇒ Hash

Gets the space info

Parameters:

  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


65
66
67
# File 'lib/storyblok/client.rb', line 65

def space(query = {})
  Request.new(self, '/cdn/spaces/me', query, nil, true).get
end

#stories(query = {}) ⇒ Hash

Gets a collection of stories

Parameters:

  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


74
75
76
# File 'lib/storyblok/client.rb', line 74

def stories(query = {})
  Request.new(self, '/cdn/stories', query).get
end

#story(id, query = {}) ⇒ Hash

Gets a specific story

Parameters:

  • id (String)
  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


84
85
86
# File 'lib/storyblok/client.rb', line 84

def story(id, query = {})
  Request.new(self, '/cdn/stories', query, id).get
end

#tags(query = {}) ⇒ Hash

Gets a collection of tags

Parameters:

  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


111
112
113
# File 'lib/storyblok/client.rb', line 111

def tags(query = {})
  Request.new(self, '/cdn/tags', query).get
end

#tree(query = {}) ⇒ Hash

Gets a link tree

Parameters:

  • query (Hash) (defaults to: {})

Returns:

  • (Hash)


129
130
131
# File 'lib/storyblok/client.rb', line 129

def tree(query = {})
  Links.new(Request.new(self, '/cdn/links', query).get).as_tree
end