Sha256: 36fdebc4f8cfeef0cd9aad4cae02695a9e6591a5e593a838570a4dabf6b183a0
Contents?: true
Size: 1.99 KB
Versions: 5
Compression:
Stored size: 1.99 KB
Contents
class PurlFetcher::Client::Reader include Enumerable attr_reader :host, :conn, :range def initialize(host: "https://purl-fetcher.stanford.edu", conn: nil) @host = host @conn = conn || Faraday.new(host) do |f| f.response :json end @range = {} end def collection_members(druid) return to_enum(:collection_members, druid) unless block_given? paginated_get("/collections/druid:#{druid.delete_prefix('druid:')}/purls", "purls").each do |obj, _meta| yield obj["druid"].delete_prefix("druid:") end end private ## # @return [Hash] a parsed JSON hash def fetch(path, params) response = conn.get(path, params: params) unless response.success? if defined?(Honeybadger) Honeybadger.context({ path:, params:, response_code: response.code, body: response.body }) end raise PurlFetcher::Client::ResponseError, "Unsuccessful response from purl-fetcher" end response.body end ## # For performance, and enumberable object is returned. # # @example operating on each of the results as they come in # paginated_get('/docs/collections/druid:123', 'purls').map { |v| puts v.inspect } # # @example getting all of the results and converting to an array # paginated_get('/docs/collections/druid:123', 'purls').to_a # # @return [Enumerator] an enumberable object def paginated_get(path, accessor, options = {}) Enumerator.new do |yielder| params = options.dup per_page = params.delete(:per_page) { 1000 } page = params.delete(:page) { 1 } max = params.delete(:max) { 1_000_000 } total = 0 loop do data = fetch(path, { per_page: per_page, page: page }.merge(params)) @range = data["range"] total += data[accessor].length data[accessor].each do |element| yielder.yield element, self end page = data["pages"]["next_page"] break if page.nil? || total >= max end end end end
Version data entries
5 entries across 5 versions & 1 rubygems