-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathresearch_safety_certification_lookup.rb
More file actions
51 lines (39 loc) · 1.38 KB
/
research_safety_certification_lookup.rb
File metadata and controls
51 lines (39 loc) · 1.38 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
# frozen_string_literal: true
class ResearchSafetyCertificationLookup
# Replace the value of this attribute in your ENV or settings.yml
# to connect to the school's custom API.
cattr_accessor(:adapter_class) { Settings.research_safety_adapter.class_name.constantize }
# This is a class method for easier stubbing in specs
def self.adapter(user)
adapter_class.new(user)
end
# Allows either a single certificate or an array of certificates
def self.certified?(user, certificates)
new(user).certified?(certificates)
end
def self.certificates_with_status_for(user)
new(user).certificates_with_status
end
def initialize(user)
@user = user
end
# Allows either a single certificate or an array of certificates
def certified?(certificates)
Array(certificates).all? { |certificate| adapter.certified?(certificate) }
end
def missing_from(certificates)
certificates.reject { |certificate| adapter.certified?(certificate) }
end
def certificates_with_status
ResearchSafetyCertificate.ordered.to_h do |certificate|
[certificate, adapter.certified?(certificate)]
end
end
private
# This is memoized so the adapter class can do its own internal caching/memoization
# For example, it might only make one HTTP request to return all of the user's
# certifications.
def adapter
@adapter ||= self.class.adapter(@user)
end
end