Class: Krikri::Harvesters::ApiHarvester

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

Overview

A harvester implementation for REST APIs. The default ApiHarvester expects Solr-like JSON responses/records.

An internal interface is provided for easier subclassing. A new API harvester may reimplement:

- #get_docs (to retrieve record docs from a response)
- #get_count (to determine total record count from a response)
- #get_identifier (to retrieve an indentifier from a record document)
- #get_content (to retrieve a content string from a record document)
- #next_options` (to generate the parameters for the next request)

If the content type of the records is other than JSON, you will also want to override ‘#content_type`.

Constant Summary

Constants included from Krikri::Harvester

Krikri::Harvester::Registry

Instance Attribute Summary collapse

Attributes included from Krikri::Harvester

#name, #uri

Attributes included from SoftwareAgent

#entity_behavior

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Krikri::Harvester

#run

Methods included from SoftwareAgent

#agent_name, #run

Constructor Details

#initialize(opts = {}) ⇒ ApiHarvester

Returns a new instance of ApiHarvester.

Parameters:

  • opts (Hash) (defaults to: {})

    options for the harvester

See Also:



24
25
26
27
# File 'lib/krikri/harvesters/api_harvester.rb', line 24

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

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



19
20
21
# File 'lib/krikri/harvesters/api_harvester.rb', line 19

def opts
  @opts
end

Class Method Details

.expected_optsHash

Returns A hash documenting the allowable options to pass to initializers.

Returns:

  • (Hash)

    A hash documenting the allowable options to pass to initializers.

See Also:

  • Krikri::Harvester::expected_opts


34
35
36
37
38
39
40
41
# File 'lib/krikri/harvesters/api_harvester.rb', line 34

def self.expected_opts
  {
    key: :api,
    opts: {
      params: { type: :string, required: false }
    }
  }
end

Instance Method Details

#content_typeString

Returns the content type for the records generated by this harvester.

Returns:

  • (String)

    the content type for the records generated by this harvester



76
77
78
# File 'lib/krikri/harvesters/api_harvester.rb', line 76

def content_type
  'application/json'
end

#countObject



45
46
47
# File 'lib/krikri/harvesters/api_harvester.rb', line 45

def count
  get_count(request(opts))
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



68
69
70
71
# File 'lib/krikri/harvesters/api_harvester.rb', line 68

def get_record(identifier)
  response = request(:params => { :q => "id:#{identifier.to_s}" })
  build_record(get_docs(response).first)
end

#record_idsEnumerator::Lazy

Gets a single record with the given identifier from the API

Returns:

  • (Enumerator::Lazy)

    an enumerator over the ids for the records targeted by this harvester.



61
62
63
# File 'lib/krikri/harvesters/api_harvester.rb', line 61

def record_ids
  enumerate_records.lazy.map { |r| get_identifier(r) }
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.



52
53
54
# File 'lib/krikri/harvesters/api_harvester.rb', line 52

def records
  enumerate_records.lazy.map { |rec| build_record(rec) }
end