openapply 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,125 +1,21 @@
1
1
  module Get
2
2
 
3
- # Record details for ONE student - this API return has the parent info 2x!
3
+ # SINGLE STUDENT CODE
4
+ #####################
5
+
6
+
7
+ # STUDENT FULL RECORD
8
+ #####################
9
+
10
+ # Summary record for ONE student - this API return has the parent info 2x!
4
11
  #
5
- # @param
6
12
  # ==== Attributes
7
- # * +student_id+ - openapply student_id
8
- # * +options+ - see httparty options
13
+ # # @student_id - openapply student_id
14
+ # * @options - see httparty options
9
15
  #
10
16
  # ==== Example code
11
17
  # @demo = Openapply.new
12
18
  # @demo.student_by_id(96)
13
- #
14
- # ==== Result
15
- # {:student=>
16
- # {:id=>96,
17
- # :serial_number=>86,
18
- # :custom_id=>"86",
19
- # :applicant_id=>"00000086",
20
- # :email=>"Jerry.Patel@eduvo.com",
21
- # :first_name=>"Jerry",
22
- # :last_name=>"Patel",
23
- # ...,
24
- # :profile_photo=>
25
- # "https://openapply-sandbox-devel-01.s3.amazonaws.com/uploads/student/avatar/000/000/096/m_9.jpg?v=1499755604",
26
- # :profile_photo_updated_at=>"2017-07-11T14:46:44.000+08:00",
27
- # :custom_fields=>
28
- # {:language=>"Hindi",
29
- # :nationality=>"Indian (India)",
30
- # :referral_source=>"Friends",
31
- # :middle_name_s=>"Danesh",
32
- # :mobile_phone=>"852 4545 1195",
33
- # ...,
34
- # :school_file=>
35
- # {:id=>73,
36
- # :filename=>"Robert_Patel_Report.pdf",
37
- # :url=>"https://demo.openapply.com/api/v1/secure_download/files/73"},
38
- # :has_your_child_attended_school_regularly=>"Yes",
39
- # ...,
40
- # :parent_guardian=>
41
- # [{:id=>268,
42
- # :serial_number=>257,
43
- # :custom_id=>"257",
44
- # :name=>"James Patel",
45
- # :first_name=>"James",
46
- # :last_name=>"Patel",
47
- # ...,
48
- # :custom_fields=>
49
- # {:title=>"CFO",
50
- # :treat_parent_as_emergency_contact=>"Yes",
51
- # :home_telephone=>"",
52
- # ...,
53
- # :parent_residency=>"Citizen"}},
54
- # {:id=>267,
55
- # :serial_number=>256,
56
- # :custom_id=>"256",
57
- # ...,
58
- # :profile_photo=>
59
- # "https://openapply-sandbox-devel-01.s3.amazonaws.com/uploads/parent/avatar/000/000/267/patel_mom.jpg?v=1499755607",
60
- # :profile_photo_updated_at=>"2017-07-11T14:46:47.000+08:00",
61
- # :parent_id=>"256",
62
- # :custom_fields=>
63
- # {:title=>"",
64
- # :treat_parent_as_emergency_contact=>"Yes",
65
- # ...,
66
- # :parent_prior_countries_list=>nil,
67
- # :parent_residency=>"Citizen"}}],
68
- # :siblings_information=>
69
- # [{:first_name=>"Deidre",
70
- # :last_name=>"Patel",
71
- # :gender=>"Female",
72
- # :birth_date=>"2008-06-06"}],
73
- # :emergency_contact=>[],
74
- # :prior_school_list=>
75
- # [{:school_name=>"Michael Varner Elementary",
76
- # :school_country=>"United States",
77
- # :school_contact_name=>"Ann Farris",
78
- # :school_contact_phone=>"(415) 777-0876"},
79
- # {:school_name=>"ISS International School",
80
- # :school_contact_name=>"Taylor Michaels",
81
- # :school_contact_phone=>"65 6235 5844",
82
- # :school_country=>"Singapore"}],
83
- # :immunization_record=>[],
84
- # :health_information=>[]},
85
- # :parent_ids=>[268, 267]},
86
- # :linked=>
87
- # {:parents=>
88
- # [{:id=>268,
89
- # :serial_number=>257,
90
- # :custom_id=>"257",
91
- # :name=>"James Patel",
92
- # :first_name=>"James",
93
- # :last_name=>"Patel",
94
- # ...,
95
- # :address=>"1641 26th Avenue",
96
- # :address_ii=>"",
97
- # :city=>"San Francisco",
98
- # :state=>"California",
99
- # :postal_code=>"94122",
100
- # :country=>"United States",
101
- # ...,
102
- # :custom_fields=>
103
- # {:title=>"CFO",
104
- # :treat_parent_as_emergency_contact=>"Yes",
105
- # :home_telephone=>"",
106
- # ...,
107
- # :parent_residency=>"Citizen"}},
108
- # {:id=>267,
109
- # :serial_number=>256,
110
- # :custom_id=>"256",
111
- # :name=>"Jane Patel",
112
- # :first_name=>"Jane",
113
- # ...,
114
- # :profile_photo=>
115
- # "https://openapply-sandbox-devel-01.s3.amazonaws.com/uploads/parent/avatar/000/000/267/patel_mom.jpg?v=1499755607",
116
- # :profile_photo_updated_at=>"2017-07-11T14:46:47.000+08:00",
117
- # :parent_id=>"256",
118
- # :custom_fields=>
119
- # {:title=>"",
120
- # :treat_parent_as_emergency_contact=>"Yes",
121
- # ...,
122
- # :parent_residency=>"Citizen"}}]}}
123
19
  def student_by_id(student_id, options ={})
