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
- #class_name(t) ⇒ Object
-
#compare_by_attributes(v1, v2, *attrs) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
-
#compare_values(v1, v2) ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity.
- #do_get(uri, ignore_errors: false) ⇒ Object
- #tidy_value(value) ⇒ Object
Instance Method Details
#class_name(t) ⇒ Object
57 58 59 60 61 |
# File 'lib/berkeley_library/av/util.rb', line 57 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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/berkeley_library/av/util.rb', line 26 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
43 44 45 46 47 48 49 50 51 |
# File 'lib/berkeley_library/av/util.rb', line 43 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 20 21 22 23 |
# File 'lib/berkeley_library/av/util.rb', line 14 def do_get(uri, ignore_errors: false) headers = { user_agent: DEFAULT_USER_AGENT } if uri.to_s.start_with?(BerkeleyLibrary::AV::Config.tind_base_uri.to_s) && ENV['LIT_TIND_API_KEY'] headers[:authorization] = "Token #{ENV['LIT_TIND_API_KEY']}" end body = URIs.get(uri, headers:) body && body.scrub rescue RestClient::Exception raise unless ignore_errors end |
#tidy_value(value) ⇒ Object
53 54 55 |
# File 'lib/berkeley_library/av/util.rb', line 53 def tidy_value(value) value && value.gsub(/[[:space:]]*-[[:space:]]*/, '-').strip end |