Class: Krikri::Harvesters::PrimoHarvester

Inherits:
Object
  • Object
show all
Includes:
Krikri::Harvester
Defined in:
lib/krikri/harvesters/primo_harvester.rb

Overview

A harvester implementation for Primo

Accepts options passed as ‘:primo => opts`

Options allowed are:

- bulk_size: The number of records to fetch from Primo per request
  (default: 500)

Constant Summary collapse

SEAR_NS =
'http://www.exlibrisgroup.com/xsd/jaguar/search'
PRIMO_NS =
'http://www.exlibrisgroup.com/xsd/primo/primo_nm_bib'
PrimoHarvestError =
Class.new(StandardError)

Constants included from Krikri::Harvester

Krikri::Harvester::Registry

Instance Attribute Summary

Attributes included from Krikri::Harvester

#name, #uri

Attributes included from SoftwareAgent

#entity_behavior

Instance Method Summary collapse

Methods included from Krikri::Harvester

expected_opts, #record_ids, #run

Methods included from SoftwareAgent

#agent_name, #run

Constructor Details

#initialize(opts = {}) ⇒ PrimoHarvester

Returns a new instance of PrimoHarvester.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/krikri/harvesters/primo_harvester.rb', line 20

def initialize(opts = {})
  @opts = opts.fetch(:primo, {})
  super

  @opts[:bulk_size] ||= 500

  @http_conn = Faraday.new do |conn|
    conn.request :retry, :max => 3
    conn.response :follow_redirects, :limit => 5
    conn.response :logger, Rails.logger
    conn.adapter :net_http
  end
end

Instance Method Details

#countInteger

Returns the number of records available for harvesting.

Returns:

  • (Integer)

    the number of records available for harvesting.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/krikri/harvesters/primo_harvester.rb', line 36

def count
  response = @http_conn.get(uri, :indx => '1', :bulkSize => '1')

  unless response.status == 200
    raise PrimoHarvestError, "Couldn't get record count"
  end

  total_hits = Nokogiri::XML(response.body)
               .xpath('//sear:DOCSET')
               .first
               .attr('TOTALHITS')

  Integer(total_hits)
end

#get_record(identifier) ⇒ #to_s

Returns the record.

Parameters:

  • identifier (#to_s)

    the identifier of the record to get

Returns:

  • (#to_s)

    the record



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/krikri/harvesters/primo_harvester.rb', line 72

def get_record(identifier)
  response = @http_conn.get(uri,
                            :indx => 1,
                            :bulkSize => 1,
                            :query => "rid,exact,#{identifier}")

  unless response.status == 200
    raise PrimoHarvestError, "Couldn't get record: #{identifier}"
  end

  enumerate_records(response.body).first
end

#recordsEnumerator::Lazy

Returns an enumerator of the records targeted by this harvester.

Returns:

  • (Enumerator::Lazy)

    an enumerator of the records targeted by this harvester.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/krikri/harvesters/primo_harvester.rb', line 54

def records
  bulk_size = @opts.fetch(:bulk_size)

  (1...count).step(bulk_size).lazy.flat_map do |offset|
    response = @http_conn.get(uri, :indx => offset, :bulkSize => bulk_size)

    unless response.status == 200
      raise PrimoHarvestError, "Record fetch from #{offset} to " \
                               "#{offset + bulk_size} failed"
    end

    enumerate_records(response.body)
  end
end