124
20
  url = "#{api_path}#{student_id}?auth_token=#{api_key}"
125
21
  return oa_answer( url, options )
@@ -127,6 +23,9 @@ module Get
127
23
  alias_method :student, :student_by_id
128
24
 
129
25
 
26
+ # STUDENT PAYMENT INFO
27
+ ######################
28
+
130
29
  # Payment details for ONE student
131
30
  #
132
31
  # ==== Attributes
@@ -136,17 +35,6 @@ module Get
136
35
  # ==== Example code
137
36
  # @demo = Openapply.new
138
37
  # @demo.payments_by_id(96)
139
- #
140
- # ==== Result
141
- # {:payments=>
142
- # [{:invoice_status=>"Paid",
143
- # :type=>"Application",
144
- # :invoice_number=>1047,
145
- # :amount=>"90.0",
146
- # :issue_date=>"2016-03-11",
147
- # :due_date=>"2016-03-31",
148
- # :payment_method=>"check",
149
- # :payment_date=>"2013-03-07"}]}
150
38
  def payments_by_id(student_id, options={})
151
39
  url = "#{api_path}#{student_id}/payments?auth_token=#{api_key}"
152
40
  return oa_answer( url, options )
@@ -154,119 +42,73 @@ module Get
154
42
  alias_method :payments, :payments_by_id
155
43
 
156
44
 
45
+ # STUDENT FULL RECORD AND PAYMENTS COMBINED
46
+ ###########################################
47
+
157
48
  # Combines the student_by_id & payments_by_id into one call with all the data
158
49
  #
159
50
  # ==== Attributes
160
51
  # * +student_id+ - openapply student_id
52
+ # * +flatten_keys+ - an array of keys to bring to the top level
53
+ # (with this key prepened) -- default (blank does nothing)
54
+ # * +reject_keys+ - an array of keys to remove from the data -- default (blank does nothing)
161
55
  #
