<commit_before>require 'httparty'

module GooglApi
  API_URL     = 'https://www.googleapis.com/urlshortener/'
  API_VERSION = 'v1'

  def self.new(api_key)
    Client.new(api_key)
  end

  class Client
    include HTTParty
    
    base_uri "#{API_URL}#{API_VERSION}"
    headers 'Content-Type' => 'application/json; charset=utf-8'
      
    def initialize(api_key)
      @api_key = { :key => api_key }
    end
    
    def shorten(url)
      raise ArgumentError.new("A URL to shorten is required") if url.blank?
      # load_respose(self.class.post('/url', :body => "{ \"longUrl\" => \"#{url}\" }"))
      # api key not working at this time
      load_respose(self.class.post('/url', :query => @api_key, :body => "{ \"longUrl\" => \"#{url}\" }"))
    end
    
    def expand(url)
      raise ArgumentError.new("A URL to expand is required") if url.blank?
      load_respose(self.class.get('/url', :query => { :shortUrl => url }))
      # api key not working at this time
      # load_respose(self.class.get('/url', :query => @api_key.merge({ :shortUrl => url })))
    end
    
    def analytics(url, projection = "FULL")
      raise ArgumentError.new("A URL to check analytics on is required") if url.blank?
      load_respose(self.class.get('/url', :query => { :shortUrl => url, :projection => projection }))
      # api key not working at this time
      # load_respose(self.class.get('/url', :query => @api_key.merge({ :shortUrl => url, :projection => projection })))
    end
  private
    def load_respose(resp)
      raise GooglError.new(resp.message, resp.code) if resp.code != 200
      GooglApi::Response.new(resp.parsed_response)
    end
  end
  
  class GooglError < StandardError
    attr_reader :code
    alias :msg :message
    def initialize(msg, code)
      @code = code
      super("#{msg} - '#{code}'")
    end
  end
end<commit_msg>added support for using api key<commit_after>require 'httparty'

module GooglApi
  API_URL     = 'https://www.googleapis.com/urlshortener/'
  API_VERSION = 'v1'

  def self.new(api_key = nil)
    Client.new(api_key)
  end

  class Client
    include HTTParty
    
    base_uri "#{API_URL}#{API_VERSION}"
    headers 'Content-Type' => 'application/json; charset=utf-8'
      
    def initialize(api_key = nil)
      @api_key = { :key => api_key } unless api_key.blank?
    end
    
    def shorten(url)
      raise ArgumentError.new("A URL to shorten is required") if url.blank?
      load_respose(self.class.post('/url', :query => @api_key, :body => "{ \"longUrl\" => \"#{url}\" }"))
    end
    
    def expand(url)
      raise ArgumentError.new("A URL to expand is required") if url.blank?
      load_respose(self.class.get('/url', :query => @api_key.merge({ :shortUrl => url })))
    end
    
    def analytics(url, projection = "FULL")
      raise ArgumentError.new("A URL to check analytics on is required") if url.blank?
      load_respose(self.class.get('/url', :query => @api_key.merge({ :shortUrl => url, :projection => projection })))
    end
  private
    def load_respose(resp)
      raise GooglError.new(resp.message, resp.code) if resp.code != 200
      GooglApi::Response.new(resp.parsed_response)
    end
  end
  
  class GooglError < StandardError
    attr_reader :code
    alias :msg :message
    def initialize(msg, code)
      @code = code
      super("#{msg} - '#{code}'")
    end
  end
end
{'fraction_non_alphanumeric': 0.1457465794170137, 'fraction_numerical': 0.00297441998810232, 'mean_word_length': 2.9195804195804196, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7284460', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1107, 'n_words': 275}