openapply 0.2.10 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,92 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'httparty'
4
- require 'openapply'
5
-
6
- class MySite < Openapply::Client
7
- include HTTParty
8
-
9
- # Defines OpenApply domain name from ENV-VARS
10
- API_URL = ENV['MY_API_BASE_URI']
11
-
12
- # Defines the OpenApply path from ENV-VARS - default is 5 seconds
13
- API_TIMEOUT = (ENV['MY_API_TIMEOUT'].to_i || 5)
14
-
15
- base_uri API_URL
16
- default_timeout API_TIMEOUT
17
-
18
- # Makes OpenApply domain name visible:
19
- def api_url
20
- API_URL
21
- end
22
-
23
- # make OpenApply timeout visible
24
- def api_timeout
25
- API_TIMEOUT
26
- end
27
-
28
- # Defines & makes visible OpenApply secret access key with ENV-VARS
29
- def api_key
30
- ENV['MY_API_AUTH_TOKEN']
31
- end
32
-
33
- # Defines and makes visib le the OpenApply path with ENV-VARS
34
- def api_path
35
- ENV['MY_API_PATH'] || "/api/v1/students/"
36
- end
37
-
38
- # Defines and makes visible the maximum records OpenApply should return
39
- # (code default is 50 - OA default is 10 - doc says 100)
40
- def api_records
41
- ENV['MY_API_RECORDS'] || '50'
42
- end
43
-
44
- def initialize
45
- @my_flatten_keys = [:custom_fields]
46
- @my_reject_keys = [:parent_guardian]
47
- end
48
-
49
- # defines remote host to recieve openapply data
50
- REMOTE_HOSTNAME = ENV['REMOTE_HOSTNAME'].freeze
51
- REMOTE_USERNAME = ENV['REMOTE_USERNAME'].freeze
52
- REMOTE_PATH_FILE = ENV['REMOTE_PATH_FILE'].freeze
53
- REMOTE_PERMISSIONS = ENV['REMOTE_PERMISSIONS'].freeze
54
-
55
- # custom code to prepare my.openapply.com data for remote host recieving data
56
- def records_as_csv_to_file( status, flatten_keys=[],
57
- reject_keys=[], student_keys=[],
58
- guardian_info={}, payment_info={}, file )
59
- data = students_as_csv_by_status( status, flatten_keys, reject_keys,
60
- student_keys, guardian_info, payment_info)
61
- # save csv string as a file
62
- open(file, 'w') { |f| f.puts data }
63
- end
64
-
65
- def records_as_csv_to_server( status, flatten_keys=[],
66
- reject_keys=[], student_keys=[],
67
- guardian_info={}, payment_info={})
68
- data = students_as_csv_by_status( status, flatten_keys, reject_keys,
69
- student_keys, guardian_info, payment_info)
70
- send_data_to_remote_server( data, REMOTE_HOSTNAME, REMOTE_USERNAME,
71
- "#{REMOTE_PATH_FILE}.csv", REMOTE_PERMISSIONS)
72
- end
73
-
74
- # DISABLED UNTIL AXLSX works with rubyzip 1.2.1 or later
75
- # def records_as_xlsx_to_file( status, flatten_keys=[],
76
- # reject_keys=[], student_keys=[],
77
- # guardian_info={}, payment_info={}, file )
78
- # data = students_as_xlsx_by_status( status, flatten_keys, reject_keys,
79
- # student_keys, guardian_info, payment_info)
80
- # data.serialize(file)
81
- # end
82
- #
83
- # def records_as_xlsx_to_server( status, flatten_keys=[],
84
- # reject_keys=[], student_keys=[],
85
- # guardian_info={}, payment_info={})
86
- # data = students_as_xlsx_by_status( status, flatten_keys, reject_keys,
87
- # student_keys, guardian_info, payment_info)
88
- # send_data_to_remote_server( data, REMOTE_HOSTNAME, REMOTE_USERNAME,
89
- # "#{REMOTE_PATH_FILE}.xlsx", REMOTE_PERMISSIONS)
90
- # end
91
-
92
- end
@@ -1,215 +0,0 @@
1
- # require 'net/scp'
2
- # require 'net/ssh'
3
-
4
- # CODE THAT TRANSFORMS STUDENT DATA
5
- ###################################
6
-
7
- module ConvertToArray
8
-
9
-
10
- # Queries by status to get a list of students details of a given status
11
- # and converts the result to an array with headers (based on keys sent)
12
- #
13
- # ==== Attributes
14
- # * +status+ - hash to convert to an array
15
- # * +flatten_keys+ - an array of keys to bring to the top level
16
- # (with this key prepened) -- default (blank does nothing)
17
- # * +reject_keys+ - an array of keys to remove from the data -- default (blank does nothing)
18
- # * +student_keys+ - [:id, :name] - include student record keys
19
- # * +guardian_info+ - include guardian record info {count: 2, keys: [:id, :name]}
20
- # * +payment_info+ - include payment info {count: 2, order: :newest, keys: [:date, :amount]}
21
- # * guardian & payment info options:
22
- # count: 2 -- how many parent or payment records to return)
23
- # keys: [:id, :date] -- an array of keys of data to return
24
- # order: :newest -- the order to return payments :newest (most recent first - default) or :oldest
25
- def students_as_array_by_status(status,
26
- flatten_keys=[], reject_keys=[],
27
- student_keys=[],
28
- guardian_info={}, payment_info={})
29
- #
30
- check = check_details_keys_validity(flatten_keys, reject_keys)
31
- return check unless check.nil? # or check[:error].nil?
32
- #
33
- # by default get payment info (unless no payment info is requested)
34
- get_payments = true
35
- get_payments = false if payment_info.nil? or payment_info.empty?
36
- # go get student details from api
37
- students_hash = students_details_by_status( status,
38
- flatten_keys, reject_keys, get_payments )
39
- #
40
- students_array = students_hash_to_array( students_hash,
41
- student_keys, guardian_info, payment_info)
42
- end
43
- alias_method :students_as_array_by_statuses, :students_as_array_by_status
44
-
45
-
46
-
47
- # ARRAY CODE
48
- ############
49
-
50
- # Given an hash of students_details converts to an arrary
51
- #
52
- # ==== Attributes
53
- # * +students+ - hash to convert to an array
54
- # * +student_keys+ - [:id, :name] - include student record keys
55
- # * +guardian_info+ - include guardian record info {count: 2, keys: [:id, :name]}
56
- # * +payment_info+ - include payment info {count: 2, order: :newest, keys: [:date, :amount]}
57
- # * guardian & payment info options:
58
- # count: 2 -- how many parent or payment records to return)
59
- # keys: [:id, :date] -- an array of keys of data to return
60
- # order: :newest -- the order to return payments :newest (most recent first - default) or :oldest
61
- def students_hash_to_array(students, student_keys=[], guardian_info={}, payment_info={})
62
-
63
- check = check_header_keys_validity(student_keys, guardian_info, payment_info)
64
- return check unless check.nil?
65
-
66
- array = []
67
- array << create_headers( student_keys, guardian_info, payment_info )
68
- return array if students.nil? or students.empty?
69
-
70
- students[:students].each do |student|
71
- row = []
72
-
73
- # next if student.nil? or student.empty? or
74
- # student[:record].nil? or student[:record].empty?
75
-
76
- # when student summary data
77
- if student[:record].nil?
78
- kid_record = student
79
- guardians = []
80
- payments = []
81
- end
82
- # when student detailed data
83
- if student[:record]
84
- kid_record = student[:record]
85
- guardians = student[:guardians]
86
- payments = student[:payments]
87
- end
88
-
89
- # inject student record info into the array
90
- student_keys.each{ |key| row << kid_record[key] }
91
-
92
- # inject guardian record info into the array
93
- if process_key_info?(guardian_info)
94
- count = info_count(guardian_info).to_i - 1
95
- # loop through the correct number of parents
96
- (0..count).each do |i|
97
- # add info if parent record exists
98
- guardian_info[:keys].each{ |key| row << guardians[i][key] } if guardians[i]
99
- # add nils if there isn't a parent record
100
- guardian_info[:keys].each{ |key| row << nil } unless guardians[i]
101
- end
102
- end
103
-
104
- # inject guardian record info (most recent - last to oldest) into the array
105
- if process_key_info?(payment_info)
106
- if payment_info[:order].nil? or payment_info[:order].eql? :newest
107
- # get the newest records first
108
- count = info_count(payment_info).to_i
109
- # loop through the correct number of parents
110
- (1..count).each do |index|
111
- i = index * -1
112
- # puts "INDEX #{i}"
113
- payment_info[:keys].each{ |key| row << payments[i][key] } if payments[i]
114
- payment_info[:keys].each{ |key| row << nil } unless payments[i]
115
- end
116
- else
117
- # start with the oldest records
118
- count = info_count(payment_info).to_i - 1
119
- # loop through the correct number of parents
120
- (0..count).each do |i|
121
- payment_info[:keys].each{ |key| row << payments[i][key] } if payments[i]
122
- payment_info[:keys].each{ |key| row << nil } unless payments[i]
123
- end
124
- end
125
- end
126
- array << row
127
- end
128
- return array
129
- end
130
-
131
- # internal key to process given info or not
132
- def process_key_info?(info)
133
- return true unless info.nil? or info.empty? or
134
- info[:keys].nil? or info[:keys].empty?
135
- return false
136
- end
137
-
138
- # determine count - may extend later to self-discover max number of records
139
- def info_count(info)
140
- info[:count] || 1
141
- end
142
-
143
- # Given the parameters passed in create the create csv / arrary headers
144
- #
145
- # ==== Attributes
146
- # * +students+ - hash to convert to an array
147
- # * +student_keys+ - include student record keys
148
- # * +guardian_info+ - include guardian record info {count: 2, keys: [:id, :name]}
149
- # * +payment_info+ - include payment info {count: 2, order: :newest, keys: [:date, :amount]}
150
- # * guardian & payment info options:
151
- # count: 2 -- how many parent or payment records to return)
152
- # keys: [:id, :date] -- an array of keys of data to return
153
- # order: :newest -- the order to return payments :newest (most recent first - default) or :oldest
154
- def create_headers( student_keys=[], guardian_info={}, payment_info={} )
155
- headers = []
156
- # figure out student headers
157
- student_keys = [:id] if student_keys.nil? or student_keys.empty?
158
- headers = student_keys.map{ |k| "student_" + k.to_s }
159
- # figure out guardian headers
160
- if process_key_info?(guardian_info)
161
- guardian_count = info_count(guardian_info)
162
- # add the correct headers
163
- (1..guardian_count).each do |i|
164
- headers += guardian_info[:keys].map{|k| "guardian#{i}_" + k.to_s }
165
- end
166
- end
167
- # calculate payment headers
168
- if process_key_info?(payment_info)
169
- payment_count = info_count(payment_info)
170
- # add the correct headers
171
- (1..payment_count).each do |i|
172
- headers += payment_info[:keys].map{|k| "payment#{i}_" + k.to_s }
173
- end
174
- end
175
- return headers
176
- end
177
-
178
- # Check header info before processing
179
- #
180
- # === Attributes
181
- # * +student_keys+ - that this is an arrary of symbols
182
- # * +guardian_info+ - that this is a hash with keys: using symbols
183
- # * +payment_info+ - that this is a hash with keys: using symbols
184
- def check_header_keys_validity(student_keys, guardian_info, payment_info)
185
- # prepare keys for testing
186
- student_keys ||= []
187
- #
188
- #
189
- guardian_info ||= {}
190
- return {error: "invalid guardian_info - use hash"} unless guardian_info.is_a? Hash
191
- guardian_keys = guardian_info[:keys] #unless guardian_keys[:keys].nil?
192
- guardian_keys ||= []
193
- #
194
- payment_info ||= {}
195
- return {error: "invalid payment_info - use hash"} unless payment_info.is_a? Hash
196
- payment_keys = payment_info[:keys] #unless payment_keys[:keys].nil?
197
- payment_keys ||= []
198
-
199
- # be sure keys are in an array
200
- return {error: "invalid student_keys - need array"} unless student_keys.is_a? Array
201
- return {error: "invalid guardian_keys - need array"} unless guardian_keys.is_a? Array
202
- return {error: "invalid payment_keys - need array"} unless payment_keys.is_a? Array
203
-
204
- # test if any key values are non-symbols (remain after removing symbols)
205
- return {error: "invalid student_keys - use symbols"} if student_keys.reject{|k| k.is_a? Symbol}.count > 0
206
- return {error: "invalid guardian_keys - use symbols"} if guardian_keys.reject{|k| k.is_a? Symbol}.count > 0
207
- return {error: "invalid payment_keys - use symbols"} if payment_keys.reject{|k| k.is_a? Symbol}.count > 0
208
-
209
- # check that if guardian info is given - it also has keys
210
- return {error: "invalid guardian_keys - keys missing"} if not guardian_info.empty? and guardian_info[:keys].nil?
211
- return {error: "invalid payment_keys - keys missing"} if not payment_info.empty? and payment_info[:keys].nil?
212
-
213
- end
214
-
215
- end
@@ -1,59 +0,0 @@
1
- require 'csv'
2
-
3
- # CODE THAT TRANSFORMS STUDENT DATA
4
- ###################################
5
-
6
- module ConvertToCsv
7
-
8
-
9
- # Queries by status to get a list of students details of a given status
10
- # and converts the result to a CSV string with headers (based on keys sent)
11
- #
12
- # ==== Attributes
13
- # * +status+ - hash to convert to an array
14
- # * +flatten_keys+ - an array of keys to bring to the top level
15
- # (with this key prepened) -- default (blank does nothing)
16
- # * +reject_keys+ - an array of keys to remove from the data -- default (blank does nothing)
17
- # * +student_keys+ - [:id, :name] - include student record keys
18
- # * +guardian_info+ - include guardian record info {count: 2, keys: [:id, :name]}
19
- # * +payment_info+ - include payment info {count: 2, order: :newest, keys: [:date, :amount]}
20
- # * guardian & payment info options:
21
- # count: 2 -- how many parent or payment records to return)
22
- # keys: [:id, :date] -- an array of keys of data to return
23
- # order: :newest -- the order to return payments :newest (most recent first - default) or :oldest
24
- def students_as_csv_by_status( status,
25
- flatten_keys=[], reject_keys=[],
26
- student_keys=[],
27
- guardian_info={}, payment_info={})
28
- #
29
- check = check_details_keys_validity(flatten_keys, reject_keys)
30
- return check unless check.nil? # or check[:error].nil?
31
- # check = check_header_keys_validity(student_keys, guardian_info, payment_info)
32
- # return check unless check.nil?
33
- #
34
- students_array = students_as_array_by_status( status,
35
- flatten_keys, reject_keys,
36
- student_keys, guardian_info, payment_info )
37
- #
38
- return students_array if students_array.is_a? Hash
39
- #
40
- student_csv_txt = students_array_to_csv( students_array )
41
- end
42
- alias_method :students_as_csv_by_statuses, :students_as_csv_by_status
43
-
44
- # Given an array convert to CSV string
45
- #
46
- # ==== Attributes
47
- # +array+ - expects a hash of students_details (should be flattened to use custom fields)
48
- def students_array_to_csv(array)
49
- return "" if array.nil? or array.empty?
50
- # https://stackoverflow.com/questions/4822422/output-array-to-csv-in-ruby
51
- csv_string = CSV.generate do |csv|
52
- array.each do |row|
53
- csv << row
54
- end
55
- end
56
- return csv_string
57
- end
58
-
59
- end
@@ -1,80 +0,0 @@
1
- require 'axlsx'
2
-
3
- # CODE THAT TRANSFORMS STUDENT DATA INTO XLSX DATA
4
- ##################################################
5
-
6
- module ConvertToXlsx
7
-
8
- # XLSX CODE
9
- ###########
10
-
11
- # Queries by status to get a list of students details of a given status
12
- # and converts the result to a XLSX Object (Axlsx::Package) with headers
13
- # (based on keys sent)
14
- #
15
- # ==== Attributes
16
- # * +status+ - hash to convert to an array
17
- # * +flatten_keys+ - an array of keys to bring to the top level
18
- # (with this key prepened) -- default (blank does nothing)
19
- # * +reject_keys+ - an array of keys to remove from the data -- default (blank does nothing)
20
- # * +student_keys+ - [:id, :name] - include student record keys
21
- # * +guardian_info+ - include guardian record info {count: 2, keys: [:id, :name]}
22
- # * +payment_info+ - include payment info {count: 2, order: :newest, keys: [:date, :amount]}
23
- # * guardian & payment info options:
24
- # count: 2 -- how many parent or payment records to return)
25
- # keys: [:id, :date] -- an array of keys of data to return
26
- # order: :newest -- the order to return payments :newest (most recent first - default) or :oldest
27
- def students_as_xlsx_by_status( status,
28
- flatten_keys=[], reject_keys=[],
29
- student_keys=[],
30
- guardian_info={}, payment_info={})
31
- #
32
- check = check_details_keys_validity(flatten_keys, reject_keys)
33
- return check unless check.nil? # or check[:error].nil?
34
- # check = check_header_keys_validity(student_keys, guardian_info, payment_info)
35
- # return check unless check.nil?
36
- #
37
- students_array = students_as_array_by_status( status,
38
- flatten_keys, reject_keys,
39
- student_keys, guardian_info, payment_info )
40
- #
41
- return students_array if students_array.is_a? Hash
42
- #
43
- students_xlsx = students_array_to_xlsx( students_array )
44
-
45
- # example how to save the xlsx object as a file
46
- # students_xlsx.serialize("spec/data/xlsx/students_from_oa.xlsx")
47
-
48
- return students_xlsx
49
- end
50
- alias_method :students_as_xlsx_by_statuses, :students_as_xlsx_by_status
51
-
52
-
53
- # Given an array convert to XLSX Object (Axlsx::Package)
54
- #
55
- # ==== Attributes
56
- # +array+ - expects a hash of students_details (should be flattened to use custom fields)
57
- def students_array_to_xlsx(student_array)
58
- xlsx_obj = Axlsx::Package.new do |p|
59
- p.workbook.add_worksheet(:name => "Students from OpenApply") do |sheet|
60
- student_array.each{ |r| sheet.add_row r }
61
- end
62
- # to save the xlsx object as a file
63
- # p.serialize("spec/data/xlsx/students_from_oa.xlsx")
64
- end
65
- return xlsx_obj
66
- end
67
-
68
- def send_xlsx_to_remote_server( data, srv_hostname, srv_username,
69
- srv_path_file, srv_file_permissions="0750",
70
- ssl_options={}
71
- )
72
- return false unless data.is_a? Axlsx::Package
73
- xfer = data.to_stream() if data.is_a? Axlsx::Package
74
- send_data_to_remote_server( xfer, srv_hostname, srv_username,
75
- srv_path_file, srv_file_permissions,
76
- ssl_options
77
- )
78
- end
79
-
80
- end