<commit_before>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
IP_GOOGLE_US = '172.16.58.3'
IP_PRIVATE = '10.0.0.1'
IP_LOCAL = '127.0.0.1'
# Use WebMock as default to speed up tests and for local development without a connection
# Change this to false to have tests make real http requests if you want. Perhaps to check whether IpInfoDb's API has changed
# However, you may need to increase the GeoIp.fallback_timeout variable if Timeout exceptions occur when tests are run
USE_WEBMOCK = true

describe 'GeoIp' do
  before :all do
    unless USE_WEBMOCK
      puts 'Running tests WITHOUT WebMock. You will need an internet connection. You may need to increase the GeoIp.fallback_timeout amount.'
      WebMock.disable!
    end
  end

  def stub_geolocation(ip, options = {}, &block)
    if USE_WEBMOCK
      stub_request(:get, GeoIp.lookup_url(ip, options)).
             with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'}).
             to_return(:status => 200, :body => yield, :headers => {})
    end
  end

  before :each do
    api_config = YAML.load_file(File.dirname(__FILE__) + '/api.yml')
    GeoIp.api_key = api_config['key']
  end

  context 'api_key' do
    it 'should return the API key when set' do
      GeoIp.api_key = 'my_api_key'
      GeoIp.api_key.should == 'my_api_key'
    end

    it 'should throw an error when API key is not set' do
      GeoIp.api_key = nil
      lambda {GeoIp.geolocation(IP_GOOGLE_US)}.should raise_error
    end
  end

  context 'city' do
    it 'should return the correct city for a public ip address' do
      stub_geolocation(IP_GOOGLE_US) do
%[{
              "statusCode" : "OK",
              "statusMessage" : "",
              "ipAddress" : "172.16.58.3",
              "countryCode" : "US",
              "countryName" : "UNITED STATES",
              "regionName" : "CALIFORNIA",
              "cityName" : "MONTEREY PARK",
              "zipCode" : "91754",
              "latitude" : "34.0505",<commit_msg>Spaces around brackets<commit_after>require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
IP_GOOGLE_US = '172.16.58.3'
IP_PRIVATE = '10.0.0.1'
IP_LOCAL = '127.0.0.1'
# Use WebMock as default to speed up tests and for local development without a connection
# Change this to false to have tests make real http requests if you want. Perhaps to check whether IpInfoDb's API has changed
# However, you may need to increase the GeoIp.fallback_timeout variable if Timeout exceptions occur when tests are run
USE_WEBMOCK = true

describe 'GeoIp' do
  before :all do
    unless USE_WEBMOCK
      puts 'Running tests WITHOUT WebMock. You will need an internet connection. You may need to increase the GeoIp.fallback_timeout amount.'
      WebMock.disable!
    end
  end

  def stub_geolocation(ip, options = {}, &block)
    if USE_WEBMOCK
      stub_request(:get, GeoIp.lookup_url(ip, options)).
             with(:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate'}).
             to_return(:status => 200, :body => yield, :headers => {})
    end
  end

  before :each do
    api_config = YAML.load_file(File.dirname(__FILE__) + '/api.yml')
    GeoIp.api_key = api_config['key']
  end

  context 'api_key' do
    it 'should return the API key when set' do
      GeoIp.api_key = 'my_api_key'
      GeoIp.api_key.should == 'my_api_key'
    end

    it 'should throw an error when API key is not set' do
      GeoIp.api_key = nil
      lambda { GeoIp.geolocation(IP_GOOGLE_US) }.should raise_error
    end
  end

  context 'city' do
    it 'should return the correct city for a public ip address' do
      stub_geolocation(IP_GOOGLE_US) do
%[{
              "statusCode" : "OK",
              "statusMessage" : "",
              "ipAddress" : "172.16.58.3",
              "countryCode" : "US",
              "countryName" : "UNITED STATES",
              "regionName" : "CALIFORNIA",
              "cityName" : "MONTEREY PARK",
              "zipCode" : "91754",
              "latitude" : "34.0505",
{'fraction_non_alphanumeric': 0.10977110509475757, 'fraction_numerical': 0.02116662564607433, 'mean_word_length': 2.759481961147086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 8, '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': '7515575', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1301, 'n_words': 398}