ipgeobase_mongoid 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/README.rdoc +38 -0
- data/Rakefile +1 -0
- data/app/models/ipgeobase_city.rb +21 -0
- data/app/models/ipgeobase_country.rb +12 -0
- data/app/models/ipgeobase_ip.rb +11 -0
- data/ipgeobase_mongoid.gemspec +24 -0
- data/lib/ipgeobase.rb +35 -0
- data/lib/ipgeobase/task.rb +17 -0
- data/lib/ipgeobase/version.rb +3 -0
- data/lib/tasks/ipgeobase.rake +40 -0
- metadata +73 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
/.idea
|
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
= IPGeoBase
|
2
|
+
|
3
|
+
Russia and Ukraine geoip lookup with DB from http://ipgeobase.ru for Mongoid 2
|
4
|
+
Гем для работы с архивом с сайта http://ipgeobase.ru, содержащим базу местонахождений российских и украинских ip-адресов (с точностью до города) for Mongoid 2.
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
(1) Add to Gemfile:
|
9
|
+
gem "ipgeobase_mongoid", require: 'ipgeobase'
|
10
|
+
(2) Install required gems:
|
11
|
+
bundle install
|
12
|
+
|
13
|
+
== Upload new database
|
14
|
+
|
15
|
+
* Update cities and regions. Downloads file if it's not present in tmp/cities.txt
|
16
|
+
rake ipgeobase:cities
|
17
|
+
|
18
|
+
* Update geo ips. Downloads file if it's not present in tmp/cidr_optim.txt
|
19
|
+
rake ipgeobase:ips
|
20
|
+
|
21
|
+
== Methods
|
22
|
+
|
23
|
+
После того, как выполнены все предыдущие шаги можно искать регион по IP адресу:
|
24
|
+
|
25
|
+
Работает только для РФ и Украины.
|
26
|
+
|
27
|
+
> Ipgeobase::find_region_by_ip('95.170.177.170')
|
28
|
+
=> #<IpgeobaseCity _id: 5059d68ee779891c72000201, _type: nil, geo_id: 1428, city: "Красноярск", region: "Красноярский край", district: "Сибирский федеральный округ", lat: 56.001251, lon: 92.88559, country_id: BSON::ObjectId('5059d68de779891c72000001')>
|
29
|
+
> Ipgeobase::find_region_by_ip('95.170.177.170').city
|
30
|
+
=> "Красноярск"
|
31
|
+
> Ipgeobase::find_region_by_ip('95.170.177.170').region
|
32
|
+
=> "Красноярский край"
|
33
|
+
> Ipgeobase::find_region_by_ip('95.170.177.170').district
|
34
|
+
=> "Сибирский федеральный округ"
|
35
|
+
> Ipgeobase::find_region_by_ip('95.170.177.170').country.name
|
36
|
+
=> "Россия"
|
37
|
+
> Ipgeobase::find_region_by_ip('95.170.177.170').country.code
|
38
|
+
=> "RU"
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
class IpgeobaseCity
|
4
|
+
include Mongoid::Document
|
5
|
+
|
6
|
+
field :geo_id, type: Integer
|
7
|
+
index :geo_id
|
8
|
+
|
9
|
+
field :city, type: String
|
10
|
+
field :region, type: String
|
11
|
+
field :district, type: String
|
12
|
+
|
13
|
+
field :lat, type: Float
|
14
|
+
field :lon, type: Float
|
15
|
+
|
16
|
+
|
17
|
+
belongs_to :country, class_name: 'IpgeobaseCountry'
|
18
|
+
has_many :ips, class_name: 'IpgeobaseIp'
|
19
|
+
|
20
|
+
validates_presence_of :city, :country, :geo_id
|
21
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
$:.push File.expand_path("../lib", __FILE__)
|
4
|
+
require "ipgeobase/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "ipgeobase_mongoid"
|
8
|
+
s.version = Ipgeobase::VERSION
|
9
|
+
s.authors = ["GlebTv"]
|
10
|
+
s.email = ["glebtv@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/rs-pro/ipgeobase_mongoid"
|
12
|
+
s.description = "IPGeoBase.ru for mongoid 2"
|
13
|
+
s.summary = "IPGeoBase.ru for mongoid 2"
|
14
|
+
|
15
|
+
s.rubyforge_project = "ipgeobase_mongoid"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency "mongoid", '~> 2.4'
|
23
|
+
s.required_ruby_version = '>= 1.9.2'
|
24
|
+
end
|
data/lib/ipgeobase.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "ipgeobase/version"
|
4
|
+
|
5
|
+
module Ipgeobase
|
6
|
+
if defined?(Rails) && defined?(Rails::Engine)
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
end
|
9
|
+
|
10
|
+
module Rails
|
11
|
+
class Railtie < ::Rails::Railtie
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.find_region_by_ip(user_ip)
|
17
|
+
raise ArgumentError, "Must have a ip addres by template 0.0.0.0" unless user_ip =~ /^[\d\.]+$/
|
18
|
+
long = ip2long(user_ip)
|
19
|
+
ip = IpgeobaseIp.where(start_ip: { '$lte' => long }).order_by({ start_ip: -1 }).limit(1).first
|
20
|
+
p ip
|
21
|
+
if ip.nil?
|
22
|
+
nil
|
23
|
+
else
|
24
|
+
ip.city
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.ip2long(ip)
|
29
|
+
long = 0
|
30
|
+
ip.split(/\./).reverse.each_with_index do |b, i|
|
31
|
+
long += b.to_i << (8*i)
|
32
|
+
end
|
33
|
+
long
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module Ipgeobase
|
4
|
+
module Task
|
5
|
+
def self.obtain_content_by_filename(filename)
|
6
|
+
file = ::Rails.root.join('tmp', filename)
|
7
|
+
if File.file? file
|
8
|
+
puts 'using local copy'
|
9
|
+
`iconv -f cp1251 -t utf8 #{file}`
|
10
|
+
else
|
11
|
+
puts 'downloading database'
|
12
|
+
url = 'http://ipgeobase.ru/files/db/Main/geo_files.tar.gz'
|
13
|
+
`curl -0 #{url} | tar -xzO #{file} | iconv -f cp1251 -t utf8`
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "ipgeobase/task"
|
4
|
+
|
5
|
+
namespace :ipgeobase do
|
6
|
+
desc "Update cities and regions. Downloads file if it's not present in tmp/cities.txt"
|
7
|
+
task :cities => :environment do
|
8
|
+
IpgeobaseIp.delete_all
|
9
|
+
IpgeobaseCity.delete_all
|
10
|
+
russia = IpgeobaseCountry.find_or_create_by(name: 'Россия', code: 'RU')
|
11
|
+
ukraine = IpgeobaseCountry.find_or_create_by(name: 'Украина', code: 'UA')
|
12
|
+
content = Ipgeobase::Task.obtain_content_by_filename('cities.txt')
|
13
|
+
content.each_line do |c|
|
14
|
+
options = c.split("\t")
|
15
|
+
country = if options[3].include? "Украина"
|
16
|
+
ukraine
|
17
|
+
else
|
18
|
+
russia
|
19
|
+
end
|
20
|
+
IpgeobaseCity.create!(geo_id: options[0], city: options[1], region: options[2], district: options[3], lat: options[4], lon: options[5], country: country)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Update geo ips. Downloads file if it's not present in tmp/cidr_optim.txt"
|
25
|
+
task :ips => :environment do
|
26
|
+
content = Ipgeobase::Task.obtain_content_by_filename('cidr_optim.txt')
|
27
|
+
IpgeobaseIp.delete_all
|
28
|
+
content.each_line do |c|
|
29
|
+
options = c.split("\t")
|
30
|
+
next if options.last.to_i.zero?
|
31
|
+
city = IpgeobaseCity.where(geo_id: options.last.strip.to_i).first
|
32
|
+
if city.nil?
|
33
|
+
p "No city"
|
34
|
+
p options
|
35
|
+
next
|
36
|
+
end
|
37
|
+
IpgeobaseIp.create(:start_ip => options.first.to_i, :end_ip => options.second.to_i, city_id: city.id)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ipgeobase_mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- GlebTv
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.4'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.4'
|
30
|
+
description: IPGeoBase.ru for mongoid 2
|
31
|
+
email:
|
32
|
+
- glebtv@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- README.rdoc
|
40
|
+
- Rakefile
|
41
|
+
- app/models/ipgeobase_city.rb
|
42
|
+
- app/models/ipgeobase_country.rb
|
43
|
+
- app/models/ipgeobase_ip.rb
|
44
|
+
- ipgeobase_mongoid.gemspec
|
45
|
+
- lib/ipgeobase.rb
|
46
|
+
- lib/ipgeobase/task.rb
|
47
|
+
- lib/ipgeobase/version.rb
|
48
|
+
- lib/tasks/ipgeobase.rake
|
49
|
+
homepage: https://github.com/rs-pro/ipgeobase_mongoid
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.9.2
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project: ipgeobase_mongoid
|
69
|
+
rubygems_version: 1.8.22
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: IPGeoBase.ru for mongoid 2
|
73
|
+
test_files: []
|