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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/stellar/homework.rb', line 201

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.



184
185
186
# File 'lib/stellar/homework.rb', line 184

def attachment_url
  @attachment_url
end

#authorObject (readonly)

Person who posted the comment.



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

def author
  @author
end

#clientObject (readonly)

Generic Stellar client used to make requests.



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

def client
  @client
end

#deletedObject (readonly) Also known as: deleted?

True if the comment was deleted.



186
187
188
# File 'lib/stellar/homework.rb', line 186

def deleted
  @deleted
end

#submissionObject (readonly)

The submission that the comment was posted on.



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

def submission
  @submission
end

#textObject (readonly)

Comment text.



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

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



246
247
248
# File 'lib/stellar/homework.rb', line 246

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

#delete!Object

Deletes this comment from Stellar.



234
235
236
237
238
239
240
241
# File 'lib/stellar/homework.rb', line 234

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