class Manabu::Students

Handles the student index for the given client

Public Class Methods

new(client) click to toggle source

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)

# File lib/manabu/students.rb, line 13
def initialize(client)
  @client = client
  @students = []
end

Public Instance Methods

delete(student) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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
# 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

Private Instance Methods

_fetch_students() click to toggle source
# File lib/manabu/students.rb, line 88
def _fetch_students()
  # TODO raise error if @client is nil
  response = @client.get('students')
  response[:students].map do |student|
    Manabu::Student.new(@client, student)
  end
end
students() click to toggle source
# File lib/manabu/students.rb, line 76
def students()
  if @students.any?
    @students
  else
    @students = _fetch_students
  end
end
whitelist_filter_attributes() click to toggle source
# File lib/manabu/students.rb, line 84
def whitelist_filter_attributes
  [:id, :name, :surname]
end