Sha256: e401a57a4264bbd4105ec57df975d7e095a80b859d011d5bfb6d503d97954db2

Contents?: true

Size: 879 Bytes

Versions: 12

Compression:

Stored size: 879 Bytes

Contents

# frozen_string_literal: true

require 'local_uri'
require 'webmock'

class DHS::Record
  DEFAULT_LIMIT = DHS::Pagination::Base::DEFAULT_LIMIT

  def self.stub_all(url, items, options = {})
    extend WebMock::API

    items.each_slice(DEFAULT_LIMIT).with_index do |(*batch), index|
      uri = LocalUri::URI.new(url)
      uri.query.merge!(
        limit_key(:parameter) => DEFAULT_LIMIT
      )
      offset = pagination_class.page_to_offset(index + 1, DEFAULT_LIMIT)
      unless index.zero?
        uri.query.merge!(
          pagination_key(:parameter) => offset
        )
      end
      request_stub = stub_request(:get, uri.to_s)
      request_stub.with(options) if options.present?
      request_stub.to_return(
        body: {
          items: batch,
          offset: index.zero? ? 0 : offset,
          total: items.length
        }.to_json
      )
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dhs-1.6.0 lib/dhs/test/stubbable_records.rb
dhs-1.5.0 lib/dhs/test/stubbable_records.rb
dhs-1.4.2 lib/dhs/test/stubbable_records.rb
dhs-1.4.1 lib/dhs/test/stubbable_records.rb
dhs-1.4.0 lib/dhs/test/stubbable_records.rb
dhs-1.3.0 lib/dhs/test/stubbable_records.rb
dhs-1.2.0 lib/dhs/test/stubbable_records.rb
dhs-1.1.0 lib/dhs/test/stubbable_records.rb
dhs-1.0.3 lib/dhs/test/stubbable_records.rb
dhs-1.0.2 lib/dhs/test/stubbable_records.rb
dhs-1.0.1 lib/dhs/test/stubbable_records.rb
dhs-1.0.0 lib/dhs/test/stubbable_records.rb