-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.rb
More file actions
69 lines (55 loc) · 1.87 KB
/
util.rb
File metadata and controls
69 lines (55 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require 'rest-client'
require 'berkeley_library/logging'
require 'berkeley_library/util'
require 'berkeley_library/av/core/module_info'
module BerkeleyLibrary
module AV
module Util
include BerkeleyLibrary::Logging
include BerkeleyLibrary::Util
DEFAULT_USER_AGENT = "#{Core::ModuleInfo::NAME} #{Core::ModuleInfo::VERSION} (#{Core::ModuleInfo::HOMEPAGE})".freeze
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
# rubocop:disable Metrics/CyclomaticComplexity
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
# rubocop:enable Metrics/CyclomaticComplexity
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
def tidy_value(value)
value && value.gsub(/[[:space:]]*-[[:space:]]*/, '-').strip
end
def class_name(t)
return class_name(t.class) unless t.is_a?(Class) || t.is_a?(Module)
t.name.sub(/^.*::/, '')
end
class << self
include AV::Util
end
end
end
end