Class: UntisWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/untis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ UntisWorker

Returns a new instance of UntisWorker.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/untis.rb', line 13

def initialize(**args)
  @login            = args[:login]
  @password         = args[:password]
  @client_id        = args[:client_id]    || self.generate_client_id
  @session_id       = args[:session_id]
  @person_id        = args[:person_id]
  @person_type      = args[:person_type]
  @api_base_url     = args[:api_base_url] || "https://kephiso.webuntis.com/WebUntis"
  @school_name      = args[:school_name]  || "Example School"
  @school_name_url  = URI::escape @school_name.gsub(/\s/, '+')
  @api_url_base     = URI "#{@api_base_url}/jsonrpc.do"
  @api_url          = URI "#{@api_base_url}/jsonrpc.do?school=#{@school_name_url}"
  @api_version      = '2.0'
  @api_version_sv   = @api_version
end

Instance Attribute Details

#api_base_urlObject

Returns the value of attribute api_base_url.



7
8
9
# File 'lib/untis.rb', line 7

def api_base_url
  @api_base_url
end

#api_urlObject

Returns the value of attribute api_url.



7
8
9
# File 'lib/untis.rb', line 7

def api_url
  @api_url
end

#api_url_baseObject

Returns the value of attribute api_url_base.



7
8
9
# File 'lib/untis.rb', line 7

def api_url_base
  @api_url_base
end

#api_versionObject

Returns the value of attribute api_version.



7
8
9
# File 'lib/untis.rb', line 7

def api_version
  @api_version
end

#api_version_svObject

Returns the value of attribute api_version_sv.



7
8
9
# File 'lib/untis.rb', line 7

def api_version_sv
  @api_version_sv
end

#client_idObject

Returns the value of attribute client_id.



7
8
9
# File 'lib/untis.rb', line 7

def client_id
  @client_id
end

#loginObject

Returns the value of attribute login.



7
8
9
# File 'lib/untis.rb', line 7

def 
  @login
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/untis.rb', line 7

def password
  @password
end

#person_idObject

Returns the value of attribute person_id.



7
8
9
# File 'lib/untis.rb', line 7

def person_id
  @person_id
end

#person_typeObject

Returns the value of attribute person_type.



7
8
9
# File 'lib/untis.rb', line 7

def person_type
  @person_type
end

#school_nameObject

Returns the value of attribute school_name.



7
8
9
# File 'lib/untis.rb', line 7

def school_name
  @school_name
end

#school_name_urlObject

Returns the value of attribute school_name_url.



7
8
9
# File 'lib/untis.rb', line 7

def school_name_url
  @school_name_url
end

#session_idObject

Returns the value of attribute session_id.



7
8
9
# File 'lib/untis.rb', line 7

def session_id
  @session_id
end

Instance Method Details

#authenticate!Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/untis.rb', line 96

def authenticate!
  result = post @api_url, build_fields(
    method: 'authenticate',
    params: {
      'user'      => @login,
      'password'  => @password,
      'client'    => @client_id
    }
  )

  if result['result']
    puts 'Auth OK!'

    @session_id     = result['result']['sessionId']
    @person_type    = result['result']['personType']
    @person_id      = result['result']['personId']
    @api_version_sv = result['jsonrpc']
  else
    puts 'Auth error!'
    puts result['error']['message']
  end

  result
end

#date_from_str(date_str) ⇒ Object



80
81
82
# File 'lib/untis.rb', line 80

def date_from_str(date_str)
  Date::strptime date_str, '%Y%m%d'
end

#date_to_str(date) ⇒ Object



84
85
86
# File 'lib/untis.rb', line 84

def date_to_str(date)
  date.strftime '%Y%m%d'
end

#generate_client_idObject



29
30
31
# File 'lib/untis.rb', line 29

def generate_client_id
  "UW-#{SecureRandom.hex}"
end

#generate_idObject



33
34
35
# File 'lib/untis.rb', line 33

def generate_id
  SecureRandom.hex
end

#get_class_reg_categoriesObject



313
314
315
316
317
318
# File 'lib/untis.rb', line 313

