Sha256: 3b0d6e046ec3519ac14b0a2f9f34846edf95fb39aa3bb8b76be7c8ec961de742

Contents?: true

Size: 815 Bytes

Versions: 2

Compression:

Stored size: 815 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

2 entries across 2 versions & 1 rubygems

Version Path
storyblok-3.2.0 lib/storyblok/links.rb
storyblok-3.1.0 lib/storyblok/links.rb