Module: Bearcat::Client::Reports

Extended by:
ClientModule
Defined in:
lib/bearcat/client/reports.rb

Constant Summary

Constants included from ClientModule

ClientModule::ARG_REGEX

Instance Attribute Summary

Attributes included from ClientModule

#_registered_endpoints

Instance Method Summary collapse

Methods included from ClientModule

context_types, endpoint, prefix

Instance Method Details

#download_report(url, save_location = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bearcat/client/reports.rb', line 16

def download_report(url, save_location=nil)
  #This method takes the verified URL returned in a Canvas report (attachment['url']), and if
  #a save_location is included, it will download it in chunks to the disk to save memory.  You
  #can also download the report to memory if you do not include a save location.
  attempts = 0
  begin
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true if url.start_with?('https')
    response = http.head(uri.to_s)
    url = response['Location']
    attempts += 1
  end while attempts <= 5 && (response.code == '301' || response.code == '302' || response.code == '307')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.to_s.start_with?('https')
  if save_location
    File.open(save_location, 'wb') do |file|
      http.request_get(uri.to_s) do |resp|
        resp.read_body do |segment|
          file.write(segment)
        end
      end
    end
  else
    response = http.request_get(uri.to_s)
    CSV.parse(response.read_body, headers: true)
  end
end