<commit_before>class GoogleDocs
  class ConfigurationError; end
  class RetrievalError < StandardError
    attr_accessor :response
    
    def initialize(response)
      @response = response
      super("Response returned: #{response.code} #{response.message}")
    end
  end
  
  RESOURCES = {
    :scope => 'https://docs.google.com/feeds/',
    :request_token_url => 'https://www.google.com/accounts/OAuthGetRequestToken',
    :access_token_url => 'https://www.google.com/accounts/OAuthGetAccessToken',
    :authorize_url => "https://www.google.com/accounts/OAuthAuthorizeToken",
    :list => 'https://docs.google.com/feeds/default/private/full',
    :create => 'https://docs.google.com/feeds/default/private/full'
  }
  
  TYPES = %w{document drawing file pdf presentation spreadsheet} # folder removed
  TYPES_YOU_CAN_CREATE = %w{document spreadsheet presentation}
  HEADERS = {'GData-Version' => '3.0'}
  ROLES = [:reader, :writer, :owner]
  SCOPES = [:user, :group, :domain, :default]
  
  def initialize(access_token, access_secret, consumer)
    @consumer = consumer
    @access_token = OAuth::AccessToken.new(@consumer, access_token, access_secret)
  end
  
  def list(options = {})
    url = create_url(RESOURCES[:list], options)
    res = @access_token.get(url, HEADERS)
    
    if res.code.to_i == 200
      return parse_list(res.body)
    else
      raise RetrievalError.new(res)
    end
  end
  
  def create(options)
    options.assert_valid_keys('title', 'document_type')
    raise(ArgumentError, "You must provide a title and document_type") if options[:title].nil? || options[:document_type].nil?
    
    body = generate_atom(options[:title], options[:document_type])
    res = @access_token.post(RESOURCES[:create], body, HEADERS.merge('Content-Type' => 'application/atom+xml'))

    if res.code.to_i == 201
      document = Nokogiri::XML(res.body)
      return parse_entry(document.xpath("//atom:entry", "atom" => "http://www.w3.org/2005/Atom"))
    else
      raise RetrievalError.new(res)
    end
  end
  
  def add_permission(acl_url, scope, scope_type = :user, role = :reader)
    raise ArguementError, "Unexpected scope_type #{scope_type}" unless SCOPES.include?(scope_type)
    raise ArguementError, "Unexpected role #{role}" unless ROLES.include?(role)
    
    body = generate_acl_atom(scope, scope_type, role)
    res = @access_token.post(acl_url, body, HEADERS.merge('Content-Type' => 'application/atom+xml'))
    
    if res.code.to_i == 201 # the acl entry was added
      return res
    elsif res.code.to_i == 409 # the acl entry already exists
      return false
    else
      raise RetrievalError.new(res)
    end
  end
  
  protected
    def parse_list(data)
      docs = []
      document = Nokogiri::XML(data)
      document.xpath("//atom:entry", "atom" => "http://www.w3.org/2005/Atom").each do |entry|
        doc = parse_entry(entry)
        docs << doc<commit_msg>Fixing typo in lib/google_docs.rb ArguementError => ArgumentError<commit_after>class GoogleDocs
  class ConfigurationError; end
  class RetrievalError < StandardError
    attr_accessor :response
    
    def initialize(response)
      @response = response
      super("Response returned: #{response.code} #{response.message}")
    end
  end
  
  RESOURCES = {
    :scope => 'https://docs.google.com/feeds/',
    :request_token_url => 'https://www.google.com/accounts/OAuthGetRequestToken',
    :access_token_url => 'https://www.google.com/accounts/OAuthGetAccessToken',
    :authorize_url => "https://www.google.com/accounts/OAuthAuthorizeToken",
    :list => 'https://docs.google.com/feeds/default/private/full',
    :create => 'https://docs.google.com/feeds/default/private/full'
  }
  
  TYPES = %w{document drawing file pdf presentation spreadsheet} # folder removed
  TYPES_YOU_CAN_CREATE = %w{document spreadsheet presentation}
  HEADERS = {'GData-Version' => '3.0'}
  ROLES = [:reader, :writer, :owner]
  SCOPES = [:user, :group, :domain, :default]
  
  def initialize(access_token, access_secret, consumer)
    @consumer = consumer
    @access_token = OAuth::AccessToken.new(@consumer, access_token, access_secret)
  end
  
  def list(options = {})
    url = create_url(RESOURCES[:list], options)
    res = @access_token.get(url, HEADERS)
    
    if res.code.to_i == 200
      return parse_list(res.body)
    else
      raise RetrievalError.new(res)
    end
  end
  
  def create(options)
    options.assert_valid_keys('title', 'document_type')
    raise(ArgumentError, "You must provide a title and document_type") if options[:title].nil? || options[:document_type].nil?
    
    body = generate_atom(options[:title], options[:document_type])
    res = @access_token.post(RESOURCES[:create], body, HEADERS.merge('Content-Type' => 'application/atom+xml'))

    if res.code.to_i == 201
      document = Nokogiri::XML(res.body)
      return parse_entry(document.xpath("//atom:entry", "atom" => "http://www.w3.org/2005/Atom"))
    else
      raise RetrievalError.new(res)
    end
  end
  
  def add_permission(acl_url, scope, scope_type = :user, role = :reader)
    raise ArgumentError, "Unexpected scope_type #{scope_type}" unless SCOPES.include?(scope_type)
    raise ArgumentError, "Unexpected role #{role}" unless ROLES.include?(role)
    
    body = generate_acl_atom(scope, scope_type, role)
    res = @access_token.post(acl_url, body, HEADERS.merge('Content-Type' => 'application/atom+xml'))
    
    if res.code.to_i == 201 # the acl entry was added
      return res
    elsif res.code.to_i == 409 # the acl entry already exists
      return false
    else
      raise RetrievalError.new(res)
    end
  end
  
  protected
    def parse_list(data)
      docs = []
      document = Nokogiri::XML(data)
      document.xpath("//atom:entry", "atom" => "http://www.w3.org/2005/Atom").each do |entry|
        doc = parse_entry(entry)
        docs << doc
{'fraction_non_alphanumeric': 0.13390119250425894, 'fraction_numerical': 0.008177172061328791, 'mean_word_length': 3.5688715953307395, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 26, 'https://': 12, 'lorem ipsum': 0, 'www.': 10, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6373025', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1912, 'n_words': 392}