162
- # ==== Example code
163
- # @demo = Openapply.new
164
- # @demo.student_details_by_id(96)
165
- #
166
- # ==== Results
167
- # NOTE: gaurdian info is moved to the top level - along with ID
168
- # {student: {
169
- # id=95,
170
- # record: {},
171
- # guardians: [],
172
- # payments: []
173
- # }
174
- # }
175
- #
176
- # for example:
177
- #
178
- # {:student=>
179
- # {:id=>96,
180
- # :record=>
181
- # {:id=>96,
182
- # :serial_number=>86,
183
- # :custom_id=>"86",
184
- # :applicant_id=>"00000086",
185
- # :email=>"Jerry.Patel@eduvo.com",
186
- # :first_name=>"Jerry",
187
- # :last_name=>"Patel",
188
- # ...,
189
- # :profile_photo=>
190
- # "https://openapply-sandbox-devel-01.s3.amazonaws.com/uploads/student/avatar/000/000/096/m_9.jpg?v=1499755604",
191
- # :profile_photo_updated_at=>"2017-07-11T14:46:44.000+08:00",
192
- # :custom_fields=>
193
- # {:language=>"Hindi",
194
- # :nationality=>"Indian (India)",
195
- # :referral_source=>"Friends",
196
- # ...,
197
- # :siblings_information=>
198
- # [{:first_name=>"Deidre",
199
- # :last_name=>"Patel",
200
- # :gender=>"Female",
201
- # :birth_date=>"2008-06-06"}],
202
- # :emergency_contact=>[],
203
- # :immunization_record=>[],
204
- # :health_information=>[]},
205
- # :parent_ids=>[268, 267]},
206
- # :payments=>
207
- # [{:invoice_status=>"Paid",
208
- # :type=>"Application",
209
- # :invoice_number=>1047,
210
- # :amount=>"90.0",
211
- # :issue_date=>"2016-03-11",
212
- # :due_date=>"2016-03-31",
213
- # :payment_method=>"check",
214
- # :payment_date=>"2013-03-07"}],
215
- # :guardians=>
216
- # [{:id=>268,
217
- # :serial_number=>257,
218
- # :custom_id=>"257",
219
- # :name=>"James Patel",
220
- # :first_name=>"James",
221
- # :last_name=>"Patel",
222
- # ...,
223
- # :custom_fields=>
224
- # {:title=>"CFO",
225
- # :treat_parent_as_emergency_contact=>"Yes",
226
- # :home_telephone=>"",
227
- # ...,
228
- # :parent_residency=>"Citizen"}},
229
- # {:id=>267,
230
- # :serial_number=>256,
231
- # :custom_id=>"256",
232
- # :name=>"Jane Patel",
233
- # :first_name=>"Jane",
234
- # :last_name=>"Patel",
235
- # ...,
236
- # :profile_photo=>
237
- # "https://openapply-sandbox-devel-01.s3.amazonaws.com/uploads/parent/avatar/000/000/267/patel_mom.jpg?v=1499755607",
238
- # :profile_photo_updated_at=>"2017-07-11T14:46:47.000+08:00",
239
- # :parent_id=>"256",
240
- # :custom_fields=>
241
- # {:title=>"",
242
- # :treat_parent_as_emergency_contact=>"Yes",
243
- # :home_telephone=>"",
244
- # ...,
245
- # :parent_residency=>"Citizen"}}]}}
246
- def student_details_by_id(id, flatten = false)
56
+ # === Returned Data
57
+ # returns the data structured as:
58
+ # { student:
59
+ # { id: xxx,
60
+ # record: {xxx} # complete student record
61
+ # guardians: [ {} ] # all guardian information
62
+ # payments: [ {} ] # all payments made via openapply
63
+ # }
64
+ # }
65
+ def student_details_by_id(id, flatten_keys=[], reject_keys=[])
66
+
67
+ check = check_details_keys_validity(flatten_keys, reject_keys)
68
+ return check unless check.nil? # or check[:error].nil?
69
+
70
+ # get full student record and guardian information
247
71
  student_info = student_by_id( "#{id}" )
72
+ # get student payment records
248
73
  payment_info = payments_by_id( "#{id}" )
249
74
 
250
- # be sure there is data to process
251
- return {student: {id: id, empty: []}} if student_info.nil? or
252
- student_info[:student].nil?
253
- student_info[:student].empty?
75
+ # be sure there is student data to process -- if not return an empty record
76
+ return {student: {id: id, empty: []}} if student_info.nil? or
77
+ student_info[:student].nil?
78
+ student_info[:student].empty?
79
+ student = []
254
80
  guardians = []
81
+ # extract guardian information
255
82
  guardians = student_info[:linked][:parents].dup unless
256
- student_info[:linked].nil? or
257
- student_info[:linked].empty? or
258
- student_info[:linked][:parents].nil?
83
+ student_info[:linked].nil? or
84
+ student_info[:linked].empty? or
85
+ student_info[:linked][:parents].nil?
259
86
  payments = []
87
+ # extract payment information
260
88
  payments = payment_info[:payments].dup unless payment_info.nil? or
