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', cache_version: Time.now.to_i }
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#datasource_entries(query = {}) ⇒ Hash
Gets a collection of datasource entries.
- #flush ⇒ Object
- #get(request) ⇒ Object
-
#initialize(given_configuration = {}) ⇒ Client
constructor
A new instance of Client.
-
#links(query = {}) ⇒ Hash
Gets a collection of links.
-
#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.
29 30 31 32 33 |
# File 'lib/storyblok/client.rb', line 29 def initialize(given_configuration = {}) @configuration = default_configuration.merge(given_configuration) validate_configuration! setup_logger end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
21 22 23 |
# File 'lib/storyblok/client.rb', line 21 def configuration @configuration end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
21 22 23 |
# File 'lib/storyblok/client.rb', line 21 def logger @logger end |
Instance Method Details
#datasource_entries(query = {}) ⇒ Hash
Gets a collection of datasource entries
59 60 61 |
# File 'lib/storyblok/client.rb', line 59 def datasource_entries(query = {}) Request.new(self, '/cdn/datasource_entries', query).get end |
#flush ⇒ Object
110 111 112 113 114 |
# File 'lib/storyblok/client.rb', line 110 def flush if !Cache.client.nil? Cache.client.set('storyblok:' + configuration[:token] + ':version', Time.now.to_i.to_s) end end |
#get(request) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/storyblok/client.rb', line 90 def get(request) endpoint = base_url + request.url query = request_query(request.query) query_string = build_nested_query(query) if Cache.client.nil? result = run_request(endpoint, query_string) else version = Cache.client.get('storyblok:' + configuration[:token] + ':version') || '0' cache_key = 'storyblok:' + configuration[:token] + ':v:' + version + ':' + request.url + ':' + Base64.encode64(query_string) cache_time = 60 * 60 * 2 result = Cache.cache(cache_key, cache_time) do run_request(endpoint, query_string) end end JSON.parse(result) end |
#links(query = {}) ⇒ Hash
Gets a collection of links
77 78 79 |
# File 'lib/storyblok/client.rb', line 77 def links(query = {}) Request.new(self, '/cdn/links', query).get end |
#stories(query = {}) ⇒ Hash
Gets a collection of stories
40 41 42 |
# File 'lib/storyblok/client.rb', line 40 def stories(query = {}) Request.new(self, '/cdn/stories', query).get end |
#story(id, query = {}) ⇒ Hash
Gets a specific story
50 51 52 |
# File 'lib/storyblok/client.rb', line 50 def story(id, query = {}) Request.new(self, '/cdn/stories', query, id).get end |