Sha256: 66952884200308e1bf0fe31cbb79b5e790c3a68bb886d07261b022482af1ac21

Contents?: true

Size: 788 Bytes

Versions: 18

Compression:

Stored size: 788 Bytes

Contents

module Storyblok
  class Links
    def initialize(response_obj)
      @links = response_obj['data']['links']
    end

    def as_tree
      tree = {}

      @links.each do |key, item|
        if tree[item['parent_id']].nil?
          tree[item['parent_id']] = []
        end

        tree[item['parent_id']] << item
      end

      generate_tree(0, tree)
    end

    private

    def generate_tree(parent_id = 0, items)
      tree = {}

      if !items[parent_id].nil?
        result = items[parent_id]

        result.each do |item|
          if tree[item['id']].nil?
            tree[item['id']] = {}
          end

          tree[item['id']]['item'] = item
          tree[item['id']]['children'] = generate_tree(item['id'], items)
        end
      end

      tree
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
storyblok-3.0.1 lib/storyblok/links.rb
storyblok-3.0.0 lib/storyblok/links.rb
storyblok-2.1.1 lib/storyblok/links.rb
storyblok-2.1.0 lib/storyblok/links.rb
storyblok-2.0.8 lib/storyblok/links.rb
storyblok-2.0.7 lib/storyblok/links.rb
storyblok-2.0.6 lib/storyblok/links.rb
storyblok-2.0.5 lib/storyblok/links.rb
storyblok-2.0.4 lib/storyblok/links.rb
storyblok-2.0.3 lib/storyblok/links.rb
storyblok-2.0.2 lib/storyblok/links.rb
storyblok-2.0.1 lib/storyblok/links.rb
storyblok-2.0.0 lib/storyblok/links.rb
storyblok-1.0.4 lib/storyblok/links.rb
storyblok-1.0.3 lib/storyblok/links.rb
storyblok-1.0.2 lib/storyblok/links.rb
storyblok-1.0.1 lib/storyblok/links.rb
storyblok-1.0.0 lib/storyblok/links.rb