Class: Alma::BibItemSet

Inherits:
ResultSet show all
Defined in:
lib/alma/bib_item_set.rb

Defined Under Namespace

Classes: ResponseError

Constant Summary collapse

ITEMS_PER_PAGE =
100

Instance Attribute Summary collapse

Attributes inherited from ResultSet

#response

Instance Method Summary collapse

Methods included from Error

#error, #error_message, #has_error?

Constructor Details

#initialize(response, options = {}) ⇒ BibItemSet

Returns a new instance of BibItemSet.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/alma/bib_item_set.rb', line 16

def initialize(response, options = {})
  @raw_response = response
  parsed = response.parsed_response
  @total_record_count = parsed["total_record_count"]
  @options = options
  @mms_id = @options.delete(:mms_id)

  validate(response)
  @items = (parsed.fetch(key, []) || [])
    .map { |item| single_record_class.new(item) }
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



10
11
12
# File 'lib/alma/bib_item_set.rb', line 10

def items
  @items
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



11
12
13
# File 'lib/alma/bib_item_set.rb', line 11

def raw_response
  @raw_response
end

#total_record_countObject (readonly)

Returns the value of attribute total_record_count.



11
12
13
# File 'lib/alma/bib_item_set.rb', line 11

def total_record_count
  @total_record_count
end

Instance Method Details

#allObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/alma/bib_item_set.rb', line 52

def all
  @last_page_index ||= false
  Enumerator.new do |yielder|
    offset = 0
    while (!@last_page_index || @last_page_index >= offset / items_per_page) do
      r = (offset == 0) ? self : single_record_class.find(@mms_id, options = @options.merge({ limit: items_per_page, offset: }))
      unless r.empty?
        r.map { |item| yielder << item }
        @last_page_index = (offset / items_per_page)
      else
        @last_page_index = @last_page_index ? @last_page_index - 1 : -1
      end

      if r.size == items_per_page
        @last_page_index += 1
      end

      offset += items_per_page
    end
  end
end

#each(&block) ⇒ Object



74
75
76
# File 'lib/alma/bib_item_set.rb', line 74

def each(&block)
  @items.each(&block)
end

#filter_missing_and_lostObject



46
47
48
49
50
# File 'lib/alma/bib_item_set.rb', line 46

def filter_missing_and_lost
  clone = dup
  clone.items = reject(&:missing_or_lost?)
  clone
end

#grouped_by_libraryObject



42
43
44
# File 'lib/alma/bib_item_set.rb', line 42

def grouped_by_library
  group_by(&:library)
end

#items_per_pageObject



90
91
92
# File 'lib/alma/bib_item_set.rb', line 90

def items_per_page
  ITEMS_PER_PAGE
end

#keyObject



82
83
84
# File 'lib/alma/bib_item_set.rb', line 82

def key
  "item"
end

#loggableObject



28
29
30
31
32
33
# File 'lib/alma/bib_item_set.rb', line 28

def loggable
  { total_record_count: @total_record_count.to_s,
    mms_id: @mms_id,
    uri: @raw_response&.request&.uri.to_s
  }.select { |k, v| !(v.nil? || v.empty?) }
end

#single_record_classObject



86
87
88
# File 'lib/alma/bib_item_set.rb', line 86

def single_record_class
  Alma::BibItem
end

#success?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/alma/bib_item_set.rb', line 78

def success?
  raw_response.response.code.to_s == "200"
end

#validate(response) ⇒ Object



35
36
37
38
39
40
# File 'lib/alma/bib_item_set.rb', line 35

def validate(response)
  if response.code != 200
    log = loggable.merge(response.parsed_response)
    raise ResponseError.new("Could not get bib items.", log)
  end
end