Sha256: 79410f96aaa816493489d1562828d1ac5219711f371d7dcf7afbd781df67692d

Contents?: true

Size: 963 Bytes

Versions: 12

Compression:

Stored size: 963 Bytes

Contents

# frozen_string_literal: true

require_relative "../response/contributors_response"

module ONEAccess
  module Request
    class ContributorsRequestHandler
      def initialize(url:, limit: PAGE_SIZE)
        @url = url
        @limit = limit
      end

      def each
        offset = 0
        loop do
          http_response = Http::HttpClient.get(url, build_params(offset))
          response = Response::ContributorsResponse.from_json(http_response.body)
          response&.data&.each do |contributor|
            yield contributor
          end
          break unless more_records?(response.meta)
          offset += 1
        end
      end

      private

      PAGE_SIZE = 100

      attr_reader :url
      attr_reader :limit

      def build_params(offset)
        {
          limit:  limit,
          offset: offset
        }
      end

      def more_records?(meta)
        meta.offset < (meta.total / meta.limit).ceil - 1
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
oneaccess-1.2.0 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-1.1.0 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-1.0.1 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-1.0.0 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-0.5.0 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-0.4.5 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-0.4.4 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-0.4.3 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-0.4.2 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-0.4.1 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-0.4.0 lib/oneaccess/request/contributors_request_handler.rb
oneaccess-0.3.0 lib/oneaccess/request/contributors_request_handler.rb