storyblok 2.0.5 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +1 -0
- data/README.md +43 -1
- data/examples/example_queries.rb +2 -2
- data/examples/renderer.rb +31 -0
- data/lib/storyblok/client.rb +46 -10
- data/lib/storyblok/request.rb +4 -3
- data/lib/storyblok/version.rb +1 -1
- data/storyblok.gemspec +1 -0
- metadata +25 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 33a4596358cf0ba305669f743c068507e4101df1ef108489220aea87ab080a10
         | 
| 4 | 
            +
              data.tar.gz: 39d281b937424cc102f5aed916e5b49ec139fa57991d0bdf033d2dcd3a7e6cce
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 15b2d39171573e27f50353dfb643930b7c8cf2b8c57ed00576c5b58ffad967376b199635aacabfb37363580bcce5df9dc7e878f56a79182cfe4017ee7f606545
         | 
| 7 | 
            +
              data.tar.gz: 99271032e787a4d40270692800c95e8b7b2d2419ced123ea022d4e67436d0937d11477beac638449421cf1dbdde624cf771dd785a535b8c6c22d4709eab8337e
         | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/Gemfile
    CHANGED
    
    
    
        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:  | 
| 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 |  | 
    
        data/examples/example_queries.rb
    CHANGED
    
    | @@ -5,14 +5,14 @@ require 'storyblok' | |
| 5 5 | 
             
            logger = Logger.new(STDOUT)
         | 
| 6 6 |  | 
| 7 7 | 
             
            client = Storyblok::Client.new(
         | 
| 8 | 
            -
              token: ' | 
| 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'])
         | 
    
        data/lib/storyblok/client.rb
    CHANGED
    
    | @@ -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 | 
            -
                   | 
| 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 | 
            -
                     | 
| 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',  | 
| 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 | 
            -
                   | 
| 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 | 
            -
             | 
| 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 |  | 
    
        data/lib/storyblok/request.rb
    CHANGED
    
    | @@ -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 =  | 
| 16 | 
            +
                    @id = 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
         | 
    
        data/lib/storyblok/version.rb
    CHANGED
    
    
    
        data/storyblok.gemspec
    CHANGED
    
    | @@ -16,6 +16,7 @@ Gem::Specification.new do |gem| | |
| 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. | 
| 4 | 
            +
              version: 2.1.1
         | 
| 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:  | 
| 11 | 
            +
            date: 2020-08-25 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,8 +95,11 @@ 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
         |