Class: Dor::ReleaseTags::PurlClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dor/release_tags/purl_client.rb

Overview

Calls the purl service and returns the XML document

Instance Method Summary collapse

Constructor Details

#initialize(host:, pid:) ⇒ PurlClient

Returns a new instance of PurlClient.



7
8
9
10
# File 'lib/dor/release_tags/purl_client.rb', line 7

def initialize(host:, pid:)
  @host = host
  @pid = pid
end

Instance Method Details

#fetchNokogiri::HTML::Document

Get XML from the purl service Fetches purl xml for a druid

Returns:

  • (Nokogiri::HTML::Document)

    parsed XML for the druid or an empty document if no purl is found

Raises:

  • (OpenURI::HTTPError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dor/release_tags/purl_client.rb', line 16

def fetch
  handler = proc do |exception, attempt_number, total_delay|
    # We assume a 404 means the document has never been published before and thus has no purl
    Dor.logger.warn "[Attempt #{attempt_number}] GET #{url} -- #{exception.class}: #{exception.message}; #{total_delay} seconds elapsed."
    raise exception unless exception.is_a? OpenURI::HTTPError
    return Nokogiri::HTML::Document.new if exception.io.status.first == '404' # ["404", "Not Found"] from OpenURI::Meta.status
  end

  with_retries(max_retries: 3, base_sleep_seconds: 3, max_sleep_seconds: 5, handler: handler) do |attempt|
    # If you change the method used for opening the webpage, you can change the :rescue param to handle the new method's errors
    Dor.logger.debug "[Attempt #{attempt}] GET #{url}"
    return Nokogiri::XML(OpenURI.open_uri(url))
  end
end