Sha256: 1dfb7dcc15d6fe476daa9ac425cf3cabdb18e65538e18e8ecd25a63708cc09a8

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true
require 'agave/local/entities_repo'
require 'agave/local/items_repo'

module Agave
  module Local
    class Loader
      attr_reader :client
      attr_reader :entities_repo
      attr_reader :items_repo
      attr_reader :preview_mode

      def initialize(client, preview_mode = false)
        @client = client
        @preview_mode = preview_mode
        @entities_repo = EntitiesRepo.new
        @items_repo = ItemsRepo.new(@entities_repo)
      end

      def load
        threads = [
          Thread.new { Thread.current[:output] = site },
          Thread.new { Thread.current[:output] = all_items },
          Thread.new { Thread.current[:output] = all_uploads }
        ]

        results = threads.map do |t|
          t.join
          t[:output]
        end

        @entities_repo = EntitiesRepo.new(*results)
        @items_repo = ItemsRepo.new(@entities_repo)
      end

      private

      def site
        include = ['item_types', 'item_types.fields']
        client.request(:get, '/api/site', include: include)
      end

      def all_items
        client.items.all(
          { version: preview_mode ? 'latest' : 'published' },
          deserialize_response: false,
          all_pages: true
        )
      end

      def all_uploads
        client.uploads.all(
          { "filter[type]" => "used" },
          deserialize_response: false,
          all_pages: true
        )
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
agave-client-0.1.3 lib/agave/local/loader.rb
agave-client-0.1.2 lib/agave/local/loader.rb
agave-client-0.1.1 lib/agave/local/loader.rb
agave-client-0.1.0 lib/agave/local/loader.rb