Class: Krikri::Harvesters::ApiHarvester
- Inherits:
-
Object
- Object
- Krikri::Harvesters::ApiHarvester
- 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
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Attributes included from Krikri::Harvester
Attributes included from SoftwareAgent
Class Method Summary collapse
-
.expected_opts ⇒ Hash
A hash documenting the allowable options to pass to initializers.
Instance Method Summary collapse
-
#content_type ⇒ String
The content type for the records generated by this harvester.
- #count ⇒ Object
-
#get_record(identifier) ⇒ #to_s
The record.
-
#initialize(opts = {}) ⇒ ApiHarvester
constructor
A new instance of ApiHarvester.
-
#record_ids ⇒ Enumerator::Lazy
Gets a single record with the given identifier from the API.
-
#records ⇒ Enumerator::Lazy
An enumerator of the records targeted by this harvester.
Methods included from Krikri::Harvester
Methods included from SoftwareAgent
Constructor Details
#initialize(opts = {}) ⇒ ApiHarvester
Returns a new instance of ApiHarvester.
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
#opts ⇒ Object (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_opts ⇒ Hash
Returns A hash documenting the allowable options to pass to initializers.
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_type ⇒ String
Returns 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 |
#count ⇒ Object
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.
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_ids ⇒ Enumerator::Lazy
Gets a single record with the given identifier from the API
61 62 63 |
# File 'lib/krikri/harvesters/api_harvester.rb', line 61 def record_ids enumerate_records.lazy.map { |r| get_identifier(r) } end |
#records ⇒ Enumerator::Lazy
Returns 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 |