Sha256: 250a67be348bb2f3f1fdde88c87df9f47f1af6db0c97f8baf43e4c5b7d442967

Contents?: true

Size: 1.1 KB

Versions: 14

Compression:

Stored size: 1.1 KB

Contents

require 'active_support'

class LHS::Record

  module All
    extend ActiveSupport::Concern

    module ClassMethods
      # Should be an edge case but sometimes all objects from a certain resource
      # are required. In this case we load the first page with the default max limit,
      # compute the amount of left over requests, do all the the left over requests
      # for the following pages and concatenate all the results in order to return
      # all the objects for a given resource.
      def all(params = {})
        all = []
        default_max_limit = 100
        data = request(params: params.merge(limit: default_max_limit))
        all.concat(data._raw[:items])
        total_left = data._raw[:total] - data.count
        limit = data._raw[:limit] || data.count
        if limit > 0
          requests = total_left / limit
          requests.times do |i|
            offset = limit * (i + 1) + 1
            all.concat request(params: params.merge(limit: limit, offset: offset))._raw[:items]
          end
        end
        data._record_class.new(LHS::Data.new(all, nil, self))
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lhs-3.3.5 lib/lhs/concerns/record/all.rb
lhs-3.3.3 lib/lhs/concerns/record/all.rb
lhs-3.3.2 lib/lhs/concerns/record/all.rb
lhs-3.3.1 lib/lhs/concerns/record/all.rb
lhs-3.3.0 lib/lhs/concerns/record/all.rb
lhs-3.2.0 lib/lhs/concerns/record/all.rb
lhs-3.1.3 lib/lhs/concerns/record/all.rb
lhs-3.1.1 lib/lhs/concerns/record/all.rb
lhs-3.1.0 lib/lhs/concerns/record/all.rb
lhs-3.0.5 lib/lhs/concerns/record/all.rb
lhs-3.0.4 lib/lhs/concerns/record/all.rb
lhs-3.0.3 lib/lhs/concerns/record/all.rb
lhs-3.0.2 lib/lhs/concerns/record/all.rb
lhs-3.0.1 lib/lhs/concerns/record/all.rb