Class: Alma::Electronic::BatchUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/alma/electronic/batch_utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BatchUtils

Returns a new instance of BatchUtils.

Parameters:

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

    The options to create a batch instance.

  • [true, (Hash)

    a customizable set of options

  • [Array<String>] (Hash)

    a customizable set of options

  • ["collection", (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Logger] (Hash)

    a customizable set of options



22
23
24
25
26
27
28
29
30
# File 'lib/alma/electronic/batch_utils.rb', line 22

def initialize(options = {})
  options ||= options {}
  @chain = options.fetch(:chain, false)
  @ids = options.fetch(:ids, [])
  @type = options.fetch(:type, "collection")
  @tag = options.fetch(:tag, Time.now.to_s)

  @@logger = options[:logger] || Logger.new("log/electronic_batch_process.log")
end

Instance Attribute Details

#notesObject (readonly)

Returns the value of attribute notes.



14
15
16
# File 'lib/alma/electronic/batch_utils.rb', line 14

def notes
  @notes
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/alma/electronic/batch_utils.rb', line 14

def type
  @type
end

Instance Method Details

#build_failed_ids(options = {}) ⇒ Object

Builds list of ids from logs based on failed attempts. Useful for rebuilding part of collection.



144
145
146
147
148
149
150
151
# File 'lib/alma/electronic/batch_utils.rb', line 144

def build_failed_ids(options = {})
  successful_ids = build_successful_ids(options)
  get_logged_items(options)
    .select { |item| item.slice("authentication_note", "public_note").values.all?(&:nil?) }
    .map { |item| item["collection_id"] }
    .select { |id| !successful_ids.include? id }
    .uniq
end

#build_ids(options = {}) ⇒ Object

Builds a list of all ids for a specific session. Useful for analytics purpose or rebuilds.



164
165
166
# File 'lib/alma/electronic/batch_utils.rb', line 164

def build_ids(options = {})
  build_failed_ids(options) + build_successful_ids(options)
end

#build_notes(options = {}) ⇒ Object

Builds the notes object using the logs.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/alma/electronic/batch_utils.rb', line 129

def build_notes(options = {})
  options ||= {}
  type ||= options.fetch(:type, "collection")

  get_logged_items(options)
    .select { |item| item.slice("authentication_note", "public_note").values.any?(&:present?) }
    .inject({}) do |nodes, item|

    id = item["#{type}_id"]
    nodes.merge(id => item.slice("authentication_note", "public_note"))
  end
end

#build_successful_ids(options = {}) ⇒ Object

Builds list of ids from logs based on successful attempts. Useful for verifying that failed ids have always failed.



155
156
157
158
159
160
# File 'lib/alma/electronic/batch_utils.rb', line 155

def build_successful_ids(options = {})
  get_logged_items(options)
    .select { |item| item.slice("authentication_note", "public_note").values.present? }
    .map { |item| item["collection_id"] }
    .uniq
end

#get_collection_notes(ids = nil, options = {}) ⇒ Object



32
33
34
35
# File 'lib/alma/electronic/batch_utils.rb', line 32

def get_collection_notes(ids = nil, options = {})
  ids ||= @ids
  get_notes(options.merge(ids: make_collection_ids(ids), type: "collection"))
end

#get_notes(options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/alma/electronic/batch_utils.rb', line 47

def get_notes(options = {})
  options ||= {}
  chain = options.fetch(:chain, @chain)
  ids = options[:ids] || (chain ? build_ids(options) : @ids)

  type = options.fetch(:type, @type)
  tag = options.fetch(:tag, @tag)
  @notes = ids.inject({}) do |notes, params|
    id = get_id(type, params)
    start = Time.now

    begin
      item = Alma::Electronic.get(params)
    rescue StandardError => e
      item = { "error" => e.message }
    end

    data = ["error", "authentication_note", "public_note"].reduce({}) do |acc, field|
      acc[field] = item[field] if item[field].present?
      acc
    end

    unavailable = item.dig("service_temporarily_unavailable", "value")
    if unavailable == "1" || unavailable == "true"
      data.merge!(item.slice("service_temporarily_unavailable", "service_unavailable_date", "service_unavailable_reason"))
    end

    if data.present?
      log(params.merge(data).merge(type:, start:, tag:))

      notes[id] = data unless data["error"].present?
    end

    notes
  end

  self.class.new(options.merge(
                   chain:,
                   ids:,
                   type:,
                   tag:,
                   notes:,
                   logger: @@logger,
  ))
end

#get_portfolio_notes(ids = nil, options = {}) ⇒ Object



42
43
44
45
# File 'lib/alma/electronic/batch_utils.rb', line 42

def get_portfolio_notes(ids = nil, options = {})
  ids ||= @ids
  get_notes(options.merge(ids: get_portfolio_ids(ids, options, type: "portfolio")))
end

#get_service_ids(ids = @ids, options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/alma/electronic/batch_utils.rb', line 93

def get_service_ids(ids = @ids, options = {})
  tag = options.fetch(:tag, @tag)
  start = Time.now

  make_collection_ids(ids)
    .map { |id| id.merge(type: "services") }
    .inject([]) do |service_ids, params|
    params.merge!(tag:)

    begin
      item = Alma::Electronic.get(params)

      if item["errorList"]
        log params.merge(item["errorList"])
          .merge(start:)
      else
        item["electronic_service"].each { |service|
          service_id = { service_id: service["id"].to_s }
          service_ids << params.slice(:collection_id)
            .merge(service_id)

          log params.merge(service_id)
            .merge(start:)
        }
      end

    rescue StandardError => e
      log params.merge("error" => e.message)
        .merge(start:)
    end

    service_ids
  end
end

#get_service_notes(ids = nil, options = {}) ⇒ Object



37
38
39
40
# File 'lib/alma/electronic/batch_utils.rb', line 37

def get_service_notes(ids = nil, options = {})
  ids ||= @ids
  get_notes(options.merge(ids: get_service_ids(ids, options), type: "service"))
end


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/alma/electronic/batch_utils.rb', line 168

def print_notes(options = {})
  options ||= {}
  chain = options.fetch(:chain, @chain)
  notes = options[:notes] || chain ? build_notes(options) : @notes
  type = options.fetch(:type, @type)
  tag = options.fetch(:tag, @tag)

  filename = options.fetch(:filename, "spec/fixtures/#{type}_notes.json")

  File.open(filename, "w") do |file|
    file.write(JSON.pretty_generate(notes))
  end

  self.class.new(options.merge(
                   chain:,
                   notes:,
                   type:,
                   tag:,
                   logger: @@logger,
  ))
end