ip2country 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/ip2c.updatedb ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'sqlite3'
4
+ require 'fileutils'
5
+ require 'csv'
6
+
7
+ puts 'Removing old database if exists...'
8
+ FileUtils.rm(ARGV[1]) if File.exist?(ARGV[1])
9
+
10
+ puts "Creating new database..."
11
+ db = SQLite3::Database.new(ARGV[1])
12
+ db.execute('CREATE TABLE ip2country (ip_from INTEGER, ip_to INTEGER, country_code2 TEXT, country_code3 TEXT, country_name TEXT)')
13
+
14
+ puts 'Inserting values into database... (may take a while)'
15
+ CSV.foreach(ARGV[0]) do |row|
16
+ from, to, cc, ccc, name = row
17
+ cc = SQLite3::Database.quote(cc)
18
+ ccc = SQLite3::Database.quote(ccc)
19
+ name = SQLite3::Database.quote(name)
20
+ db.execute("INSERT INTO ip2country VALUES (#{from}, #{to}, '#{cc}', '#{ccc}', '#{name}')")
21
+ end
22
+
23
+ puts 'Done'
@@ -0,0 +1,18 @@
1
+ class IP2Country
2
+ # IP2Country represents a country of a matching IP returned by IP2Country#lookup.
3
+ class Country
4
+ attr_reader :cc, :ccc, :name
5
+
6
+ def initialize(cc, ccc, name)
7
+ @cc = cc.to_s
8
+ @ccc = ccc.to_s
9
+ @name = name.to_s
10
+ end
11
+
12
+ alias code2 cc
13
+ alias code3 ccc
14
+ alias country_code2 cc
15
+ alias country_code3 ccc
16
+ alias country_name name
17
+ end
18
+ end
Binary file
data/lib/ip2country.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'sqlite3'
2
+ require 'ip2country/country'
3
+
4
+ # IP2Country is used to lookup a host IP from a specified user ip2country database
5
+ # and determine their regional location.
6
+ class IP2Country
7
+ # Initialize IP2Country instance using a specified database. If no database is
8
+ # provided, then it will attempt to use the default one packaged with this
9
+ # library.
10
+ def initialize(database = "#{File.dirname(__FILE__)}/ip2country/ip2country.db")
11
+ @db_file = File.expand_path(database.to_s)
12
+ @db = SQLite3::Database.open(@db_file)
13
+ end
14
+
15
+ # Lookup a host IP to figure out their region. If successful, an IP2Country::Country
16
+ # instance will be returned, else nil.
17
+ def lookup(ip)
18
+ long = ip2long(ip)
19
+ result = @db.execute("SELECT country_code2, country_code3, country_name FROM ip2country WHERE ip_from <= #{long} AND ip_to >= #{long}")[0]
20
+ Country.new(*result) if result
21
+ end
22
+
23
+ private
24
+
25
+ # Some code I took from Ruby's mailing list to convert the IP to a long. I
26
+ # believe the original author of this method is Florian Frank.
27
+ def ip2long(ip)
28
+ long = 0
29
+ ip.split(/\./).reverse.each_with_index { |x,i| long += x.to_i << (i * 8) }
30
+ long
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: ip2country
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-05-01 00:00:00 +09:00
8
+ summary: IP2Country library for Ruby
9
+ require_paths:
10
+ - lib
11
+ email: shugotenshi@gmail.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: ip2country
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Matthew Harris
31
+ files:
32
+ - lib/ip2country.rb
33
+ - lib/ip2country
34
+ - lib/ip2country/country.rb
35
+ - lib/ip2country/ip2country.db
36
+ - bin/ip2c.updatedb
37
+ test_files: []
38
+
39
+ rdoc_options: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ executables:
44
+ - ip2c.updatedb
45
+ extensions: []
46
+
47
+ requirements: []
48
+
49
+ dependencies:
50
+ - !ruby/object:Gem::Dependency
51
+ name: sqlite3-ruby
52
+ version_requirement:
53
+ version_requirements: !ruby/object:Gem::Version::Requirement
54
+ requirements:
55
+ - - ">"
56
+ - !ruby/object:Gem::Version
57
+ version: 0.0.0
58
+ version: