Class: GeoIP::Country

Inherits:
Object
  • Object
show all
Defined in:
ext/geoip/geoip.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(*args) ⇒ Object

GeoIP::Country.new(‘/path/to/GeoIPCountry.dat’) load_option is not required for this database because it is ignored.



217
218
219
220
# File 'ext/geoip/geoip.c', line 217

static VALUE rb_geoip_country_new(int argc, VALUE *argv, VALUE self)
{
  return rb_geoip_database_new(mGeoIP_Country, argc, argv, self);
}

Instance Method Details

#look_up(addr) ⇒ Object

Pass this function an IP address as a string, it will return a hash containing all the information that the database knows about the IP

db.look_up('24.24.24.24')
=> {:country_code=>"US",
    :country_code3=>"USA",
    :country_name=>"United States"}


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'ext/geoip/geoip.c', line 229

VALUE rb_geoip_country_look_up(VALUE self, VALUE addr) {
  GeoIP *gi;
  VALUE hash = Qnil;
  int country_id;
  
  Check_Type(addr, T_STRING);
  Data_Get_Struct(self, GeoIP, gi);
  country_id = GeoIP_id_by_addr(gi, StringValuePtr(addr));
  if(country_id < 1) return Qnil;
  
  hash = rb_hash_new();
  rb_hash_sset(hash, "country_code", rb_str_new2(GeoIP_country_code[country_id]));
  rb_hash_sset(hash, "country_code3", rb_str_new2(GeoIP_country_code3[country_id]));
  rb_hash_sset(hash, "country_name", rb_str_new2(GeoIP_country_name[country_id]));
  
  return hash;
}