-
Notifications
You must be signed in to change notification settings - Fork 0
/
calfresh.rb
110 lines (94 loc) · 3.35 KB
/
calfresh.rb
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'pdf_forms'
require 'securerandom'
module Calfresh
FORM_FIELDS = {
case_number: 'CASE NUMBER', \
felony_counts: 'code sect', \
code_sections: 'pursuant to Penal Code 117018', \
sentence_is_complete: 'Defendant alleges that she', \
sentence_is_in_progress: 'undefined_3', \
mailing_address: 'undefined_4', \
name_of_defendant: 'TYPE OR PRINT NAME', \
name_of_counsel: 'TYPE OR PRINT NAME_2', \
}
# @todo: Add a second signature field for signature of counsel.
class ApplicationWriter
def initialize
@pdftk = PdfForms.new('pdftk')
end
def fill_out_form(input)
base64_signature_blob = input[:signature]
symbolized_key_input = symbolize_keys(input)
validated_field_input = filter_input_for_valid_fields(symbolized_key_input)
input_for_pdf_writer = map_input_to_pdf_field_names(validated_field_input)
input_for_pdf_writer[FORM_FIELDS[:date]] = Date.today.strftime("%m/%d/%Y")
unique_key = SecureRandom.hex
filled_in_form_path = "/tmp/application_#{unique_key}.pdf"
@pdftk.fill_form('./alameda_county.pdf', filled_in_form_path, input_for_pdf_writer) # @todo: Update.
write_signature_png_to_tmp(base64_signature_blob, unique_key)
convert_application_pdf_to_png_set(unique_key)
add_signature_to_application(unique_key)
Application.new(unique_key)
end
#private
def filter_input_for_valid_fields(form_input_hash)
form_input_hash.select do |human_readable_field_name, value|
FORM_FIELDS.has_key?(human_readable_field_name)
end
end
def map_input_to_pdf_field_names(form_input)
new_hash = Hash.new
form_input.each do |human_readable_field_name, value|
new_hash[FORM_FIELDS[human_readable_field_name]] = value
end
new_hash
end
def write_signature_png_to_tmp(signature_blob, unique_key)
system("echo #{signature_blob} | base64 --decode > /tmp/signature_#{unique_key}.png")
end
def convert_application_pdf_to_png_set(unique_key)
system("convert -alpha deactivate -density 300 -depth 8 -quality 85 /tmp/application_#{unique_key}.pdf /tmp/application_#{unique_key}.png")
end
def add_signature_to_application(unique_key)
system("composite -geometry +31+2700 /tmp/signature_#{unique_key}.png /tmp/application_#{unique_key}.png /tmp/application_#{unique_key}.png")
end
def symbolize_keys(hash)
symbolized_hash = Hash.new
hash.each { |key,value| symbolized_hash[key.to_sym] = value }
symbolized_hash
end
end
class Application
attr_reader :unique_key
def initialize(unique_key)
@unique_key = unique_key
end
def has_pngs?
files_exist = true
png_filenames.each do |filename|
if File.exists?(filename) == false
files_exist = false
end
end
files_exist
end
def png_file_set
file_array = Array.new
png_filenames.each do |filename|
file_array << File.new(filename)
end
file_array
end
def png_filenames
filename_array = Array.new
filename_array << "/tmp/application_#{@unique_key}.png"
(7..15).each do |page_number|
filename_array << "calfresh_application_images/page-#{page_number}.png"
end
filename_array
end
def signed_png_path
"/tmp/application_#{@unique_key}.png"
end
end
end