Class: Manabu::Students

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

Overview

Handles the student index for the given client

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Students

Initializes against the passed client instance. If the client instance does not contain a client with a valid authorization all methods will return nil.

Parameters:

client

A Manabu::Client instance (with a valid authorization)



13
14
15
16
# File 'lib/manabu/students.rb', line 13

def initialize(client)
  @client = client
  @students = []
end

Instance Method Details

#delete(student) ⇒ Object



67
68
69
70
71
72
# File 'lib/manabu/students.rb', line 67

def delete(student)
  @client.delete("students/#{student.id}")
  @students.reject! { |object| object.id == student.id }
  # NOTE: check in response when implement error object
  true
end

#filter(attrs = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/manabu/students.rb', line 37

def filter(attrs = {})
  result = students.dup
  if attrs.has_key?(:enrollment_status)
    result.select! { |student| student.enrollment_status&.code == attrs[:enrollment_status]  }
  end
  result
end

#register(student) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/manabu/students.rb', line 45

def register(student)
  new_student = case student
  when Manabu::Student
    register_student_by_object(student)
  when Hash
    register_student_by_hash(student)
  end
  new_student.tap { |object| @students << object }
end

#register_student_by_hash(student) ⇒ Object



61
62
63
64
65
# File 'lib/manabu/students.rb', line 61

def register_student_by_hash(student)
  res = @client.post('students', student)
  # TODO: handle errors
  Manabu::Student.new(@client, res)
end

#register_student_by_object(student) ⇒ Object



55
56
57
58
59
# File 'lib/manabu/students.rb', line 55

def register_student_by_object(student)
  res = @client.post('students', student.to_hash)
  # TODO: handle errors
  student.fill(res)
end

#roster(**filters) ⇒ Object

Returns a roster of all students which the client user has access to.

Parameters:

filters:

A hash of filters to narrow down results. Available filters include:
* enrollment_status - [] TODO fill in enrollment statuses


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/manabu/students.rb', line 24

def roster(**filters)
   # TODO: handle errors
   # TODO: handle filters in API endpoint
 # NOTE: cache results
  return students if filters.empty?

  students.select do |student|
    filters.slice(*whitelist_filter_attributes).all? do |filter, value|
      student.send(filter) == value
    end
  end
end