Sha256: 26089333060213f43b347d2cde32d6cfdb5f32443a7d054504455d46a9e47720

Contents?: true

Size: 1.23 KB

Versions: 7

Compression:

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

7 entries across 7 versions & 1 rubygems

Version Path
dato-0.3.3 lib/dato/local/loader.rb
dato-0.3.2 lib/dato/local/loader.rb
dato-0.3.1 lib/dato/local/loader.rb
dato-0.3.0 lib/dato/local/loader.rb
dato-0.2.7 lib/dato/local/loader.rb
dato-0.2.6 lib/dato/local/loader.rb
dato-0.2.4 lib/dato/local/loader.rb