· 6 years ago · Feb 11, 2020, 12:26 PM
1# frozen_string_literal: true
2
3# Publicly accessible citation evidence for violators to view when
4# viewing their citation information on the public violator portal
5# (https://www.alertbus.com).
6# Shares some of its api with {PrintEvidence}.
7class PublicEvidence
8 CLUSTER_SIZE = 1000
9
10 # @return [Array<String>]
11 # the three photo evidence files (via filename) needed for printing
12 PRINT_FILENAMES = %w[back.jpg lic.jpg over.jpg].freeze
13
14 attr_reader :key, :parent, :partner
15
16 delegate :children, to: :to_pathname
17 delegate :exist?, to: :to_pathname
18 delegate :empty?, to: :to_pathname
19 delegate :join, to: :to_pathname
20 delegate :relative_public_url, to: :to_pathname
21 delegate :to_s, to: :to_pathname
22
23 def initialize(key:, parent: nil, partner:)
24 parent ||= AlertBus[:folders][:evidence]
25 @key = key
26 @parent = Pathname.new(parent.to_s)
27 @partner = partner
28 end
29
30 def ==(other)
31 return false unless other.respond_to?(:to_path)
32 to_path == other.to_path
33 end
34
35 def citation_cluster
36 (key / CLUSTER_SIZE).to_s.rjust(4, '0')
37 end
38
39 def hash
40 Digest::MD5.hexdigest("#{key} Night Cheese #{key}")
41 end
42
43 # @param filename [String]
44 # the name of the file to look for within the public evidence locker
45 # @return [Boolean]
46 # if the public evidence locker has a file with the specified name
47 def includes?(filename)
48 children.any? { |child| child.basename.to_s == filename }
49 end
50
51 # @see http://ruby-doc.org/core-2.6.2/BasicObject.html#method-i-method_missing
52 # Invoked when PublicEvidence is sent a method it cannot handle.
53 # If {#to_path} can respond to the method, it will handle the request.
54 # @param method [Symbol]
55 # the method called
56 # @param args [Array<Object>]
57 # any arguments passed to the method
58 # @return
59 # the method call's result
60 def method_missing(method, *args)
61 return to_path.send(method, *args) if to_path.respond_to?(method)
62 super
63 end
64
65 # @return [Boolean]
66 # if the public evidence has enough photos present to be printed
67 def printable_evidence_exist?
68 PRINT_FILENAMES.all? { |filename| includes?(filename) }
69 end
70
71 # @return [Array<String>]
72 # a collection of print files this public evidence locker doesn't have,
73 # see {PublicEvidence::PRINT_FILENAMES}
74 def printable_evidence_needed
75 PRINT_FILENAMES.select { |filename| !includes?(filename) }
76 end
77
78 # @return [Boolean]
79 # if the public evidence locker is missing files needed for printing
80 def printable_evidence_needed?
81 PRINT_FILENAMES.any? { |filename| !includes?(filename) }
82 end
83
84 # @param method [Symbol]
85 # the method's name as a symbol
86 # @return [Boolean]
87 # if this class can respond to the method
88 def respond_to_missing?(method, *args)
89 to_path.respond_to?(method, *args) || super
90 end
91
92 # @deprecated
93 # Use {#to_pathname} to fit convention, this should return a String.
94 # @return [Pathname]
95 def to_path
96 to_pathname
97 end
98
99 # @return [Pathname]
100 def to_pathname
101 Pathname.new(parent) + partner.id.to_s + citation_cluster.to_s + hash.to_s
102 end
103end