<commit_before>module CensusApi
  class Client
    attr_reader   :api_key, :options
    attr_accessor :dataset

    DATASETS = %w( sf1 ac5 )

    def initialize(options = {})
      raise ArgumentError, "You must set an :api_key." unless options.keys.include? :api_key

      # Use RestClient directly to determine the validity of the API Key
      path = "http://api.census.gov/data/2010/sf1?key=#{options[:api_key]}&get=P0010001&for=state:01"
      response = RestClient.get(path)

      if response.body.include? "Invalid Key"
        raise "'#{options[:api_key]}' is not a valid API key. Check your key for errors, or request a new one at census.gov."
      end
      
      @api_key = options[:api_key]
      if options[:dataset]
        @dataset = options[:dataset].downcase if DATASETS.include? options[:dataset].downcase
      end
    end

    def find(field, level, *within)
      raise "Client has not been assigned a dataset to query. Try @client.dataset = 'SF1' or anything from #{DATASETS}" if self.dataset.nil?
      Request.find(@dataset, {key: @api_key, fields: field, level: level, within: within})
    end

    def dataset=(dataset)
      @dataset = dataset.downcase
    end

  end
end<commit_msg>api_key back to first parameter, rest is options hash<commit_after>module CensusApi
  class Client
    attr_reader   :api_key, :options
    attr_accessor :dataset

    DATASETS = %w( sf1 ac5 ) # can add more datasets as support becomes available

    def initialize(api_key, options = {})
      raise ArgumentError, "You must set an api_key." unless api_key

      # Use RestClient directly to determine the validity of the API Key
      path = "http://api.census.gov/data/2010/sf1?key=#{api_key}&get=P0010001&for=state:01"
      response = RestClient.get(path)

      if response.body.include? "Invalid Key"
        raise "'#{api_key}' is not a valid API key. Check your key for errors, or request a new one at census.gov."
      end
      
      @api_key = api_key
      if options[:dataset]
        @dataset = options[:dataset].downcase if DATASETS.include? options[:dataset].downcase
      end
    end

    def find(field, level, *within)
      raise "Client has not been assigned a dataset to query. Try @client.dataset = 'SF1' or anything from #{DATASETS}" if self.dataset.nil?
      Request.find(@dataset, {key: @api_key, fields: field, level: level, within: within})
    end

    def dataset=(dataset)
      @dataset = dataset.downcase
    end

  end
end
{'fraction_non_alphanumeric': 0.10298869143780291, 'fraction_numerical': 0.013731825525040387, 'mean_word_length': 3.0874587458745872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4086837', 'n_tokens_mistral': 770, 'n_tokens_neox': 739, 'n_words': 247}