def get_class_reg_categories
  post @api_url, build_fields(
    method: 'getClassregCategories',
    params: []
  )
end

#get_class_reg_category_groupsObject



320
321
322
323
324
325
# File 'lib/untis.rb', line 320

def get_class_reg_category_groups
  post @api_url, build_fields(
    method: 'getClassregCategoryGroups',
    params: []
  )
end

#get_class_reg_events(**args) ⇒ Object



271
272
273
274
275
276
277
278
279
# File 'lib/untis.rb', line 271

def get_class_reg_events(**args)
  post @api_url, build_fields(
    method: 'getClassregEvents',
    params: {
      endDate:    args[:end_date]     || args[:endDate],
      startDate:  args[:start_date]   || args[:startDate],
    }
  )
end

#get_class_reg_events_for_element(**args) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/untis.rb', line 327

def get_class_reg_events_for_element(**args)
  opts = args[:options] || args

  opts[:element] = {} if opts[:element].nil?

  post @api_url, build_fields(
    method: 'getClassregEvents',
    params: {
      options: {
        endDate:    opts[:end_date]           || opts[:endDate],
        startDate:  opts[:start_date]         || opts[:startDate],
        element: {
          id:       opts[:element][:id],
          type:     opts[:element][:type],
          keyType:  opts[:element][:keyType]  || opts[:element][:key_type] || 'id',
        }
      }
    }
  )
end

#get_classes(**args) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/untis.rb', line 135

def get_classes(**args)
  post @api_url, build_fields(
    method: 'getKlassen',
    params: {
      schoolyearId: args[:school_year] || args[:year] || args[:schoolyearId] || args[:school_year_id]
    }
  )
end

#get_current_school_yearObject



186
187
188
189
190
191
# File 'lib/untis.rb', line 186

def get_current_school_year
  post @api_url, build_fields(
    method: 'getCurrentSchoolyear',
    params: []
  )
end

#get_departmentsObject



158
159
160
161
162
163
# File 'lib/untis.rb', line 158

def get_departments
  post @api_url, build_fields(
    method: 'getDepartments',
    params: []
  )
end

#get_exam_typesObject



292
293
294
295
296
297
# File 'lib/untis.rb', line 292

def get_exam_types
  post @api_url, build_fields(
    method: 'getExamTypes',
    params: []
  )
end

#get_exams(**args) ⇒ Object



281
282
283
284
285
286
287
288
289
290
# File 'lib/untis.rb', line 281

def get_exams(**args)
  post @api_url, build_fields(
    method: 'getExams',
    params: {
      endDate:    args[:end_date]     || args[:endDate],
      startDate:  args[:start_date]   || args[:startDate],
      examTypeId: args[:exam_type_id] || args[:exam_type] || args[:examTypeId] || args[:exam_id]
    }
  )
end

#get_holidaysObject



165
166
167
168
169
170
# File 'lib/untis.rb', line 165

def get_holidays
  post @api_url, build_fields(
    method: 'getHolidays',
    params: []
  )
end

#get_latest_import_timeObject



241
242
243
244
245
246
# File 'lib/untis.rb', line 241

def get_latest_import_time
  post @api_url, build_fields(
    method: 'getLatestImportTime',
    params: []
  )
end

#get_person_id(**args) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/untis.rb', line 248

def get_person_id(**args)
  post @api_url, build_fields(
    method: 'getPersonId',
    params: {
      type: args[:type] || 5,
      sn:   args[:sn]   || args[:surname]   || args[:last_name],
      fn:   args[:fn]   || args[:forename]  || args[:name]        || args[:first_name],
      dob:  args[:dob]  || args[:birthday]  || args[:birth_date]  || 0,
    }
  )
end

#get_roomsObject



151
152
153
154
155
156
# File 'lib/untis.rb', line 151

def get_rooms
  post @api_url, build_fields(
    method: 'getRooms',
    params: []
  )
end

#get_school_yearsObject



193
194
195
196
197
198
# File 'lib/untis.rb', line 193

