Class: IP2Country
- Inherits:
-
Object
- Object
- IP2Country
- Defined in:
- lib/ip2country.rb,
lib/ip2country/country.rb
Overview
IP2Country is used to lookup a host IP from a specified user ip2country database and determine their regional location.
Defined Under Namespace
Classes: Country
Instance Method Summary collapse
-
#initialize(database = "#{File.dirname(__FILE__)}/ip2country/ip2country.db") ⇒ IP2Country
constructor
Initialize IP2Country instance using a specified database.
-
#lookup(ip) ⇒ Object
Lookup a host IP to figure out their region.
Constructor Details
#initialize(database = "#{File.dirname(__FILE__)}/ip2country/ip2country.db") ⇒ IP2Country
Initialize IP2Country instance using a specified database. If no database is provided, then it will attempt to use the default one packaged with this library.
10 11 12 13 |
# File 'lib/ip2country.rb', line 10 def initialize(database = "#{File.dirname(__FILE__)}/ip2country/ip2country.db") @db_file = File.(database.to_s) @db = SQLite3::Database.open(@db_file) end |
Instance Method Details
#lookup(ip) ⇒ Object
Lookup a host IP to figure out their region. If successful, an IP2Country::Country instance will be returned, else nil.
17 18 19 20 21 |
# File 'lib/ip2country.rb', line 17 def lookup(ip) long = ip2long(ip) result = @db.execute("SELECT country_code2, country_code3, country_name FROM ip2country WHERE ip_from <= #{long} AND ip_to >= #{long}")[0] Country.new(*result) if result end |