ipgeobase_local 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ .bundle
5
+ .config
6
+ coverage
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+ Gemfile.lock
16
+
17
+ # YARD artifacts
18
+ .yardoc
19
+ _yardoc
20
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ipgeobase_local.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Anton Taraev
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # IpgeobaseLocal
2
+
3
+ География российских и украинских IP-адресов. Поиск местонахождения (города) IP-адреса, локальный поиск
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ipgeobase_local'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ipgeobase_local
18
+
19
+ ## Usage
20
+
21
+ rake ipgeobase_local:update# => загрузка базы c ipgeobase.ru
22
+
23
+ IpgeobaseLocal.load#предварительная загрузка базы в пямять
24
+
25
+ ip_meta = IpgeobaseLocal.lookup('10.11.12.134')
26
+
27
+ ip_meta.city # => Москва
28
+
29
+ ip_meta.country # => Россия
30
+ ## Usage with Rails
31
+
32
+ config/initializers/ipgeo.rb
33
+
34
+ IpgeobaseLocal.base_directory = "db/geo_files/"
35
+ IpgeobaseLocal.load
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/ipgeobase_local/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Anton Taraev"]
6
+ gem.email = ["anti191@gmail.com"]
7
+ gem.description = %q{geo ip}
8
+ gem.summary = %q{geo ip}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "ipgeobase_local"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = IpgeobaseLocal::VERSION
17
+
18
+ end
@@ -0,0 +1,17 @@
1
+ module IpgeobaseLocal
2
+ autoload 'Version', "ipgeobase_local/version"
3
+ autoload 'IpMetaData', 'ipgeobase_local/ip_meta_data'
4
+ autoload 'MemoryBase', 'ipgeobase_local/memory_base'
5
+ require 'ipgeobase_local/rails_config' if defined?(Rails)
6
+
7
+ class << self
8
+ attr_accessor :base_directory
9
+ end
10
+
11
+ def self.lookup(ip)
12
+ MemoryBase.parse(ip)
13
+ end
14
+ def self.load
15
+ MemoryBase.load unless defined?(Rake)
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ require 'iconv' unless String.new.respond_to?(:encode)
2
+
3
+ module IpgeobaseLocal
4
+ class IpMetaData
5
+
6
+ def initialize(country='', region='', city='')
7
+ @country = country
8
+ @region = region
9
+ @city = city
10
+ end
11
+
12
+ def city;@city;end
13
+ def country;@country;end
14
+ def region;@region;end
15
+
16
+ end
17
+ end
@@ -0,0 +1,83 @@
1
+ require 'singleton'
2
+ require 'ipaddr'
3
+ require 'csv'
4
+
5
+ module IpgeobaseLocal
6
+ class MemoryBase
7
+ include ::Singleton
8
+
9
+ def self.parse(ip)
10
+ instance.find(ip)
11
+ end
12
+
13
+ def self.load
14
+ instance
15
+ end
16
+
17
+ def initialize()
18
+ load_base unless defined?(Rake)
19
+ end
20
+
21
+ def find(ip)
22
+ ip = convert_ip(ip)
23
+ binary_find(ip)
24
+ end
25
+
26
+ def binary_find(ip)
27
+ max_index = @cidr_base.size - 1
28
+ min_index = 0
29
+ while min_index < max_index
30
+ current_index = (min_index + max_index)/2
31
+ current_index = current_index.round
32
+ if ip < @cidr_base[current_index][:start]
33
+ max_index = current_index
34
+ next
35
+ end
36
+ if ip > @cidr_base[current_index][:end]
37
+ min_index = current_index + 1
38
+ next
39
+ end
40
+ if (@cidr_base[current_index][:start] <= ip)&&( ip <= @cidr_base[current_index][:end])
41
+ return @cidr_base[current_index][:meta_data]
42
+ end
43
+ end
44
+ false
45
+ end
46
+
47
+ private
48
+
49
+ def load_base
50
+ city_base = {}
51
+ @cidr_base = []
52
+
53
+ cidr_file = "#{IpgeobaseLocal.base_directory}cidr_optim.txt"
54
+ cities_file = "#{IpgeobaseLocal.base_directory}cities.txt"
55
+
56
+ CSV.foreach(cities_file, col_sep: "\t", quote_char:"'", encoding: "windows-1251:utf-8") do |line|
57
+ city_base[line[0]] = line[1..5]
58
+ end
59
+
60
+ CSV.foreach(cidr_file, col_sep: "\t", quote_char:"'") do |line|
61
+ region = ''
62
+ city = ''
63
+ country = line[3]
64
+ unless line[4] == '-'
65
+ city_info = city_base[line[4]]
66
+ if city_info
67
+ city = city_info[0]
68
+ region = city_info[1]
69
+ end
70
+ end
71
+ @cidr_base << {start: line[0].to_i, end: line[1].to_i, meta_data: IpMetaData.new(country, region, city)}
72
+ end
73
+
74
+ end
75
+
76
+ def convert_ip(ip)
77
+ if ::IPAddr.new(ip).ipv4?
78
+ part = ip.split(".").map{|i| i.to_i}
79
+ 256**3*part[0]+256**2*part[1]+256*part[2]+part[3]
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,9 @@
1
+ module IpgeobaseLocal
2
+ class RailsConfig < Rails::Railtie
3
+
4
+ rake_tasks do
5
+ load "tasks/ipgeobase_local.rake"
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module IpgeobaseLocal
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: UTF-8
2
+ namespace :ipgeobase_local do
3
+
4
+ desc "Update base file form ipgeobase.ru"
5
+ task :update => :environment do
6
+ require 'net/http'
7
+ dir = "#{IpgeobaseLocal.base_directory}"
8
+ if dir.blank?
9
+ raise "Not set base directory"
10
+ end
11
+ uri = URI('http://ipgeobase.ru/files/db/Main/geo_files.tar.gz')
12
+ file_name = uri.path.split('/').last
13
+ file_name = [dir, file_name].join
14
+
15
+ FileUtils.mkdir_p "#{dir}"
16
+
17
+ Net::HTTP.start(uri.host, uri.port) do |http|
18
+ request = Net::HTTP::Get.new uri.request_uri
19
+ http.request request do |response|
20
+ open file_name, 'wb' do |io|
21
+ response.read_body do |chunk|
22
+ io.write chunk
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ system("tar -xzf #{file_name} -C #{dir}")
29
+ FileUtils.rm file_name
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ipgeobase_local
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anton Taraev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: geo ip
15
+ email:
16
+ - anti191@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - ipgeobase_local.gemspec
27
+ - lib/ipgeobase_local.rb
28
+ - lib/ipgeobase_local/ip_meta_data.rb
29
+ - lib/ipgeobase_local/memory_base.rb
30
+ - lib/ipgeobase_local/rails_config.rb
31
+ - lib/ipgeobase_local/version.rb
32
+ - lib/tasks/ipgeobase_local.rake
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: geo ip
57
+ test_files: []