Sha256: 65d46c2a7640713f3986671324df66415a6121b123a4bb88f475472dfdeae3e8

Contents?: true

Size: 1.6 KB

Versions: 37

Compression:

Stored size: 1.6 KB

Contents

require 'csv'

module Bearcat
  class Client < Footrest::Client
    module Reports
      extend ClientModule

      prefix "/api/v1/accounts/:account/reports/" do
        get :report_list
        post :start_report, ":report_name"
        get :report_history, ":report_name"
        get :report_status, ":report_name/:report_id"
        delete :delete_report, ":report_name/:report_id"
      end

      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

    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
bearcat-1.5.13 lib/bearcat/client/reports.rb
bearcat-1.5.12 lib/bearcat/client/reports.rb
bearcat-1.5.11 lib/bearcat/client/reports.rb
bearcat-1.5.10 lib/bearcat/client/reports.rb
bearcat-1.5.9 lib/bearcat/client/reports.rb
bearcat-1.5.8 lib/bearcat/client/reports.rb
bearcat-1.5.7 lib/bearcat/client/reports.rb
bearcat-1.5.6 lib/bearcat/client/reports.rb
bearcat-1.5.5 lib/bearcat/client/reports.rb
bearcat-1.5.4 lib/bearcat/client/reports.rb
bearcat-1.5.3 lib/bearcat/client/reports.rb
bearcat-1.5.2 lib/bearcat/client/reports.rb
bearcat-1.5.0 lib/bearcat/client/reports.rb
bearcat-1.5.0.beta4 lib/bearcat/client/reports.rb
bearcat-1.5.0.beta3 lib/bearcat/client/reports.rb
bearcat-1.5.0.beta2 lib/bearcat/client/reports.rb
bearcat-1.5.0.beta1 lib/bearcat/client/reports.rb