Class: Stellar::Homework::Submission

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

Overview

A student’s submission for an assignment.

Defined Under Namespace

Classes: Comment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tr, homework) ⇒ Submission

Creates a submission from a <tr> element in the Stellar homework page.

Parameters:

  • tr (Nokogiri::XML::Element)

    a <tr> element in the Stellar homework page describing this submission

  • homework (Stellar::Homework)

    Stellar client scoped to the assignment that this submission is for

Raises:

  • (ArgumentError)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/stellar/homework.rb', line 110

def initialize(tr, homework)
  link = tr.css('a').find do |link|
    (/^\s*\d+\s*$/ =~ link.inner_text) && !(/grade/ =~ link['href'])
  end
  raise ArgumentError, 'Invalid submission-listing <tr>' unless link

  @url = URI.join tr.document.url, link['href']
  @homework = homework
  @client = homework.client
  
  page = @client.get_nokogiri @url

  unless author_link = page.css('#content h4 a[href^="mailto:"]').first
    raise ArgumentError, 'Invalid submission-listing <tr>'
  end
  @name = author_link.inner_text
  @email = author_link['href'].sub /^mailto:/, ''
  @file_url = page.css('#rosterBox a[href*="studentWork"]').map { |link|
    next nil unless link.inner_text == homework.name
    URI.join @url.to_s, link['href']
  }.reject(&:nil?).first
  @time = page.css('#rosterBox .instruction').map { |span|
    unless span.css('strong').any? { |strong| /date/i =~ strong.inner_text }
      next nil
    end
    time_string = span.inner_text.split(':', 2).last.strip
    time = DateTime.parse(time_string + ' GMT-4').to_time
  }.reject(&:nil?).first
  
  @add_comment_url = URI.join @url.to_s,
      page.css('#comments a[href*="add"]').first['href']
  reload_comments! page
end

Instance Attribute Details

#clientObject (readonly)

Generic Stellar client used to make requests.



102
103
104
# File 'lib/stellar/homework.rb', line 102

def client
  @client
end

#commentsObject (readonly)

Comments posted on this submission.



96
97
98
# File 'lib/stellar/homework.rb', line 96

def comments
  @comments
end

#emailObject (readonly)

Email of the student who authored this submission.



93
94
95
# File 'lib/stellar/homework.rb', line 93

def email
  @email
end

#file_urlObject (readonly)

URL to the last file that the student submitted.



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

def file_url
  @file_url
end

#homeworkObject (readonly)

Homework that the submission belongs to.



99
100
101
# File 'lib/stellar/homework.rb', line 99

def homework
  @homework
end

#nameObject (readonly)

Name of the student who authored this submission.



90
91
92
# File 'lib/stellar/homework.rb', line 90

def name
  @name
end

#timeObject (readonly)

Submission time.



87
88
89
# File 'lib/stellar/homework.rb', line 87

def time
  @time
end

Instance Method Details

#add_comment(text, file_data = nil, file_mime_type = 'text/plain', file_name = 'attachment.txt') ⇒ Stellar::Homework::Submission

Adds a comment to the student’s submission.

Parameters:

  • text (String)

    the comment text

  • file_data (String) (defaults to: nil)

    the content of the file attached to the comment; by default, no file is attached

  • file_mime_type (String) (defaults to: 'text/plain')

    if a file is attached, indicates its type; examples: ‘text/plain’, ‘application/pdf’

  • file_name (String) (defaults to: 'attachment.txt')

    name of the file attached to the comment; by default, ‘attachment.txt’

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/stellar/homework.rb', line 161

def add_comment(text, file_data = nil, file_mime_type = 'text/plain',
                file_name = 'attachment.txt')
  add_page = @client.get @add_comment_url
  add_form = add_page.form_with :action => /addcomment/i
  
  add_form.field_with(:name => /newCommentRaw/i).value = text
  add_form.field_with(:name => /newComment/i).value = text
  add_form.checkbox_with(:name => /privateComment/i).checked = :checked
  if file_data
    upload = add_form.file_uploads.first
    upload.file_name = file_name
    upload.mime_type = file_mime_type
    upload.file_data = file_data
  end
  add_form.submit add_form.button_with(:name => /submit/i)
  self
end

#file_dataString

The contents of the file attached to this Stellar submission.

Returns:

  • (String)

    raw file data



147
148
149
# File 'lib/stellar/homework.rb', line 147

def file_data
  @client.get_file @file_url
end

#reload_comments!(page = nil) ⇒ Object

Reloads the problem set’s comments page.



180
181
182
183
184
185
# File 'lib/stellar/homework.rb', line 180

def reload_comments!(page = nil)
  page ||= @client.get_nokogiri @url
  @comments = page.css('#comments ~ table.dataTable').map { |table|
    Comment.new table, self
  }.reject(&:nil?)
end