Rcov C0 Coverage Information - RCov

app/controllers/api/submission_downloads_controller.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
app/controllers/api/submission_downloads_controller.rb 108 67
23.15%
8.96%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 module Api
2   require 'zip/zip'
3 
4   #=== Description
5   # Allows for downloading of submission files and their annotations
6   # Uses Rails' RESTful routes (check 'rake routes' for the configured routes)
7   class SubmissionDownloadsController < MainApiController
8 
9     #=== Description
10     # Triggered by a HTTP GET request to /api/submission_downloads(.:format)
11     # Downloads a SubmissionFile, possibly with annotations.
12     # Requires the following parameters:
13     #   group_name:   Name of the group that submitten the file
14     #   assignment:   Assignment for which the file was submitted
15     # Allows the following optional paramenters:
16     #   filename:     Name of the file, if absent all files will be downloaded
17     #   include_annotations:  If 'true', will include annotations in the file(s)
18     #=== Returns
19     # The requested file, or a zip file containing all requested files
20     def show
21       if !request.get?
22         # pretend this URL does not exist
23         render :file => "#{::Rails.root.to_s}/public/404.html", :status => 404
24         return
25       end
26       if !has_required_http_params?(params)
27         # incomplete/invalid HTTP params
28         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
29         return
30       end
31 
32       # check if there's a valid submission
33       submission = Submission.get_submission_by_group_and_assignment(params[:group_name],
34                                                                       params[:assignment])
35 
36       if submission.nil?
37         # no such submission
38         render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
39         return
40       end
41 
42       #If requested a single file
43       if !params[:filename].blank?
44         files = [SubmissionFile.find_by_filename_and_submission_id(params[:filename], submission.id)]
45         single_file = true
46       else
47       #otherwise we give them the whole directory of files
48         files = SubmissionFile.find_all_by_submission_id(submission.id)
49         single_file = false
50         FileUtils.remove_file("downloads/submissions.zip", true)
51       end
52 
53       files.each do |file|
54         if file.nil?
55           # no such submission file
56           render :file => "#{::Rails.root.to_s}/public/422.xml", :status => 422
57           return
58         end
59 
60         #Get the file contents
61         begin
62           if params[:include_annotations] == 'true'
63             file_contents = file.retrieve_file(true)
64           else
65             file_contents = file.retrieve_file
66           end
67         rescue Exception => e
68             # could not retrieve file
69             render :file => "#{::Rails.root.to_s}/public/500.xml", :status => 500
70           return
71         end
72 
73         #Send the file or make the zip file
74         if single_file
75           send_data file_contents, :disposition => 'inline', :filename => file.filename
76         else
77           Zip::ZipFile.open("tmp/submissions.zip", Zip::ZipFile::CREATE) do |zipfile|
78             if ! zipfile.find_entry(file.path)
79               zipfile.mkdir(file.path)
80             end
81             zipfile.get_output_stream(file.path + "/" + file.filename) { |f| f.puts file_contents }
82           end
83         end
84       end
85 
86       #Send the zip file
87       if !single_file
88         send_file "tmp/submissions.zip", :disposition => 'inline', :filename => "#{params[:assignment]}_#{params[:group_name]}.zip"
89       end
90     end
91 
92     private
93 
94     # Helper method to check for required HTTP parameters
95     def has_required_http_params?(param_hash)
96       # Note: The blank? method is a Rails extension.
97       # Specific keys have to be present, and their values
98       # must not be blank.
99       if !param_hash[:assignment].blank? &&
100       !param_hash[:group_name].blank?
101         return true
102       else
103         return false
104       end
105     end
106   end
107 
108 end

Generated on Tue Feb 07 00:07:35 -0500 2012 with rcov 0.9.10