rs_ipgeobase_mongoid 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac6984be29eb73ddf56c0b40659f1aa24e2161a6
4
+ data.tar.gz: 07a6cad165f9e31a983adf9ea9335118b22ae74d
5
+ SHA512:
6
+ metadata.gz: 8bb623b4f098fe791b779931c38d5efc7afa20059c671288d470fcd3e8257d60a64017fe19b4b0caf8af147ff7a02f5058185ae865f79e3944ce8c107f70d55e
7
+ data.tar.gz: fb3867ff850ebb2636369a05769b962c40a7064136e9a6489bf1755644984a45a26ef86be3265c4a26559401ba3dfa9b2f77d11bbb73c0642446ceda752469dd
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /tmp/*
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ ipgeobase
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p353
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ipgeobase.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rs_ipgeobase_mongoid (1.0.0)
5
+ mongoid (>= 3.0, < 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.16)
11
+ activesupport (= 3.2.16)
12
+ builder (~> 3.0.0)
13
+ activesupport (3.2.16)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ multi_json (~> 1.0)
16
+ builder (3.0.4)
17
+ diff-lcs (1.2.5)
18
+ i18n (0.6.9)
19
+ mongoid (3.1.6)
20
+ activemodel (~> 3.2)
21
+ moped (~> 1.4)
22
+ origin (~> 1.0)
23
+ tzinfo (~> 0.3.29)
24
+ moped (1.5.1)
25
+ multi_json (1.8.4)
26
+ origin (1.1.0)
27
+ rspec (2.14.1)
28
+ rspec-core (~> 2.14.0)
29
+ rspec-expectations (~> 2.14.0)
30
+ rspec-mocks (~> 2.14.0)
31
+ rspec-core (2.14.7)
32
+ rspec-expectations (2.14.4)
33
+ diff-lcs (>= 1.1.3, < 2.0)
34
+ rspec-mocks (2.14.4)
35
+ tzinfo (0.3.38)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (>= 1.0.21)
42
+ rs_ipgeobase_mongoid!
43
+ rspec (>= 2.14.0)
data/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = IPGeoBase
2
+
3
+ Russia and Ukraine geoip lookup with DB from http://ipgeobase.ru for Mongoid 3/4
4
+
5
+ WARNING: 1.0 breaks backward compatibility. For mongoid 2 use ~> 0
6
+
7
+ Гем для работы с архивом с сайта http://ipgeobase.ru, содержащим базу местонахождений российских и украинских ip-адресов (с точностью до города) для MongoDB (Mongoid 3/4).
8
+
9
+ == Installation
10
+
11
+ Add to Gemfile:
12
+
13
+ gem "rs_ipgeobase_mongoid"
14
+
15
+ Install required gems:
16
+
17
+ bundle install
18
+
19
+ == Upload new database
20
+
21
+ * Update cities and regions. Downloads file if it's not present in tmp/cities.txt
22
+
23
+ rake ipgeobase:cities
24
+
25
+ * city id's are not changed on DB update and can be used as foreign keys
26
+
27
+ * Update geo ips. Downloads file if it's not present in tmp/cidr_optim.txt
28
+
29
+ rake ipgeobase:ips
30
+
31
+ == Methods
32
+
33
+ Search for city by IP (Russia and Ukraine only):
34
+
35
+ После того, как выполнены все предыдущие шаги можно искать город по IP адресу.
36
+
37
+ Работает только для РФ и Украины. Если город не найден, метод вернет nil.
38
+
39
+ > geo = Ipgeobase::find_region_by_ip('95.170.177.170')
40
+ > pp geo
41
+ => #<Ipgeobase::City _id: 5059d68ee779891c72000201, _type: nil, geo_id: 1428, city: "Красноярск",
42
+ region: "Красноярский край", district: "Сибирский федеральный округ", lat: 56.001251, lon: 92.88559,
43
+ country_id: BSON::ObjectId('5059d68de779891c72000001')>
44
+ > geo.city
45
+ => "Красноярск"
46
+ > geo.region
47
+ => "Красноярский край"
48
+ > geo.district
49
+ => "Сибирский федеральный округ"
50
+ > geo.country.name
51
+ => "Россия"
52
+ > geo.country.code
53
+ => "RU"
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ load File.join(File.dirname(__FILE__), 'lib', 'ipgeobase', 'tasks.rake')
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ module Ipgeobase
3
+ class City
4
+ include Mongoid::Document
5
+
6
+ field :geo_id, type: Integer
7
+ index({geo_id: 1}, {unique: true})
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
+ belongs_to :country, class_name: 'Ipgeobase::Country'
17
+ has_many :ips, class_name: 'Ipgeobase::Ip'
18
+
19
+ validates_presence_of :city, :country, :geo_id
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ module Ipgeobase
4
+ class Country
5
+ include Mongoid::Document
6
+
7
+ field :name, type: String
8
+ field :code, type: String
9
+
10
+ has_many :cities, class_name: 'Ipgeobase::City'
11
+
12
+ validates_presence_of :name
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ module Ipgeobase
4
+ class Ip
5
+ include Mongoid::Document
6
+
7
+ field :start_ip, type: Integer
8
+ index({start_ip: 1}, {unique: true})
9
+ field :end_ip, type: Integer
10
+
11
+ belongs_to :city, class_name: 'Ipgeobase::City'
12
+ validates_presence_of :start_ip, :end_ip, :city_id
13
+ end
14
+ end
data/lib/ipgeobase.rb ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+
3
+ require "ipgeobase/version"
4
+ require 'ipaddr'
5
+
6
+ if defined? Rails::Engine
7
+ require 'ipgeobase/engine'
8
+ end
9
+
10
+ if defined? Rails::Railtie
11
+ require 'ipgeobase/railtie'
12
+ end
13
+
14
+ module Ipgeobase
15
+ class << self
16
+ def find_region_by_ip(user_ip)
17
+ long = ip2long(user_ip)
18
+ ip = Ipgeobase::Ip.where(start_ip: { '$lte' => long }).order_by({ start_ip: -1 }).limit(1).first
19
+ if ip.nil? or ip.end_ip < long
20
+ nil
21
+ else
22
+ ip.city
23
+ end
24
+ end
25
+
26
+ def ip2long(ip)
27
+ IPAddr.new(ip, Socket::AF_INET).to_i
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module Ipgeobase
2
+ class Engine < ::Rails::Engine
3
+
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ module Ipgeobase
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "ipgeobase/tasks.rake"
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ module Ipgeobase
4
+ module Task
5
+ def self.obtain_content_by_filename(filename)
6
+ if defined? Rails
7
+ file = ::Rails.root.join('tmp', filename)
8
+ else
9
+ file = File.join(File.dirname(__FILE__), '..', '..', 'tmp', filename)
10
+ end
11
+ unless File.exist? file
12
+ puts 'downloading database'
13
+ url = 'http://ipgeobase.ru/files/db/Main/geo_files.tar.gz'
14
+ Kernel.system("curl #{url} | tar -xzOf - #{filename} | iconv -f cp1251 -t utf-8 > #{file}")
15
+ end
16
+ File.read(file)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
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
+ puts 'updating cities'
9
+ russia = Ipgeobase::Country.find_or_create_by(name: 'Россия', code: 'RU')
10
+ ukraine = Ipgeobase::Country.find_or_create_by(name: 'Украина', code: 'UA')
11
+ content = Ipgeobase::Task.obtain_content_by_filename('cities.txt')
12
+ content.each_line do |c|
13
+ options = c.split("\t")
14
+ country = if options[3].include? "Украина"
15
+ ukraine
16
+ else
17
+ russia
18
+ end
19
+ city = Ipgeobase::City.find_or_initialize_by(geo_id: options[0])
20
+ city.assign_attributes(city: options[1], region: options[2], district: options[3], lat: options[4], lon: options[5], country: country)
21
+ city.save!
22
+ end
23
+ end
24
+
25
+ desc "Update geo ips. Downloads file if it's not present in tmp/cidr_optim.txt"
26
+ task :ips => :environment do
27
+ puts 'updating ips'
28
+ content = Ipgeobase::Task.obtain_content_by_filename('cidr_optim.txt')
29
+ Ipgeobase::Ip.delete_all
30
+ content.each_line do |c|
31
+ options = c.split("\t")
32
+ next if options.last.to_i.zero?
33
+ city = Ipgeobase::City.where(geo_id: options.last.strip.to_i).first
34
+ if city.nil?
35
+ p "No city"
36
+ p options
37
+ next
38
+ end
39
+ Ipgeobase::Ip.create(:start_ip => options.first.to_i, :end_ip => options.second.to_i, city_id: city.id)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module Ipgeobase
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'ipgeobase'
2
+
Binary file
@@ -0,0 +1,25 @@
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 = "rs_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 3/4"
13
+ s.summary = "IPGeoBase.ru for mongoid 3/4"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency('mongoid', [">= 3.0", "< 5.0"])
21
+ s.required_ruby_version = '>= 1.9.2'
22
+
23
+ s.add_development_dependency(%q<rspec>, [">= 2.14.0"])
24
+ s.add_development_dependency(%q<bundler>, [">= 1.0.21"])
25
+ end
@@ -0,0 +1,19 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Ipgeobase do
6
+ it 'finds city by ip' do
7
+ geo = Ipgeobase::find_region_by_ip('95.170.177.170')
8
+ geo.region.should eq 'Красноярский край'
9
+ geo.country.code.should eq 'RU'
10
+ end
11
+ it "doesn't return first or last city on fail" do
12
+ geo = Ipgeobase::find_region_by_ip('0.0.0.0')
13
+ geo.should be_nil
14
+ geo = Ipgeobase::find_region_by_ip('10.0.0.0')
15
+ geo.should be_nil
16
+ geo = Ipgeobase::find_region_by_ip('255.255.255.255')
17
+ geo.should be_nil
18
+ end
19
+ end
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+
6
+ require 'rspec'
7
+ require 'mongoid'
8
+ require 'ipgeobase'
9
+
10
+
11
+ # Requires supporting files with custom matchers and macros, etc,
12
+ # in ./support/ and its subdirectories.
13
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
14
+
15
+ Mongoid.configure do |config|
16
+ ENV["MONGOID_ENV"] = "test"
17
+ Mongoid.load!("spec/support/mongoid.yml")
18
+ end
19
+
20
+ require File.dirname(__FILE__) + '/../app/models/ipgeobase/city.rb'
21
+ require File.dirname(__FILE__) + '/../app/models/ipgeobase/country.rb'
22
+ require File.dirname(__FILE__) + '/../app/models/ipgeobase/ip.rb'
23
+
24
+ Ipgeobase::City.create_indexes
25
+ Ipgeobase::Ip.create_indexes
26
+
27
+ require 'rake'
28
+ app = Rake.application
29
+ app.init
30
+ # do this as many times as needed
31
+ # app.add_import 'lib/ipgeobase/tasks.rake'
32
+ # this loads the Rakefile and other imports
33
+ app.load_rakefile
34
+ app["ipgeobase:cities"].execute
35
+ moscow_id1 = Ipgeobase::City.where(city: 'Москва').first.id
36
+
37
+ app["ipgeobase:cities"].execute
38
+ moscow_id2 = Ipgeobase::City.where(city: 'Москва').first.id
39
+ # check that city ids are not changed on reimport
40
+ raise 'city id changed' if moscow_id1 != moscow_id2
41
+
42
+ app["ipgeobase:ips"].execute
43
+
44
+ RSpec.configure do |config|
45
+ config.mock_with :rspec
46
+ end
@@ -0,0 +1,8 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: ipgeobase
5
+ hosts:
6
+ - localhost:27017
7
+ options:
8
+ consistency: :strong
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rs_ipgeobase_mongoid
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - GlebTv
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: '5.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: 2.14.0
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 2.14.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.21
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 1.0.21
61
+ description: IPGeoBase.ru for mongoid 3/4
62
+ email:
63
+ - glebtv@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - .gitignore
69
+ - .ruby-gemset
70
+ - .ruby-version
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - README.rdoc
74
+ - Rakefile
75
+ - app/models/ipgeobase/city.rb
76
+ - app/models/ipgeobase/country.rb
77
+ - app/models/ipgeobase/ip.rb
78
+ - lib/ipgeobase.rb
79
+ - lib/ipgeobase/engine.rb
80
+ - lib/ipgeobase/railtie.rb
81
+ - lib/ipgeobase/task.rb
82
+ - lib/ipgeobase/tasks.rake
83
+ - lib/ipgeobase/version.rb
84
+ - lib/rs_ipgeobase_mongoid.rb
85
+ - pkg/ipgeobase_mongoid-1.0.0.gem
86
+ - rs_ipgeobase_mongoid.gemspec
87
+ - spec/ipgeobase_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/support/mongoid.yml
90
+ - tmp/.gitkeep
91
+ homepage: https://github.com/rs-pro/ipgeobase_mongoid
92
+ licenses: []
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '>='
101
+ - !ruby/object:Gem::Version
102
+ version: 1.9.2
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.1.10
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: IPGeoBase.ru for mongoid 3/4
114
+ test_files:
115
+ - spec/ipgeobase_spec.rb
116
+ - spec/spec_helper.rb
117
+ - spec/support/mongoid.yml