Class: BerkeleyLibrary::TIND::API::Search

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/berkeley_library/tind/api/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection: nil, pattern: nil, index: nil, date_range: nil, format: Format::XML) ⇒ Search

Returns a new instance of Search.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
# File 'lib/berkeley_library/tind/api/search.rb', line 14

def initialize(collection: nil, pattern: nil, index: nil, date_range: nil, format: Format::XML)
  raise ArgumentError, 'Search requires a collection' unless collection

  @collection = collection
  @pattern = pattern
  @index = index
  @date_range = DateRange.ensure_date_range(date_range)
  @format = Format.ensure_format(format)
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



12
13
14
# File 'lib/berkeley_library/tind/api/search.rb', line 12

def collection
  @collection
end

#date_rangeObject (readonly)

Returns the value of attribute date_range.



12
13
14
# File 'lib/berkeley_library/tind/api/search.rb', line 12

def date_range
  @date_range
end

#formatObject (readonly)

Returns the value of attribute format.



12
13
14
# File 'lib/berkeley_library/tind/api/search.rb', line 12

def format
  @format
end

#indexObject (readonly)

Returns the value of attribute index.



12
13
14
# File 'lib/berkeley_library/tind/api/search.rb', line 12

def index
  @index
end

#patternObject (readonly)

Returns the value of attribute pattern.



12
13
14
# File 'lib/berkeley_library/tind/api/search.rb', line 12

def pattern
  @pattern
end

Instance Method Details

#each_result(freeze: false) {|marc_record| ... } ⇒ self #each_result(freeze: false) ⇒ Enumerable<MARC::Record>

Iterates over the records returned by this search.

Overloads:

  • #each_result(freeze: false) {|marc_record| ... } ⇒ self

    Yields each record to the provided block.

    Parameters:

    • freeze (Boolean) (defaults to: false)

      whether to freeze each record before yielding.

    Yield Parameters:

    • marc_record (MARC::Record)

      each record

    Returns:

    • (self)
  • #each_result(freeze: false) ⇒ Enumerable<MARC::Record>

    Returns an enumerator of the records.

    Parameters:

    • freeze (Boolean) (defaults to: false)

      whether to freeze each record before yielding.

    Returns:

    • (Enumerable<MARC::Record>)

      the records



52
53
54
55
56
57
# File 'lib/berkeley_library/tind/api/search.rb', line 52

def each_result(freeze: false, &block)
  return to_enum(:each_result, freeze: freeze) unless block_given?

  perform_search(freeze: freeze, &block)
  self
end

#paramsObject

rubocop: disable Metrics/AbcSize



25
26
27
28
29
30
31
32
33
# File 'lib/berkeley_library/tind/api/search.rb', line 25

def params
  @params ||= {}.tap do |params|
    params[:c] = collection if collection
    params[:p] = pattern if pattern
    params[:f] = index if index
    params.merge!(date_range.to_params) if date_range
    params[:format] = self.format.to_s if self.format
  end
end

#resultsArray<MARC::Record>

Performs this search and returns the results as array.

Returns:

  • (Array<MARC::Record>)

    the results



38
39
40
# File 'lib/berkeley_library/tind/api/search.rb', line 38

def results
  each_result.to_a
end