pin_point 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.
- checksums.yaml +15 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/lib/pin_point/ip_block.rb +46 -0
- data/lib/pin_point/railtie.rb +12 -0
- data/lib/pin_point/tasks/pin_point.rake +111 -0
- data/lib/pin_point/version.rb +3 -0
- data/lib/pin_point.rb +15 -0
- data/pin_point.gemspec +27 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YzM2NjVlMmVlY2IxZmYwNDc1MDhhODQ5OWY2YzY2ZmNkZjVjMDFhNA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YTU1ZmY2NDEzZDYyNjQ4ZjEzMjc1YmMyZDg2YWZjNjBiYzdmNDczYw==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NmEwMTRkZGI5OWMxZTdiOWVhODNlZWZjNDI5MjZhZDQ4MjhmMTdjZjgyZDE3
|
10
|
+
NjcwYTc3OWY0ZThiNTQ2YzdiZWMxNzAwMDgzOTNlODQ3YWQ2MWMyMjNhZjcz
|
11
|
+
YzAzNjNmZjZjNGU5ZjEwMGNiYzdmMjMxZDhiZmYwNjMzMTFiZmU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YzRhYzYyZjEyZDk5MjU3NzQwYTVkOTVhOGU2MjBmYjQzMmY1NjczM2UyZTg1
|
14
|
+
MmNmNDQyYWVkYzk0NGMxYmMzYjgwMmRiZjA1Y2YwY2I1MTkwMTU3NzFkYWNk
|
15
|
+
ZDQ5NzI5NTc1M2RiMDIyZDAyODZjNWQ1ZTUzZjU5MmQ3MWE1MGY=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Adam Ross Cohen, John Cihocki
|
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,51 @@
|
|
1
|
+
# PinPoint
|
2
|
+
|
3
|
+
Pinpoint a users location by their IP address.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'pin_point'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install pin_point
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
To import the IP Block and location mapping data just run the provided rake task in your apps root directory:
|
22
|
+
|
23
|
+
$ rake pin_point:import
|
24
|
+
|
25
|
+
Be aware that this imports over 2 million IP Blocks and then maps each of them to a location. This operation takes a while ( 30+ minutes ), even on fast machines.
|
26
|
+
|
27
|
+
Once the data is all imported you can look up blocks based on IP strings:
|
28
|
+
|
29
|
+
``` ruby
|
30
|
+
PinPoint.ip '173.194.43.1'
|
31
|
+
=># <PinPoint::IpBlock _id: 51e5ec3fbe8a5cbd5417c2ee,
|
32
|
+
# range_low: 2915172352,
|
33
|
+
# range_high: 2915197695,
|
34
|
+
# location: 2703,
|
35
|
+
# coordinates: [-122.0574, 37.4192],
|
36
|
+
# city: "Mountain View",
|
37
|
+
# state: "CA",
|
38
|
+
# country: "US">
|
39
|
+
```
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
See LICENSE.txt
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
module PinPoint
|
4
|
+
|
5
|
+
class IpBlock
|
6
|
+
|
7
|
+
include Mongoid::Document
|
8
|
+
|
9
|
+
field :range_low, type: Integer
|
10
|
+
field :range_high, type: Integer
|
11
|
+
field :location, type: Integer
|
12
|
+
field :coordinates, type: Array, default: []
|
13
|
+
field :city, type: String
|
14
|
+
field :state, type: String
|
15
|
+
field :country, type: String
|
16
|
+
|
17
|
+
index range_low: -1
|
18
|
+
index location: 1
|
19
|
+
|
20
|
+
class << self
|
21
|
+
|
22
|
+
def for_ip ip
|
23
|
+
val = ip_to_int ip
|
24
|
+
maybe = where( :range_low.lte => val ).order_by( [[:range_low, :desc]] ).first
|
25
|
+
if maybe.try( :range_high ).to_i >= val
|
26
|
+
return maybe
|
27
|
+
end
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def ip_to_int ip
|
34
|
+
val = 0
|
35
|
+
ip.split( "." ).inject( 3 ) do |exp, octet|
|
36
|
+
val += (octet.to_i * (256 ** exp))
|
37
|
+
next exp - 1
|
38
|
+
end
|
39
|
+
val
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'net/http'
|
3
|
+
require 'zip/zipfilesystem'
|
4
|
+
|
5
|
+
namespace :pin_point do
|
6
|
+
|
7
|
+
task :import => :environment do
|
8
|
+
|
9
|
+
TIME = Time.now.to_i
|
10
|
+
ZIP_FILE = "/tmp/pin_point_data_#{TIME}.zip"
|
11
|
+
BLOCK_FILE = "/tmp/pin_point_block_#{TIME}.csv"
|
12
|
+
LOCATION_FILE = "/tmp/pin_point_location_#{TIME}.csv"
|
13
|
+
REMOTE_DATA_DOMAIN = 'geolite.maxmind.com'
|
14
|
+
REMOTE_DATA_PATH = '/download/geoip/database/GeoLiteCity_CSV/GeoLiteCity-latest.zip'
|
15
|
+
|
16
|
+
puts "Sourcing data..."
|
17
|
+
|
18
|
+
Net::HTTP.start REMOTE_DATA_DOMAIN do |http|
|
19
|
+
response = http.get REMOTE_DATA_PATH
|
20
|
+
open ZIP_FILE, 'wb' do |file|
|
21
|
+
file.write response.body
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
puts "Unzipping..."
|
26
|
+
|
27
|
+
Zip::ZipFile.open ZIP_FILE do |zipfile|
|
28
|
+
zipfile.each do |file|
|
29
|
+
case file.name
|
30
|
+
when /block/i
|
31
|
+
zipfile.extract file.name, BLOCK_FILE
|
32
|
+
when /location/i
|
33
|
+
zipfile.extract file.name, LOCATION_FILE
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
puts "Removing old IP Block data..."
|
39
|
+
|
40
|
+
PinPoint::IpBlock.delete_all
|
41
|
+
|
42
|
+
puts "Dropping existing indexes..."
|
43
|
+
|
44
|
+
PinPoint::IpBlock.remove_indexes
|
45
|
+
|
46
|
+
puts "Generating new indexes..."
|
47
|
+
|
48
|
+
PinPoint::IpBlock.create_indexes
|
49
|
+
|
50
|
+
puts "Importing IP Block data. This will take a while..."
|
51
|
+
|
52
|
+
import_start = ip_block_start = Time.now
|
53
|
+
open BLOCK_FILE, 'r' do |file|
|
54
|
+
while line = file.gets
|
55
|
+
if $. > 2
|
56
|
+
begin
|
57
|
+
row = CSV.parse_line( line.force_encoding('UTF-8').chars.select{|i| i.valid_encoding?}.join, col_sep: ',' )
|
58
|
+
rescue CSV::MalformedCSVError
|
59
|
+
Rails.logger.debug "Failed to parse: #{line}\n#{$!.message}"
|
60
|
+
else
|
61
|
+
PinPoint::IpBlock.create({
|
62
|
+
range_low: row[0].to_i,
|
63
|
+
range_high: row[1].to_i,
|
64
|
+
location: row[2].to_i
|
65
|
+
})
|
66
|
+
end
|
67
|
+
if ($. - 2) % 100000 == 0
|
68
|
+
now = Time.now
|
69
|
+
current_count = ($. - 2)
|
70
|
+
per_second = ($. - 2) / ( ( now - ip_block_start ) / 1.second )
|
71
|
+
seconds = ( now - ip_block_start ) % 1.minute
|
72
|
+
minutes = ( now - ip_block_start ) / 1.minute
|
73
|
+
puts "%d blocks imported in %d minutes, %d seconds. %.2f blocks/s ..." % [ current_count, minutes, seconds, per_second ]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
puts "Mapping location data to IP Blocks..."
|
80
|
+
|
81
|
+
location_start = Time.now
|
82
|
+
open LOCATION_FILE, 'r' do |file|
|
83
|
+
while line = file.gets
|
84
|
+
if $. > 2
|
85
|
+
begin
|
86
|
+
row = CSV.parse_line( line.force_encoding('UTF-8').chars.select{|i| i.valid_encoding?}.join, col_sep: ',' )
|
87
|
+
rescue CSV::MalformedCSVError
|
88
|
+
Rails.logger.debug "Failed to parse: #{line}\n#{$!.message}"
|
89
|
+
else
|
90
|
+
PinPoint::IpBlock.where( location: row[0].to_i ).update_all({
|
91
|
+
country: row[1],
|
92
|
+
state: row[2],
|
93
|
+
city: row[3],
|
94
|
+
coordinates: [row[6].to_f, row[5].to_f]
|
95
|
+
})
|
96
|
+
end
|
97
|
+
if ($. - 2) % 100000 == 0
|
98
|
+
now = Time.now
|
99
|
+
current_count = ($. - 2)
|
100
|
+
per_second = ($. - 2) / ( ( now - location_start ) / 1.second )
|
101
|
+
seconds = ( now - location_start ) % 1.minute
|
102
|
+
minutes = ( now - location_start ) / 1.minute
|
103
|
+
puts "%d locations mapped in %d minutes, %d seconds. %.2f locations/s ..." % [ current_count, minutes, seconds, per_second ]
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
data/lib/pin_point.rb
ADDED
data/pin_point.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pin_point/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pin_point"
|
8
|
+
spec.version = PinPoint::VERSION
|
9
|
+
spec.authors = ["Adam Ross Cohen", "John Cihocki"]
|
10
|
+
spec.email = ["ross@startupgiraffe.com", "john@startupgiraffe.com"]
|
11
|
+
spec.description = %q{Pinpoint a users location by their IP address.}
|
12
|
+
spec.summary = %q{pin_point imports publicly available data about IP Blocks and their corresponding locations into mongodb. Once the dataset is imported lookups by IP address return associated location data.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "mongoid"
|
22
|
+
spec.add_dependency "rake"
|
23
|
+
spec.add_dependency "rubyzip"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pin_point
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Ross Cohen
|
8
|
+
- John Cihocki
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ! '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ! '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rubyzip
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: bundler
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '1.3'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '1.3'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rake
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ! '>='
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
description: Pinpoint a users location by their IP address.
|
85
|
+
email:
|
86
|
+
- ross@startupgiraffe.com
|
87
|
+
- john@startupgiraffe.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/pin_point.rb
|
98
|
+
- lib/pin_point/ip_block.rb
|
99
|
+
- lib/pin_point/railtie.rb
|
100
|
+
- lib/pin_point/tasks/pin_point.rake
|
101
|
+
- lib/pin_point/version.rb
|
102
|
+
- pin_point.gemspec
|
103
|
+
homepage: ''
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.0.5
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: pin_point imports publicly available data about IP Blocks and their corresponding
|
127
|
+
locations into mongodb. Once the dataset is imported lookups by IP address return
|
128
|
+
associated location data.
|
129
|
+
test_files: []
|