Sha256: 74b598fa5ee4e163b71d82dd9367ffbab7717612bab0d73e162320a072aab2e5

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

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

module Dato
  module Local
    class Loader
      attr_reader :client
      attr_reader :entities_repo
      attr_reader :items_repo

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

      def load
        @entities_repo = EntitiesRepo.new(site, all_items)
        @items_repo = ItemsRepo.new(@entities_repo)
      end

      private

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

      def all_items
        items_per_page = 500
        base_response = client.request(:get, '/items', 'page[limit]' => 500)

        extra_pages = (base_response[:meta][:total_count] / items_per_page.to_f).ceil - 1

        extra_pages.times do |page|
          base_response[:data] += client.request(
            :get,
            '/items',
            'page[offset]' => items_per_page * (page + 1),
            'page[limit]' => items_per_page
          )[:data]
        end

        base_response
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dato-0.1.31 lib/dato/local/loader.rb
dato-0.1.30 lib/dato/local/loader.rb