<commit_before>require 'google/api_client'

@@discovery_document = nil

class GoogleDrive

  def initialize(refresh_token = nil)
    @refresh_token = refresh_token
  end

  def client
    return @client if @client
    @client = GoogleDrive.google_client(@refresh_token)
    @client.authorization.fetch_access_token!
    @client.retries = 10
    @client.connection.options[:timeout] = 500

    if @@discovery_document
      @client.register_discovery_document('drive', 'v2', discovery_document.value)
    else
      @drive = client.discovered_api('drive', 'v2')
      @@discovery_document = @client.discovery_document('drive', 'v2')
    end

    @client
  end

  def drive
    @drive ||= client.discovered_api('drive', 'v2')
  end

  def self.google_client(refresh_token)
    client = Google::APIClient.new(
        :application_name => Rails.application.secrets.application_name,
        :application_version => '1.0')
    client.authorization.client_id = Rails.application.secrets.google_id
    client.authorization.client_secret = Rails.application.secrets.google_secret
    client.authorization.scope = Rails.application.secrets.google_scope
    client.authorization.refresh_token = refresh_token
    client
  end

  def load_spreadsheet(google_id)
    # Can use this to get the url where the document can be downloaded
    # result = client.execute(
    #   :api_method => drive.files.get,
    #   :parameters => { 'fileId' => google_id })
    result = HTTParty.get("https://docs.google.com/spreadsheets/export?id=#{google_id}&exportFormat=csv")
    raise "Error loading Spreadsheet from Google: #{result.body}" unless result.code == 200
    CSV.parse(result.body)
  end

end<commit_msg>Added ability to load specific sheet in spreadsheet.<commit_after>require 'google/api_client'

@@discovery_document = nil

class GoogleDrive

  def initialize(refresh_token = nil)
    @refresh_token = refresh_token
  end

  def client
    return @client if @client
    @client = GoogleDrive.google_client(@refresh_token)
    @client.authorization.fetch_access_token!
    @client.retries = 10
    @client.connection.options[:timeout] = 500

    if @@discovery_document
      @client.register_discovery_document('drive', 'v2', discovery_document.value)
    else
      @drive = client.discovered_api('drive', 'v2')
      @@discovery_document = @client.discovery_document('drive', 'v2')
    end

    @client
  end

  def drive
    @drive ||= client.discovered_api('drive', 'v2')
  end

  def self.google_client(refresh_token)
    client = Google::APIClient.new(
        :application_name => Rails.application.secrets.application_name,
        :application_version => '1.0')
    client.authorization.client_id = Rails.application.secrets.google_id
    client.authorization.client_secret = Rails.application.secrets.google_secret
    client.authorization.scope = Rails.application.secrets.google_scope
    client.authorization.refresh_token = refresh_token
    client
  end

  def load_spreadsheet(google_id, gid = nil)
    # Can use this to get the url where the document can be downloaded
    # result = client.execute(
    #   :api_method => drive.files.get,
    #   :parameters => { 'fileId' => google_id })
    url = "https://docs.google.com/spreadsheets/export?id=#{google_id}&exportFormat=csv"
    url << "&gid=#{gid}" if gid.present?
    result = HTTParty.get(url)
    raise "Error loading Spreadsheet from Google: #{result.body}" unless result.code == 200
    CSV.parse(result.body)
  end

end
{'fraction_non_alphanumeric': 0.11098365357040436, 'fraction_numerical': 0.00802982506452538, 'mean_word_length': 3.9057665260196908, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2534412', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1046, 'n_words': 216}