geoip_redis 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54f140c92c744b0979451fa9dcddb338a230d90f
4
+ data.tar.gz: cce5774f92d358c484ce0251e2aef2c5a3163486
5
+ SHA512:
6
+ metadata.gz: 61f2320b292e8cdaa900aad00876e841db5c0f84243e42b81007c340da719e529adfef0a993d4bcd092bd7c1f2749654c5de70fe7f89d105c2b2a035b0681b51
7
+ data.tar.gz: f303d171adf7236e81831c580b3c0e61e1750c3a5fda915f8373a380e15dcd169e6a5ddfe1d377c32b70c342f93d752aa45b70c59d695329942cc41b3b78e21b
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Sergey Zabolotnov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,81 @@
1
+ # GeoipRedis
2
+ Puts MaxMind GeoIP2 database to Redis and allows to resolve country/city by ip. The main idea described [here](http://redis4you.com/articles.php?id=018&name=GeoIP+in+Redis).
3
+
4
+ ## Restrictions
5
+ * lagacy version of GeoIP database isn't supported
6
+ * working only with IPv4 database
7
+
8
+ ## Installation
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "geoip_redis"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install redis_geoip
22
+
23
+ ## Usage
24
+
25
+ Working with GeoIP2 Country database:
26
+ * load:
27
+
28
+ ```ruby
29
+ require "redis"
30
+ require "geoip_redis/country_loader"
31
+
32
+ redis = Redis.new(url: "redis://localhost:6379/4")
33
+ loader = GeoipRedis::CountryLoader.new(redis)
34
+
35
+ loader.load_blocks("path/to/blocks_ipv4.csv")
36
+ loader.load_locations("path/to/locations.csv")
37
+ ```
38
+
39
+ * resolve country by ip:
40
+
41
+ ```ruby
42
+ require "redis"
43
+ require "geoip_redis/resolver"
44
+
45
+ redis = Redis.new(url: "redis://localhost:6379/4")
46
+ resolver = GeoipRedis::Resolver.new(redis)
47
+
48
+ country = resolver.resolve("123.234.23.12")
49
+ ```
50
+
51
+ Working with GeoIP2 City database:
52
+ * load:
53
+
54
+ ```ruby
55
+ require "redis"
56
+ require "geoip_redis/city_loader"
57
+
58
+ redis = Redis.new(url: "redis://localhost:6379/5")
59
+ loader = GeoipRedis::CityLoader.new(redis)
60
+
61
+ loader.load_blocks("path/to/blocks_ipv4.csv")
62
+ loader.load_locations("path/to/locations.csv")
63
+ ```
64
+
65
+ * resolve city by ip:
66
+
67
+ ```ruby
68
+ require "redis"
69
+ require "geoip_redis/resolver"
70
+
71
+ redis = Redis.new(url: "redis://localhost:6379/5")
72
+ resolver = GeoipRedis::Resolver.new(redis)
73
+
74
+ city = resolver.resolve("123.234.23.12")
75
+ ```
76
+
77
+ ## Contributing
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/zabolotnov87/geoip_redis. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
79
+
80
+ ## Licence
81
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1 @@
1
+ require "geoip_redis/version"
@@ -0,0 +1,30 @@
1
+ require "geoip_redis/ip_range"
2
+
3
+ module GeoipRedis
4
+ class BlocksParser
5
+
6
+ CIDR = 0
7
+ GEONAME_ID = 1
8
+ private_constant :CIDR, :GEONAME_ID
9
+
10
+ def ip_range(data)
11
+ location_id = data[GEONAME_ID]
12
+ cidr = data[CIDR]
13
+ IpRange.build_from_network(cidr, location_id)
14
+ end
15
+
16
+ POSTAL_CODE = 6
17
+ LATITUDE = 7
18
+ LONGITUDE = 8
19
+ private_constant :POSTAL_CODE, :LATITUDE, :LONGITUDE
20
+
21
+ def location(data)
22
+ {
23
+ location_id: data[GEONAME_ID],
24
+ postal_code: data[POSTAL_CODE],
25
+ latitude: data[LATITUDE],
26
+ longitude: data[LONGITUDE],
27
+ }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ require "geoip_redis/store"
2
+ require "geoip_redis/csv_reader"
3
+ require "geoip_redis/blocks_parser"
4
+ require "geoip_redis/locations_parser"
5
+
6
+ module GeoipRedis
7
+ class CityLoader
8
+ def initialize(redis)
9
+ @store = Store.new(redis)
10
+ @csv_reader = CSVReader.new
11
+ @blocks_parser = BlocksParser.new
12
+ @locations_parser = LocationsParser.new
13
+ end
14
+
15
+ def load_blocks(path_to_blocks)
16
+ read_by_batch(path_to_blocks) do |rows|
17
+ ip_ranges = rows.map { |row| @blocks_parser.ip_range(row) }
18
+ @store.put_ip_ranges(ip_ranges)
19
+
20
+ locations = rows.map { |row| @blocks_parser.location(row) }
21
+ @store.put_locations(locations)
22
+ end
23
+ end
24
+
25
+ def load_locations(path_to_locations)
26
+ read_by_batch(path_to_locations) do |rows|
27
+ locations = rows.map { |row| @locations_parser.city(row) }
28
+ @store.put_locations(locations)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def read_by_batch(path_to_file)
35
+ File.open(path_to_file) do |file|
36
+ @csv_reader.read_by_batch(file) { |rows| yield rows }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ require "geoip_redis/store"
2
+ require "geoip_redis/csv_reader"
3
+ require "geoip_redis/blocks_parser"
4
+ require "geoip_redis/locations_parser"
5
+
6
+ module GeoipRedis
7
+ class CountryLoader
8
+ def initialize(redis)
9
+ @store = Store.new(redis)
10
+ @csv_reader = CSVReader.new
11
+ @blocks_parser = BlocksParser.new
12
+ @locations_parser = LocationsParser.new
13
+ end
14
+
15
+ def load_blocks(path_to_blocks)
16
+ read_by_batch(path_to_blocks) do |rows|
17
+ ip_ranges = rows.map { |row| @blocks_parser.ip_range(row) }
18
+ @store.put_ip_ranges(ip_ranges)
19
+ end
20
+ end
21
+
22
+ def load_locations(path_to_locations)
23
+ read_by_batch(path_to_locations) do |rows|
24
+ locations = rows.map { |row| @locations_parser.country(row) }
25
+ @store.put_locations(locations)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def read_by_batch(path_to_file, &block)
32
+ File.open(path_to_file) do |file|
33
+ @csv_reader.read_by_batch(file) { |rows| block.call(rows) }
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ require "csv"
2
+
3
+ module GeoipRedis
4
+ class CSVReader
5
+
6
+ HEADER = 1
7
+ private_constant :HEADER
8
+
9
+ def read_by_batch(io, batch_size: 1000, &block)
10
+ batch = []
11
+ CSV.new(io).each.lazy.drop(HEADER).each_with_index do |row, idx|
12
+ batch << row
13
+ if (idx + 1) % batch_size == 0
14
+ block.call(batch)
15
+ batch = []
16
+ end
17
+ end
18
+
19
+ block.call(batch) unless batch.empty?
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ require "ipaddr"
2
+
3
+ module GeoipRedis
4
+ IpRange = Struct.new(:location_id, :min_ip_num, :max_ip_num) do
5
+
6
+ def self.build_from_network(network, location_id)
7
+ ip_range = IPAddr.new(network).to_range
8
+ min_ip_num, max_ip_num = ip_range.first.to_i, ip_range.last.to_i
9
+ new(location_id, min_ip_num, max_ip_num)
10
+ end
11
+
12
+ def self.decode(encoded)
13
+ location_id, min_ip_num, max_ip_num = encoded.split(":")
14
+ min_ip_num, max_ip_num = min_ip_num.to_i, max_ip_num.to_i
15
+ new(location_id, min_ip_num, max_ip_num)
16
+ end
17
+
18
+ def encode
19
+ "#{location_id}:#{min_ip_num}:#{max_ip_num}"
20
+ end
21
+
22
+ def member?(ip_num)
23
+ (min_ip_num..max_ip_num).member?(ip_num)
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,38 @@
1
+ module GeoipRedis
2
+ class LocationsParser
3
+ def country(data)
4
+ geoname_id, locale_code, continent_code, continent_name,
5
+ country_iso_code, country_name = data
6
+
7
+ {
8
+ location_id: geoname_id,
9
+ locale_code: locale_code,
10
+ continent_code: continent_code,
11
+ continent_name: continent_name,
12
+ country_iso_code: country_iso_code,
13
+ country_name: country_name,
14
+ }
15
+ end
16
+
17
+ SUBDIVISION_1_ISO_CODE = 6
18
+ private_constant :SUBDIVISION_1_ISO_CODE
19
+
20
+ def city(data)
21
+ subdivision_1_iso_code, subdivision_1_name, subdivision_2_iso_code,
22
+ subdivision_2_name, city_name, metro_code, time_zone =
23
+ data[SUBDIVISION_1_ISO_CODE..-1]
24
+
25
+ location = {
26
+ subdivision_1_iso_code: subdivision_1_iso_code,
27
+ subdivision_1_name: subdivision_1_name,
28
+ subdivision_2_iso_code: subdivision_2_iso_code,
29
+ subdivision_2_name: subdivision_2_name,
30
+ city_name: city_name,
31
+ metro_code: metro_code,
32
+ time_zone: time_zone,
33
+ }
34
+
35
+ country(data).merge(location)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,33 @@
1
+ require "ipaddr"
2
+
3
+ require "geoip_redis/store"
4
+
5
+ module GeoipRedis
6
+ class Resolver
7
+ def initialize(redis)
8
+ @store = Store.new(redis)
9
+ end
10
+
11
+ def resolve(ip)
12
+ ip_num = normalize_ip(ip).to_i
13
+ ip_range = @store.find_ip_range(ip_num)
14
+
15
+ return if ip_range.nil? || !ip_range.member?(ip_num)
16
+
17
+ @store.find_location(ip_range.location_id)
18
+ end
19
+
20
+ private
21
+
22
+ def normalize_ip(ip)
23
+ case ip
24
+ when String
25
+ IPAddr.new(ip)
26
+ when Numeric
27
+ IPAddr.new(ip, Socket::AF_INET)
28
+ else
29
+ ip
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,53 @@
1
+ require "redis"
2
+
3
+ require "geoip_redis/ip_range"
4
+
5
+ module GeoipRedis
6
+ class Store
7
+ IP_RANGES_KEY = "geoip:ipranges"
8
+ LOCATIONS_KEY = "geoip:locations"
9
+
10
+ def initialize(redis)
11
+ @redis = redis
12
+ end
13
+
14
+ def put_ip_ranges(ip_ranges)
15
+ @redis.pipelined do
16
+ ip_ranges.each { |ip_range| put_ip_range(ip_range) }
17
+ end
18
+ end
19
+
20
+ def find_ip_range(ip_num)
21
+ encoded_ip_range = @redis.zrangebyscore(
22
+ IP_RANGES_KEY, ip_num, "+inf", limit: [0, 1]).first
23
+
24
+ IpRange.decode(encoded_ip_range) if encoded_ip_range
25
+ end
26
+
27
+ def put_locations(locations)
28
+ @redis.pipelined do
29
+ locations.each { |location| put_location(location) }
30
+ end
31
+ end
32
+
33
+ def find_location(location_id)
34
+ @redis.hgetall location_key(location_id)
35
+ end
36
+
37
+ private
38
+
39
+ def put_ip_range(ip_range)
40
+ @redis.zadd(IP_RANGES_KEY, ip_range.max_ip_num, ip_range.encode)
41
+ end
42
+
43
+ def put_location(location)
44
+ location_id = location.fetch(:location_id)
45
+ location.delete(:location_id)
46
+ @redis.hmset location_key(location_id), location.flatten
47
+ end
48
+
49
+ def location_key(location_id)
50
+ "#{LOCATIONS_KEY}:#{location_id}"
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module GeoipRedis
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geoip_redis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Zabolotnov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ description: Put MaxMind GeoIP2 database to Redis
84
+ email:
85
+ - sergey.zabolotnov@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE.txt
91
+ - README.md
92
+ - lib/geoip_redis.rb
93
+ - lib/geoip_redis/blocks_parser.rb
94
+ - lib/geoip_redis/city_loader.rb
95
+ - lib/geoip_redis/country_loader.rb
96
+ - lib/geoip_redis/csv_reader.rb
97
+ - lib/geoip_redis/ip_range.rb
98
+ - lib/geoip_redis/locations_parser.rb
99
+ - lib/geoip_redis/resolver.rb
100
+ - lib/geoip_redis/store.rb
101
+ - lib/geoip_redis/version.rb
102
+ homepage: https://github.com/zabolotnov87/geoip_redis
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.5.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Put MaxMind GeoIP2 database to Redis
126
+ test_files: []
127
+ has_rdoc: