Class: Alma::Electronic::BatchUtils
- Inherits:
-
Object
- Object
- Alma::Electronic::BatchUtils
- Defined in:
- lib/alma/electronic/batch_utils.rb
Instance Attribute Summary collapse
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#build_failed_ids(options = {}) ⇒ Object
Builds list of ids from logs based on failed attempts.
-
#build_ids(options = {}) ⇒ Object
Builds a list of all ids for a specific session.
-
#build_notes(options = {}) ⇒ Object
Builds the notes object using the logs.
-
#build_successful_ids(options = {}) ⇒ Object
Builds list of ids from logs based on successful attempts.
- #get_collection_notes(ids = nil, options = {}) ⇒ Object
- #get_notes(options = {}) ⇒ Object
- #get_portfolio_notes(ids = nil, options = {}) ⇒ Object
- #get_service_ids(ids = @ids, options = {}) ⇒ Object
- #get_service_notes(ids = nil, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ BatchUtils
constructor
A new instance of BatchUtils.
- #print_notes(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BatchUtils
Returns a new instance of BatchUtils.
22 23 24 25 26 27 28 29 30 |
# File 'lib/alma/electronic/batch_utils.rb', line 22 def initialize( = {}) ||= {} @chain = .fetch(:chain, false) @ids = .fetch(:ids, []) @type = .fetch(:type, "collection") @tag = .fetch(:tag, Time.now.to_s) @@logger = [:logger] || Logger.new("log/electronic_batch_process.log") end |
Instance Attribute Details
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
14 15 16 |
# File 'lib/alma/electronic/batch_utils.rb', line 14 def notes @notes end |
#type ⇒ Object (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( = {}) successful_ids = build_successful_ids() get_logged_items() .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( = {}) build_failed_ids() + build_successful_ids() 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( = {}) ||= {} type ||= .fetch(:type, "collection") get_logged_items() .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( = {}) get_logged_items() .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, = {}) ids ||= @ids get_notes(.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( = {}) ||= {} chain = .fetch(:chain, @chain) ids = [:ids] || (chain ? build_ids() : @ids) type = .fetch(:type, @type) tag = .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. } 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(.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, = {}) ids ||= @ids get_notes(.merge(ids: get_portfolio_ids(ids, , 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, = {}) tag = .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.) .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, = {}) ids ||= @ids get_notes(.merge(ids: get_service_ids(ids, ), type: "service")) end |
#print_notes(options = {}) ⇒ Object
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( = {}) ||= {} chain = .fetch(:chain, @chain) notes = [:notes] || chain ? build_notes() : @notes type = .fetch(:type, @type) tag = .fetch(:tag, @tag) filename = .fetch(:filename, "spec/fixtures/#{type}_notes.json") File.open(filename, "w") do |file| file.write(JSON.pretty_generate(notes)) end self.class.new(.merge( chain:, notes:, type:, tag:, logger: @@logger, )) end |