<commit_before>require 'nokogiri'

module Contacts
  class Google < OAuthConsumer
    CONSUMER_OPTIONS = Util.frozen_hash(
      :site => "https://www.google.com",
      :request_token_path => "/accounts/OAuthGetRequestToken",
      :access_token_path => "/accounts/OAuthGetAccessToken",
      :authorize_path => "/accounts/OAuthAuthorizeToken"
    )

    REQUEST_TOKEN_PARAMS = Util.frozen_hash(
      'scope' => "https://www.google.com/m8/feeds/"
    )

    def initialize(options={})
      super(CONSUMER_OPTIONS, REQUEST_TOKEN_PARAMS)
    end

    def contacts(options={})
      return nil if @access_token.nil?
      params = {:limit => 200}.update(options)
      google_params = translate_parameters(params)
      query = params_to_query(google_params)
      begin
        response = @access_token.get("/m8/feeds/contacts/default/thin?#{query}")
      rescue OAuth::Unauthorized => error
        # Token probably expired.
        @error = error.message
        return nil
      end
      parse_contacts(response.body)
    end

    private

    def translate_parameters(params)
      params.inject({}) do |all, pair|
        key, value = pair
        unless value.nil?
          key = case key
                when :limit
                  'max-results'
                when :offset
                  value = value.to_i + 1
                  'start-index'
                when :order
                  all['sortorder'] = 'descending' if params[:descending].nil?
                  'orderby'
                when :descending<commit_msg>fix bug: need the full url for the contact request to work<commit_after>require 'nokogiri'

module Contacts
  class Google < OAuthConsumer
    CONSUMER_OPTIONS = Util.frozen_hash(
      :site => "https://www.google.com",
      :request_token_path => "/accounts/OAuthGetRequestToken",
      :access_token_path => "/accounts/OAuthGetAccessToken",
      :authorize_path => "/accounts/OAuthAuthorizeToken"
    )

    REQUEST_TOKEN_PARAMS = Util.frozen_hash(
      'scope' => "https://www.google.com/m8/feeds/"
    )

    def initialize(options={})
      super(CONSUMER_OPTIONS, REQUEST_TOKEN_PARAMS)
    end

    def contacts(options={})
      return nil if @access_token.nil?
      params = {:limit => 200}.update(options)
      google_params = translate_parameters(params)
      query = params_to_query(google_params)
      begin
        response = @access_token.get("http://www.google.com/m8/feeds/contacts/default/thin?#{query}")
      rescue OAuth::Unauthorized => error
        # Token probably expired.
        @error = error.message
        return nil
      end
      parse_contacts(response.body)
    end

    private

    def translate_parameters(params)
      params.inject({}) do |all, pair|
        key, value = pair
        unless value.nil?
          key = case key
                when :limit
                  'max-results'
                when :offset
                  value = value.to_i + 1
                  'start-index'
                when :order
                  all['sortorder'] = 'descending' if params[:descending].nil?
                  'orderby'
                when :descending
{'fraction_non_alphanumeric': 0.10559796437659033, 'fraction_numerical': 0.003816793893129771, 'mean_word_length': 2.1200396825396823, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, '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': '4295627', 'n_tokens_mistral': 955, 'n_tokens_neox': 912, 'n_words': 186}