261
- payment_info[:payments].nil?
262
- # process meaningful data
263
- record = student_info[:student].dup
264
- # remove duplicated parental data fields
265
- record[:custom_fields][:parent_guardian] = nil
89
+ payment_info[:payments].nil?
90
+ # if flatten and reject are not set - set them so they do nothing
91
+ flatten_keys = [:flatten_no_keys] if flatten_keys.nil? or flatten_keys.empty?
92
+ reject_keys = [:reject_no_keys] if reject_keys.nil? or reject_keys.empty?
266
93
 
94
+ # extract the student info into the desired format (and removes \n in strings)
95
+ # student = student_info[:student] - this is the effective result wo
96
+ # flatten/reject keys
97
+ student = flatten_record(student_info[:student], flatten_keys, reject_keys)
98
+
99
+ # process guardian records - the same way student records
100
+ # (loop on guardian arrary since there can be multiple parents)
101
+ g_flat = []
102
+ guardians = guardians.each do |guard|
103
+ next if guard.empty?
104
+ g_flat << flatten_record( guard, flatten_keys, reject_keys )
105
+ end unless guardians.empty?
106
+ guardians = g_flat unless g_flat.empty?
107
+
108
+ # organize the student details
267
109
  return { student:
268
110
  { id: id,
269
- record: record,
111
+ record: student,
270
112
  payments: payments,
271
113
  guardians: guardians,
272
114
  }
@@ -274,4 +116,64 @@ module Get
274
116
  end
275
117
  alias_method :student_details, :student_details_by_id
276
118
 
119
+ # UTILITIES
120
+ ###########
121
+
122
+ # return value & remove linebreaks & trim spaces (if a string)
123
+ def clean_data( value )
124
+ return value.gsub("\n",' ').strip if value.is_a? String
125
+ return value
126
+ end
127
+
128
+ # This method preprocesses records - brings keys to the top level and
129
+ # removes fields and removes \n from strings
130
+ #
131
+ # === Attributes
132
+ # * +hash+ -- **ONE students** student_details
133
+ # * +flatten_keys+ -- keys to bring to the top level (with top key prepended)
134
+ # * +reject_keys+ -- remove data matching these keys
135
+ # TODO: add recursion?
136
+ def flatten_record(hash, flatten_keys=[:flatten_no_keys],reject_keys=[:reject_no_keys])
137
+ answer = {}
138
+
139
+ # loop through each key value of the student record
140
+ hash.each do |key,val|
141
+ # skip loop if this key matches a value to remove
142
+ next if reject_keys.include? key
143
+
144
+ # put data back into hash if not to be flattened
145
+ answer[key] = clean_data(val) unless flatten_keys.include? key
146
+
147
+ # if current key matches a flatten value & HAS nested data values
148
+ # then bring nested data to top level
149
+ val.each do |k,v|
150
+ # remove any nested values if they match a reject_key
151
+ next if reject_keys.include? k
152
+
153
+ # (prepend flatten_key_to_current_key to prevent conflicts)
154
+ new_key = "#{key.to_s}_#{k.to_s}".to_sym
155
+ # clean the data and add back to to top level with a new key
156
+ answer[new_key] = clean_data(v)
157
+ end if flatten_keys.include?(key) and
158
+ not val.empty?
159
+ end
160
+ return answer
161
+ end
162
+
163
+ # Check the validity of keys to process student_details
164
+ #
165
+ # === Attributes
166
+ # * +flatten_keys+ - is an array of symbols
167
+ # * +reject_keys+ - is an array of symbols
168
+ def check_details_keys_validity(flatten_keys, reject_keys)
169
+ # # be sure flatten_keys are in an array
170
+ return {error: "invalid flatten_keys - need array"} unless flatten_keys.is_a? Array
171
+ # # be sure reject_keys are in an array
172
+ return {error: "invalid reject_keys - need array"} unless reject_keys.is_a? Array
173
+ # # test if any values are non-symbols (remain after removing symbols)
174
+ return {error: "invalid flatten_keys - use symbols"} if flatten_keys.reject{|k| k.is_a? Symbol}.count > 0
175
+ # # test if any values are non-symbols (remain after removing symbols)
176
+ return {error: "invalid reject_keys - use symbols"} if reject_keys.reject{|k| k.is_a? Symbol}.count > 0
177
+ end
178
+
277
179
  end