def get_school_years
  post @api_url, build_fields(
    method: 'getSchoolyears',
    params: []
  )
end

#get_status_dataObject



179
180
181
182
183
184
# File 'lib/untis.rb', line 179

def get_status_data
  post @api_url, build_fields(
    method: 'getStatusData',
    params: []
  )
end

#get_studentsObject



128
129
130
131
132
133
# File 'lib/untis.rb', line 128

def get_students
  post @api_url, build_fields(
    method: 'getStudents',
    params: []
  )
end

#get_subjectsObject



144
145
146
147
148
149
# File 'lib/untis.rb', line 144

def get_subjects
  post @api_url, build_fields(
    method: 'getSubjects',
    params: []
  )
end

#get_substitutions(**args) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'lib/untis.rb', line 260

def get_substitutions(**args)
  post @api_url, build_fields(
    method: 'getSubstitutions',
    params: {
      endDate:      date_to_str(args[:end_date]   || args[:endDate]),
      startDate:    date_to_str(args[:start_date] || args[:startDate]),
      departmentId: args[:department_id]          || args[:departmentId] || 0,
    }
  )
end

#get_teachersObject



121
122
123
124
125
126
# File 'lib/untis.rb', line 121

def get_teachers
  post @api_url, build_fields(
    method: 'getTeachers',
    params: []
  )
end

#get_timegridObject



172
173
174
175
176
177
# File 'lib/untis.rb', line 172

def get_timegrid
  post @api_url, build_fields(
    method: 'getTimegridUnits',
    params: []
  )
end

#get_timetable(**args) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/untis.rb', line 200

def get_timetable(**args)
  if args[:options].nil?
    post @api_url, build_fields(
      method: 'getTimetable',
      params: {
        id:         args[:id],
        type:       args[:type],
        startDate:  date_to_str(args[:start_date] || args[:startDate]),
        endDate:    date_to_str(args[:end_date]   || args[:endDate])
      }
    )
  else
    opts = args[:options]

    post @api_url, build_fields(
      method: 'getTimetable',
      params: {
        options: {
          element: {
            id:               opts[:element][:id],
            type:             opts[:element][:type],
            keyType:          opts[:element][:key_type]     || opts[:element][:keyType] || 'id'
          },
          startDate:          date_to_str(opts[:start_date] || opts[:startDate]         || Date.today),
          endDate:            date_to_str(opts[:end_date]   || opts[:endDate]           || Date.today),
          onlyBaseTimetable:  opts[:only_base_timetable]    || opts[:onlyBaseTimetable] || false,
          showBooking:        opts[:show_booking]           || opts[:showBooking]       || false,
          showInfo:           opts[:show_info]              || opts[:showInfo]          || false,
          showSubstText:      opts[:show_subst_text]        || opts[:showSubstText]     || false,
          showLsText:         opts[:show_ls_text]           || opts[:showLsText]        || false,
          showStudentgroup:   opts[:show_student_group]     || opts[:showStudentgroup]  || false,
          klasseFields:       opts[:class_fields]           || opts[:klasseFields],
          roomFields:         opts[:room_fields]            || opts[:roomFields],
          subjectFields:      opts[:subject_fields]         || opts[:subjectFields],
          teacherFields:      opts[:teacher_fields]         || opts[:teacherFields]
        }
      }
    )
  end
end

#get_timetable_with_absences(**args) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/untis.rb', line 299

def get_timetable_with_absences(**args)
  opts = args[:options] || args

  post @api_url, build_fields(
    method: 'getTimetableWithAbsences',
    params: {
      options: {
        endDate:    opts[:end_date]   || opts[:endDate],
        startDate:  opts[:start_date] || opts[:startDate]
      }
    }
  )
end

#time_from_str(time_str) ⇒ Object



88
89
90
# File 'lib/untis.rb', line 88

def time_from_str(time_str)
  Time::strptime time_str, '%H%M'
end

#time_to_str(time) ⇒ Object



92
93
94
# File 'lib/untis.rb', line 92

def time_to_str(time)
  time.strftime '%H%M'
end