Class: Stellar::Homework::Submission::Comment

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

Overview

A comment on a Stellar submission.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, submission) ⇒ Comment

Creates a comment from a <table> in a Stellar submission details page.

Parameters:

  • table (Nokogiri::XML::Element)

    a <table> element in a Stellar submission details showing this comment

  • submission (Stellar::Homework::Submission)

    Stellar client scoped to the submission that was commented on



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/stellar/homework.rb', line 211

def initialize(table, submission)
  @submission = submission
  @client = @submission.client

  page_url = table.document.url
  @author = table.css('thead tr th.announcedBy').first.inner_text
  
  unless content = table.css('tbody tr td.announcement').first
    raise 'Invalid submission comment table'
  end
  if (deleted_text = table.css('tbody tr td.announcement > em').first) &&
      deleted_text.inner_text == 'deleted'
    @deleted = true
    @text = nil
    @attachment_url = nil
  else
    @deleted = false
    
    unless delete_link = table.css('thead a[href*="delete"]').first
      raise ArgumentError, 'Invalid submission comment table'
    end
    @delete_url = URI.join page_url, delete_link['href']
    @text = content.css('p').inner_text
    attachment_links = table.css('tbody tr td.announcement > a')
    if attachment_links.empty?
      @attachment_url = nil
    else
      @attachment_url = URI.join page_url, attachment_links.first['href']
    end
  end
end

Instance Attribute Details

#attachment_urlObject (readonly)

URL to the file attached to the comment. Can be nil.



194
195
196
# File 'lib/stellar/homework.rb', line 194

def attachment_url
  @attachment_url
end

#authorObject (readonly)

Person who posted the comment.



190
191
192
# File 'lib/stellar/homework.rb', line 190

def author
  @author
end

#clientObject (readonly)

Generic Stellar client used to make requests.



203
204
205
# File 'lib/stellar/homework.rb', line 203

def client
  @client
end

#deletedObject (readonly) Also known as: deleted?

True if the comment was deleted.



196
197
198
# File 'lib/stellar/homework.rb', line 196

def deleted
  @deleted
end

#submissionObject (readonly)

The submission that the comment was posted on.



201
202
203
# File 'lib/stellar/homework.rb', line 201

def submission
  @submission
end

#textObject (readonly)

Comment text.



192
193
194
# File 'lib/stellar/homework.rb', line 192

def text
  @text
end

Instance Method Details

#attachment_dataString

The contents of the file attached to this Stellar submission comment.

Returns:

  • (String)

    raw file data



256
257
258
# File 'lib/stellar/homework.rb', line 256

def attachment_data
  @attachment_url && @client.get_file(@attachment_url)
end

#delete!Object

Deletes this comment from Stellar.



244
245
246
247
248
249
250
251
# File 'lib/stellar/homework.rb', line 244

def delete!
  return if @deleted
  delete_page = @client.get @delete_url
  delete_form = delete_page.form_with(:action => /delete/i)
  delete_button = delete_form.button_with(:name => /delete/i)
  delete_form.submit delete_button
  @deleted = true
end