Class: Storyblok::Client
- Inherits:
-
Object
- Object
- Storyblok::Client
- 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
-
#cache_version ⇒ Object
Returns the value of attribute cache_version.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #cached_get(request, bypass_cache = false) ⇒ Object
-
#datasource_entries(query = {}) ⇒ Hash
Gets a collection of datasource entries.
-
#datasources(query = {}) ⇒ Hash
Gets a collection of datasources.
- #delete(path, additional_headers = {}) ⇒ Object
- #flush ⇒ Object
- #get(path, additional_headers = {}) ⇒ Object
-
#get_from_cdn(slug, query = {}, id = nil) ⇒ Hash
Dynamic cdn endpoint call.
-
#initialize(given_configuration = {}) ⇒ Client
constructor
A new instance of Client.
-
#links(query = {}) ⇒ Hash
Gets a collection of links.
- #post(path, payload, additional_headers = {}) ⇒ Object
- #put(path, payload, additional_headers = {}) ⇒ Object
-
#render(data) ⇒ String
Returns html from richtext field data.
- #run_management_request(action, path, payload = {}, additional_headers = {}) ⇒ Object
-
#set_component_resolver(component_resolver) ⇒ nil
Sets component resolver.
-
#space(query = {}) ⇒ Hash
Gets the space info.
-
#stories(query = {}) ⇒ Hash
Gets a collection of stories.
-
#story(id, query = {}) ⇒ Hash
Gets a specific story.
-
#tags(query = {}) ⇒ Hash
Gets a collection of tags.
-
#tree(query = {}) ⇒ Hash
Gets a link tree.
Constructor Details
#initialize(given_configuration = {}) ⇒ Client
Returns a new instance of Client.
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_version ⇒ Object
Returns the value of attribute cache_version.
24 25 26 |
# File 'lib/storyblok/client.rb', line 24 def cache_version @cache_version end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
23 24 25 |
# File 'lib/storyblok/client.rb', line 23 def configuration @configuration end |
#logger ⇒ Object (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
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
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 |
#flush ⇒ Object
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
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 |
#links(query = {}) ⇒ Hash
Gets a collection of links
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
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
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
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
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
84 85 86 |
# File 'lib/storyblok/client.rb', line 84 def story(id, query = {}) Request.new(self, '/cdn/stories', query, id).get end |