Class: Stellar::Gradebook::StudentList

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

Overview

Collection of students in a course’s Gradebook module.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gradebook) ⇒ StudentList

Creates a list of students in a class’ Gradebook.

Parameters:



190
191
192
193
194
195
196
# File 'lib/stellar/gradebook.rb', line 190

def initialize(gradebook)
  @gradebook = gradebook
  @client = gradebook.client
  
  @url = gradebook.navigation['Students']
  reload!
end

Instance Attribute Details

#clientObject (readonly)

Generic Stellar client used to make requests.



185
186
187
# File 'lib/stellar/gradebook.rb', line 185

def client
  @client
end

#gradebookObject (readonly)

The course’s



182
183
184
# File 'lib/stellar/gradebook.rb', line 182

def gradebook
  @gradebook
end

Instance Method Details

#allArray<Stellar::Gradebook::Student>

All students listed in this course’s Gradebook module.

Returns:



200
201
202
# File 'lib/stellar/gradebook.rb', line 200

def all
  @students
end

#named(name) ⇒ Stellar::Gradebook::Student

A student in the course’s Gradebook module.

Parameters:

  • name (String)

    the name of the desired student

Returns:



208
209
210
# File 'lib/stellar/gradebook.rb', line 208

def named(name)
  @students.find { |a| a.name == name }
end

#reload!Stellar::Gradebook::StudentList

Reloads the contents of this student list.

Returns:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# 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) ⇒ Stellar::Gradebook::Student

A student in the course’s Gradebook module.

Parameters:

  • email (String)

    the e-mail of the desired student

Returns:



216
217
218
# File 'lib/stellar/gradebook.rb', line 216

def with_email(email)
  @students.find { |a| a.email == email }
end