class Stellar::Gradebook::StudentList

Collection of students in a course's Gradebook module.

Attributes

client[R]

Generic Stellar client used to make requests.

gradebook[R]

The course's

Public Class Methods

new(gradebook) click to toggle source

Creates a list of students in a class' Gradebook.

@param [Stellar::Gradebook] gradebook client scoped to a course's Gradebook

# File lib/stellar/gradebook.rb, line 190
def initialize(gradebook)
  @gradebook = gradebook
  @client = gradebook.client
  
  @url = gradebook.navigation['Students']
  reload!
end

Public Instance Methods

all() click to toggle source

All students listed in this course's Gradebook module. @return [Array<Stellar::Gradebook::Student>] list of students in this course

# File lib/stellar/gradebook.rb, line 200
def all
  @students
end
named(name) click to toggle source

A student in the course's Gradebook module. @param [String] name the name of the desired student @return [Stellar::Gradebook::Student] a student with the given name, or nil

if no such student exists
# File lib/stellar/gradebook.rb, line 208
def named(name)
  @students.find { |a| a.name == name }
end
reload!() click to toggle source

Reloads the contents of this student list.

@return [Stellar::Gradebook::StudentList] self, for easy call chaining

# File lib/stellar/gradebook.rb, line 223
def reload!
  student_page = @client.get_nokogiri @url
  
  data_script = student_page.css('script').find do |script|
    /var rows\s*\=.*\;/m =~ script.inner_text
  end
  data = JSON.parse((/var rows\s*\=([^;]*)\;/m).
      match(data_script.inner_text)[1].gsub("'", '"'))
  
  @students = data.map { |student_line|
    email = student_line[0]
    url = URI.join @url.to_s, student_line[1]
    name = student_line[2].split(',', 2).map(&:strip).reverse.join(' ')
    
    begin
      Stellar::Gradebook::Student.new url, email, name, gradebook
    rescue ArgumentError
      nil
    end
  }.reject(&:nil?)
  
  self
end
with_email(email) click to toggle source

A student in the course's Gradebook module. @param [String] email the e-mail of the desired student @return [Stellar::Gradebook::Student] a student with the given e-mail

address, or nil if no such student exists
# File lib/stellar/gradebook.rb, line 216
def with_email(email)
  @students.find { |a| a.email == email }
end