Sha256: 8369b6327fa5ba44b1d23ed646adf158995bc9d6535ea6f65532febb267e293f

Contents?: true

Size: 1.94 KB

Versions: 62

Compression:

Stored size: 1.94 KB

Contents

require 'csv'

module Bearcat
  class Client < Footrest::Client
    module Reports

      def report_list(account)
        get("/api/v1/accounts/#{account}/reports")
      end

      def start_report(account, report_name, params = {})
        post("/api/v1/accounts/#{account}/reports/#{report_name}", params)
      end

      def report_history(account, report_name)
        get("/api/v1/accounts/#{account}/reports/#{report_name}")
      end

      def report_status(account, report_name, report_id)
        get("/api/v1/accounts/#{account}/reports/#{report_name}/#{report_id}")
      end

      def delete_report(account, report_name, report_id)
        delete("/api/v1/accounts/#{account}/reports/#{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

62 entries across 62 versions & 1 rubygems

Version Path
bearcat-1.4.13 lib/bearcat/client/reports.rb
bearcat-1.4.12 lib/bearcat/client/reports.rb
bearcat-1.4.11 lib/bearcat/client/reports.rb
bearcat-1.4.10 lib/bearcat/client/reports.rb
bearcat-1.4.9 lib/bearcat/client/reports.rb
bearcat-1.4.8 lib/bearcat/client/reports.rb
bearcat-1.4.7 lib/bearcat/client/reports.rb
bearcat-1.4.6 lib/bearcat/client/reports.rb
bearcat-1.4.5 lib/bearcat/client/reports.rb
bearcat-1.4.4 lib/bearcat/client/reports.rb
bearcat-1.4.3 lib/bearcat/client/reports.rb
bearcat-1.4.2 lib/bearcat/client/reports.rb
bearcat-1.4.1 lib/bearcat/client/reports.rb
bearcat-1.4.0 lib/bearcat/client/reports.rb
bearcat-1.3.55 lib/bearcat/client/reports.rb
bearcat-1.3.53 lib/bearcat/client/reports.rb
bearcat-1.3.52 lib/bearcat/client/reports.rb
bearcat-1.3.51 lib/bearcat/client/reports.rb
bearcat-1.3.49 lib/bearcat/client/reports.rb
bearcat-1.3.48 lib/bearcat/client/reports.rb