Module: BerkeleyLibrary::AV::Util

Extended by:
Util
Includes:
Logging, Util
Included in:
Marc::Util, Metadata::Readers::Base, Metadata::Value, Metadata::Value, RecordId, Track, Util
Defined in:
lib/berkeley_library/av/util.rb

Constant Summary collapse

DEFAULT_USER_AGENT =
"#{Core::ModuleInfo::NAME} #{Core::ModuleInfo::VERSION} (#{Core::ModuleInfo::HOMEPAGE})".freeze

Instance Method Summary collapse

Instance Method Details

#class_name(t) ⇒ Object



53
54
55
56
57
# File 'lib/berkeley_library/av/util.rb', line 53

def class_name(t)
  return class_name(t.class) unless t.is_a?(Class) || t.is_a?(Module)

  t.name.sub(/^.*::/, '')
end

#compare_by_attributes(v1, v2, *attrs) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/berkeley_library/av/util.rb', line 22

def compare_by_attributes(v1, v2, *attrs)
  return 0 if v1.equal?(v2)
  return if v2.nil?

  attr_order = attrs.lazy.filter_map do |attr|
    return nil unless v2.respond_to?(attr)

    a1 = v1.send(attr)
    a2 = v2.send(attr)
    o = compare_values(a1, a2)
    o unless o.nil? || o == 0
  end.first

  attr_order || 0
end

#compare_values(v1, v2) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity



39
40
41
42
43
44
45
46
47
# File 'lib/berkeley_library/av/util.rb', line 39

def compare_values(v1, v2)
  return 0 if v1 == v2
  return 1 if v1.nil?
  return -1 if v2.nil?
  # TODO: better array comparison
  return compare_values(v1.to_s, v2.to_s) unless v1.respond_to?(:<)

  v1 < v2 ? -1 : 1
end

#do_get(uri, ignore_errors: false) ⇒ Object



14
15
16
17
18
19
# File 'lib/berkeley_library/av/util.rb', line 14

def do_get(uri, ignore_errors: false)
  body = URIs.get(uri, headers: { user_agent: DEFAULT_USER_AGENT })
  body && body.scrub
rescue RestClient::Exception
  raise unless ignore_errors
end

#tidy_value(value) ⇒ Object



49
50
51
# File 'lib/berkeley_library/av/util.rb', line 49

def tidy_value(value)
  value && value.gsub(/[[:space:]]*-[[:space:]]*/, '-').strip
end