geo_magic 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +112 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/geo_magic.gemspec +92 -0
- data/lib/geo_magic.rb +4 -0
- data/lib/geo_magic/calculate.rb +39 -0
- data/lib/geo_magic/distance.rb +26 -0
- data/lib/geo_magic/location.rb +102 -0
- data/lib/geo_magic/meta.rb +10 -0
- data/lib/geo_magic/point.rb +23 -0
- data/lib/geo_magic/remote.rb +44 -0
- data/lib/geo_magic/util.rb +16 -0
- data/spec/geo_magic_calculate_spec.rb +63 -0
- data/spec/geo_magic_include_calc_spec.rb +36 -0
- data/spec/geo_magic_include_remote_spec.rb +31 -0
- data/spec/geo_magic_plane_dist_spec.rb +17 -0
- data/spec/geo_magic_remote_spec.rb +21 -0
- data/spec/spec_helper.rb +12 -0
- metadata +210 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--format nested --color
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem 'geo-distance', '>= 0.1.0'
|
7
|
+
gem 'httparty', '>= 0.6.0'
|
8
|
+
|
9
|
+
# Add dependencies to develop your gem here.
|
10
|
+
# Include everything needed to run rake, tests, features, etc.
|
11
|
+
group :development do
|
12
|
+
gem "rspec", ">= 2.3.0"
|
13
|
+
gem "bundler", "~> 1.0.0"
|
14
|
+
gem "jeweler", "~> 1.5.2"
|
15
|
+
gem "rcov", ">= 0"
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
crack (0.1.8)
|
5
|
+
diff-lcs (1.1.2)
|
6
|
+
geo-distance (0.1.0)
|
7
|
+
git (1.2.5)
|
8
|
+
httparty (0.6.1)
|
9
|
+
crack (= 0.1.8)
|
10
|
+
jeweler (1.5.2)
|
11
|
+
bundler (~> 1.0.0)
|
12
|
+
git (>= 1.2.5)
|
13
|
+
rake
|
14
|
+
rake (0.8.7)
|
15
|
+
rcov (0.9.9)
|
16
|
+
rspec (2.4.0)
|
17
|
+
rspec-core (~> 2.4.0)
|
18
|
+
rspec-expectations (~> 2.4.0)
|
19
|
+
rspec-mocks (~> 2.4.0)
|
20
|
+
rspec-core (2.4.0)
|
21
|
+
rspec-expectations (2.4.0)
|
22
|
+
diff-lcs (~> 1.1.2)
|
23
|
+
rspec-mocks (2.4.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.0.0)
|
30
|
+
geo-distance (>= 0.1.0)
|
31
|
+
httparty (>= 0.6.0)
|
32
|
+
jeweler (~> 1.5.2)
|
33
|
+
rcov
|
34
|
+
rspec (>= 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Kristian Mandrup
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
= Geo Magic
|
2
|
+
|
3
|
+
This little clever project was the result of me not being satisfied with existing Geo Location and Coding solutions out there to suit my needs.
|
4
|
+
Many of the projects did not return quality data (fx when requesting IP or location) and others were tightly bound to Active Record and SQL databases for calculations etc.
|
5
|
+
|
6
|
+
This little gem should get you going...
|
7
|
+
|
8
|
+
* Get IP and location data using freegeoip.net
|
9
|
+
* Calculate of distance between map points using haversine (supports multiple distance units)
|
10
|
+
|
11
|
+
You can either include the complete library like this:
|
12
|
+
|
13
|
+
require 'geo_magic'
|
14
|
+
|
15
|
+
Or require only the functionality you need (see usage examples below)
|
16
|
+
|
17
|
+
== IP and location data
|
18
|
+
|
19
|
+
Uses freegeoip.net JSON API
|
20
|
+
|
21
|
+
RSpec 2 examples of API usage
|
22
|
+
|
23
|
+
require 'geo_magic/remote'
|
24
|
+
|
25
|
+
it "should get the remote IP address" do
|
26
|
+
ip = GeoMagic::Remote.my_ip
|
27
|
+
puts "ip: #{ip.inspect}"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should get other location" do
|
31
|
+
location = GeoMagic::Remote.location_of '74.200.247.59'
|
32
|
+
puts "location: #{location['city']}"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should get my location" do
|
36
|
+
location = GeoMagic::Remote.my_location
|
37
|
+
puts location
|
38
|
+
puts "location: #{location['city']}"
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
== Distance calculation
|
43
|
+
|
44
|
+
Uses haversine formula
|
45
|
+
|
46
|
+
RSpec 2 examples of API usage
|
47
|
+
|
48
|
+
require 'geo_magic/calculate'
|
49
|
+
|
50
|
+
it "calculates distance using array args" do
|
51
|
+
dist = GeoMagic::Calculate.distance [@long1, @lat1], [@long2, @lat2]
|
52
|
+
puts dist
|
53
|
+
end
|
54
|
+
|
55
|
+
it "calculates distance using Point args" do
|
56
|
+
from_point = GeoMagic::Point.new @long1, @lat1
|
57
|
+
to_point = GeoMagic::Point.new @long2, @lat2
|
58
|
+
|
59
|
+
puts "from: #{from_point}, to: #{to_point}"
|
60
|
+
|
61
|
+
dist = GeoMagic::Calculate.distance from_point, to_point
|
62
|
+
puts dist
|
63
|
+
end
|
64
|
+
|
65
|
+
it "calculates distance using Hash args (short)" do
|
66
|
+
from_point = GeoMagic::Point.new(@long1, @lat1).to_hash :short
|
67
|
+
to_point = GeoMagic::Point.new(@long2, @lat2).to_hash :short
|
68
|
+
|
69
|
+
puts "from: #{from_point}, to: #{to_point}"
|
70
|
+
|
71
|
+
dist = GeoMagic::Calculate.distance from_point, to_point
|
72
|
+
puts dist
|
73
|
+
end
|
74
|
+
|
75
|
+
== Meta magic
|
76
|
+
|
77
|
+
You can also include the functionality directly into any class like this
|
78
|
+
|
79
|
+
class Map
|
80
|
+
include GeoMagic::Calculate
|
81
|
+
end
|
82
|
+
|
83
|
+
class MapCalc
|
84
|
+
geo_magic :calc
|
85
|
+
end
|
86
|
+
|
87
|
+
it "calculates distance using array args" do
|
88
|
+
dist = Map.distance [@long1, @lat1], [@long2, @lat2]
|
89
|
+
puts dist
|
90
|
+
end
|
91
|
+
|
92
|
+
it "calculates distance using array args" do
|
93
|
+
dist = MapCalc.distance [@long1, @lat1], [@long2, @lat2]
|
94
|
+
puts dist
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
== Contributing to geo_magic
|
99
|
+
|
100
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
101
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
102
|
+
* Fork the project
|
103
|
+
* Start a feature/bugfix branch
|
104
|
+
* Commit and push until you are happy with your contribution
|
105
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
106
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
107
|
+
|
108
|
+
== Copyright
|
109
|
+
|
110
|
+
Copyright (c) 2011 Kristian Mandrup. See LICENSE.txt for
|
111
|
+
further details.
|
112
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "geo_magic"
|
16
|
+
gem.homepage = "http://github.com/kristianmandrup/geo_magic"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Get your IP and location data and calculate distances on the globe}
|
19
|
+
gem.description = %Q{Get IP and location data using freegeoip.net - can also calculate of distance between map points using haversine supporting multiple distance units}
|
20
|
+
gem.email = "kmandrup@gmail.com"
|
21
|
+
gem.authors = ["Kristian Mandrup"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
|
25
|
+
gem.add_runtime_dependency 'geo-distance', '>= 0.1.0'
|
26
|
+
gem.add_runtime_dependency 'httparty', '>= 0.6.0'
|
27
|
+
|
28
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
29
|
+
end
|
30
|
+
Jeweler::RubygemsDotOrgTasks.new
|
31
|
+
|
32
|
+
require 'rspec/core'
|
33
|
+
require 'rspec/core/rake_task'
|
34
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
35
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
36
|
+
end
|
37
|
+
|
38
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
39
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
40
|
+
spec.rcov = true
|
41
|
+
end
|
42
|
+
|
43
|
+
task :default => :spec
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "geo_magic #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/geo_magic.gemspec
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{geo_magic}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kristian Mandrup"]
|
12
|
+
s.date = %q{2011-01-13}
|
13
|
+
s.description = %q{Get IP and location data using freegeoip.net - can also calculate of distance between map points using haversine supporting multiple distance units}
|
14
|
+
s.email = %q{kmandrup@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"geo_magic.gemspec",
|
29
|
+
"lib/geo_magic.rb",
|
30
|
+
"lib/geo_magic/calculate.rb",
|
31
|
+
"lib/geo_magic/distance.rb",
|
32
|
+
"lib/geo_magic/location.rb",
|
33
|
+
"lib/geo_magic/meta.rb",
|
34
|
+
"lib/geo_magic/point.rb",
|
35
|
+
"lib/geo_magic/remote.rb",
|
36
|
+
"lib/geo_magic/util.rb",
|
37
|
+
"spec/geo_magic_calculate_spec.rb",
|
38
|
+
"spec/geo_magic_include_calc_spec.rb",
|
39
|
+
"spec/geo_magic_include_remote_spec.rb",
|
40
|
+
"spec/geo_magic_plane_dist_spec.rb",
|
41
|
+
"spec/geo_magic_remote_spec.rb",
|
42
|
+
"spec/spec_helper.rb"
|
43
|
+
]
|
44
|
+
s.homepage = %q{http://github.com/kristianmandrup/geo_magic}
|
45
|
+
s.licenses = ["MIT"]
|
46
|
+
s.require_paths = ["lib"]
|
47
|
+
s.rubygems_version = %q{1.3.7}
|
48
|
+
s.summary = %q{Get your IP and location data and calculate distances on the globe}
|
49
|
+
s.test_files = [
|
50
|
+
"spec/geo_magic_calculate_spec.rb",
|
51
|
+
"spec/geo_magic_include_calc_spec.rb",
|
52
|
+
"spec/geo_magic_include_remote_spec.rb",
|
53
|
+
"spec/geo_magic_plane_dist_spec.rb",
|
54
|
+
"spec/geo_magic_remote_spec.rb",
|
55
|
+
"spec/spec_helper.rb"
|
56
|
+
]
|
57
|
+
|
58
|
+
if s.respond_to? :specification_version then
|
59
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
60
|
+
s.specification_version = 3
|
61
|
+
|
62
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
63
|
+
s.add_runtime_dependency(%q<geo-distance>, [">= 0.1.0"])
|
64
|
+
s.add_runtime_dependency(%q<httparty>, [">= 0.6.0"])
|
65
|
+
s.add_development_dependency(%q<rspec>, [">= 2.3.0"])
|
66
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
67
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
68
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
69
|
+
s.add_runtime_dependency(%q<geo-distance>, [">= 0.1.0"])
|
70
|
+
s.add_runtime_dependency(%q<httparty>, [">= 0.6.0"])
|
71
|
+
else
|
72
|
+
s.add_dependency(%q<geo-distance>, [">= 0.1.0"])
|
73
|
+
s.add_dependency(%q<httparty>, [">= 0.6.0"])
|
74
|
+
s.add_dependency(%q<rspec>, [">= 2.3.0"])
|
75
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
76
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
77
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
78
|
+
s.add_dependency(%q<geo-distance>, [">= 0.1.0"])
|
79
|
+
s.add_dependency(%q<httparty>, [">= 0.6.0"])
|
80
|
+
end
|
81
|
+
else
|
82
|
+
s.add_dependency(%q<geo-distance>, [">= 0.1.0"])
|
83
|
+
s.add_dependency(%q<httparty>, [">= 0.6.0"])
|
84
|
+
s.add_dependency(%q<rspec>, [">= 2.3.0"])
|
85
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
86
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
87
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
88
|
+
s.add_dependency(%q<geo-distance>, [">= 0.1.0"])
|
89
|
+
s.add_dependency(%q<httparty>, [">= 0.6.0"])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
data/lib/geo_magic.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'geo-distance'
|
2
|
+
require 'geo_magic/point'
|
3
|
+
require 'geo_magic/util'
|
4
|
+
require 'geo_magic/location'
|
5
|
+
|
6
|
+
module GeoMagic
|
7
|
+
module Calculate #:nodoc:
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def distance from_point, to_point, options = { :unit => :meters }
|
14
|
+
points = extract_points from_point, to_point
|
15
|
+
puts "points: #{points.inspect}"
|
16
|
+
dist = ::GeoDistance.distance( *points )[options[:unit]]
|
17
|
+
puts "the distance from (#{points[0]}, #{points[1]}) to (#{points[2]}, #{points[3]}) is: #{dist}"
|
18
|
+
dist.number
|
19
|
+
end
|
20
|
+
|
21
|
+
def plane_distance from_point, to_point, options = { :unit => :meters }
|
22
|
+
points = extract_points from_point, to_point
|
23
|
+
Math.sqrt((points[0] - points[2] + points[1] - points[3]).abs)
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def extract_points from_point, to_point
|
29
|
+
[extract_point(from_point), extract_point(to_point)].flatten.map(&:to_f)
|
30
|
+
end
|
31
|
+
|
32
|
+
def extract_point point
|
33
|
+
GeoMagic::Util.extract_point point
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
extend ClassMethods
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module GeoDistance
|
2
|
+
class Haversine < DistanceFormula
|
3
|
+
def self.point_distance( point_a, point_b, units = :meters )
|
4
|
+
points = GeoMagic::Util.extract_points point_a, point_b
|
5
|
+
distance *points, units
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module GeoDistance
|
11
|
+
class Spherical < DistanceFormula
|
12
|
+
def self.point_distance( point_a, point_b, units = :meters )
|
13
|
+
points = GeoMagic::Util.extract_points point_a, point_b
|
14
|
+
distance *points, units
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module GeoDistance
|
20
|
+
class Vincenty < DistanceFormula
|
21
|
+
def self.point_distance( point_a, point_b, units = :meters )
|
22
|
+
points = GeoMagic::Util.extract_points point_a, point_b
|
23
|
+
distance *points, units
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module GeoMagic
|
2
|
+
class Location #:nodoc:
|
3
|
+
attr_accessor :latitude, :longitude, :ip
|
4
|
+
attr_writer :city, :region, :country
|
5
|
+
|
6
|
+
def initialize raw_location
|
7
|
+
raw_location.each_pair do |key, value|
|
8
|
+
@city_name = value and next if key == 'city'
|
9
|
+
send :"#{key}=", value
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def [] key
|
14
|
+
send key
|
15
|
+
end
|
16
|
+
|
17
|
+
class City
|
18
|
+
attr_accessor :name, :metrocode, :zipcode
|
19
|
+
|
20
|
+
def initialize name, zipcode, metrocode
|
21
|
+
@name = name
|
22
|
+
@zipcode = zipcode
|
23
|
+
@metrocode = metrocode
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
"city: #{name}#{zip_str}#{metro_str}"
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def zip_str
|
33
|
+
", zipcode: #{zipcode}" if !zipcode.empty?
|
34
|
+
end
|
35
|
+
|
36
|
+
def metro_str
|
37
|
+
", metrocode: #{metrocode}" if !metrocode.empty?
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Region
|
42
|
+
attr_accessor :name, :code
|
43
|
+
|
44
|
+
def initialize name, code
|
45
|
+
@name = name
|
46
|
+
@code = code
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_s
|
50
|
+
"region: #{name}, code: #{code}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Country
|
55
|
+
attr_accessor :name, :code
|
56
|
+
|
57
|
+
def initialize name, code
|
58
|
+
@name = name
|
59
|
+
@code = code
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_s
|
63
|
+
"country: #{name}, code: #{code}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def map_point
|
68
|
+
@map_point ||= GeoMagic::Point.new latitude, longitude
|
69
|
+
end
|
70
|
+
|
71
|
+
def city
|
72
|
+
@city ||= City.new city_name, zipcode, metrocode
|
73
|
+
end
|
74
|
+
|
75
|
+
def region
|
76
|
+
@region ||= Region.new region_name, region_code
|
77
|
+
end
|
78
|
+
|
79
|
+
def country
|
80
|
+
@country ||= Country.new country_name, country_code
|
81
|
+
end
|
82
|
+
|
83
|
+
def to_hash mode= :long
|
84
|
+
end
|
85
|
+
|
86
|
+
def to_s
|
87
|
+
"latitude: #{latitude}, longitude #{longitude}#{ip_str}\n#{city}\n#{region}\n#{country}"
|
88
|
+
end
|
89
|
+
|
90
|
+
protected
|
91
|
+
|
92
|
+
attr_accessor :country_name, :country_code
|
93
|
+
attr_accessor :region_code, :region_name
|
94
|
+
attr_accessor :metrocode, :zipcode, :city_name
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def ip_str
|
99
|
+
", :ip #{ip}" if ip
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module GeoMagic
|
2
|
+
class Point #:nodoc:
|
3
|
+
attr_accessor :latitude, :longitude
|
4
|
+
|
5
|
+
def initialize latitude, longitude
|
6
|
+
@latitude = latitude
|
7
|
+
@longitude = longitude
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_hash mode= :long
|
11
|
+
case mode
|
12
|
+
when :short
|
13
|
+
{:lat => latitude, :long => longitude}
|
14
|
+
else
|
15
|
+
{:latitude => latitude, :longitude => longitude}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
"(lat: #{latitude}, long: #{longitude})"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'geo_magic/point'
|
3
|
+
require 'geo_magic/location'
|
4
|
+
|
5
|
+
module GeoMagic
|
6
|
+
module Remote #:nodoc:
|
7
|
+
# Get the remote location of the request IP using http://hostip.info
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend(ClassMethods)
|
11
|
+
end
|
12
|
+
|
13
|
+
module HostIP
|
14
|
+
def self.my_ip
|
15
|
+
response = HTTParty.get('http://api.hostip.info/get_html.php')
|
16
|
+
ip = response.split("\n")
|
17
|
+
ip.last.gsub /IP:\s+/, ''
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def my_ip
|
23
|
+
response = HTTParty.get('http://freegeoip.net/json/')
|
24
|
+
response.parsed_response['ip']
|
25
|
+
end
|
26
|
+
|
27
|
+
def my_location mode = :location
|
28
|
+
create_location HTTParty.get('http://freegeoip.net/json/')
|
29
|
+
end
|
30
|
+
|
31
|
+
def location_of ip
|
32
|
+
create_location HTTParty.get("http://freegeoip.net/json/#{ip}")
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def create_location response
|
38
|
+
GeoMagic::Location.new response.parsed_response
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
extend ClassMethods
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module GeoMagic
|
2
|
+
module Util #:nodoc:
|
3
|
+
def self.extract_point point
|
4
|
+
case point
|
5
|
+
when Hash
|
6
|
+
[ point[:lat] || point[:latitude], point[:long] || point[:longitude] ]
|
7
|
+
when GeoMagic::Point
|
8
|
+
[point.latitude, point.longitude]
|
9
|
+
when GeoMagic::Location
|
10
|
+
[point.latitude, point.longitude]
|
11
|
+
when Array
|
12
|
+
[point[0], point[1]]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'geo_magic'
|
3
|
+
|
4
|
+
describe "GeoMagic Calculate" do
|
5
|
+
before do
|
6
|
+
@long1 = -104.88544
|
7
|
+
@lat1 = 39.06546
|
8
|
+
|
9
|
+
@long2 = -104.80
|
10
|
+
@lat2 = @lat1
|
11
|
+
end
|
12
|
+
|
13
|
+
it "calculates distance using array args - using algorithm haversine" do
|
14
|
+
dist = GeoMagic::Calculate.distance [@long1, @lat1], [@long2, @lat2]
|
15
|
+
puts dist
|
16
|
+
end
|
17
|
+
|
18
|
+
it "Changing default distance algorithm to vincenty" do
|
19
|
+
GeoDistance.default_algorithm = :vincenty
|
20
|
+
dist = GeoMagic::Calculate.distance [@long1, @lat1], [@long2, @lat2]
|
21
|
+
puts dist
|
22
|
+
end
|
23
|
+
|
24
|
+
it "calculates distance using Point args" do
|
25
|
+
from_point = GeoMagic::Point.new @long1, @lat1
|
26
|
+
to_point = GeoMagic::Point.new @long2, @lat2
|
27
|
+
|
28
|
+
puts "from: #{from_point}, to: #{to_point}"
|
29
|
+
|
30
|
+
dist = GeoMagic::Calculate.distance from_point, to_point
|
31
|
+
puts dist
|
32
|
+
end
|
33
|
+
|
34
|
+
it "calculates distance using Location arg from my location" do
|
35
|
+
from_point = GeoMagic::Remote.my_location
|
36
|
+
to_point = GeoMagic::Point.new @long2, @lat2
|
37
|
+
|
38
|
+
puts "from: #{from_point}, to: #{to_point}"
|
39
|
+
|
40
|
+
dist = GeoMagic::Calculate.distance from_point, to_point
|
41
|
+
puts dist
|
42
|
+
end
|
43
|
+
|
44
|
+
it "calculates distance using Hash args (short)" do
|
45
|
+
from_point = GeoMagic::Point.new(@long1, @lat1).to_hash :short
|
46
|
+
to_point = GeoMagic::Point.new(@long2, @lat2).to_hash :short
|
47
|
+
|
48
|
+
puts "from: #{from_point}, to: #{to_point}"
|
49
|
+
|
50
|
+
dist = GeoMagic::Calculate.distance from_point, to_point
|
51
|
+
puts dist
|
52
|
+
end
|
53
|
+
|
54
|
+
it "calculates distance using Hash args (long)" do
|
55
|
+
from_point = GeoMagic::Point.new(@long1, @lat1)
|
56
|
+
to_point = GeoMagic::Point.new(@long2, @lat2)
|
57
|
+
|
58
|
+
puts "from: #{from_point}, to: #{to_point}"
|
59
|
+
|
60
|
+
dist = GeoMagic::Calculate.distance from_point, to_point
|
61
|
+
puts dist
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'geo_magic'
|
3
|
+
|
4
|
+
class Map
|
5
|
+
include GeoMagic::Calculate
|
6
|
+
end
|
7
|
+
|
8
|
+
class MapCalc
|
9
|
+
geo_magic :calc
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
describe "GeoMagic" do
|
14
|
+
before do
|
15
|
+
@long1 = -104.88544
|
16
|
+
@lat1 = 39.06546
|
17
|
+
|
18
|
+
@long2 = -104.80
|
19
|
+
@lat2 = @lat1
|
20
|
+
end
|
21
|
+
|
22
|
+
it "calculates distance using array args" do
|
23
|
+
dist = Map.distance [@long1, @lat1], [@long2, @lat2]
|
24
|
+
puts dist
|
25
|
+
end
|
26
|
+
|
27
|
+
it "calculates distance using Point args" do
|
28
|
+
from_point = GeoMagic::Point.new @long1, @lat1
|
29
|
+
to_point = GeoMagic::Point.new @long2, @lat2
|
30
|
+
|
31
|
+
puts "from: #{from_point}, to: #{to_point}"
|
32
|
+
|
33
|
+
dist = Map.distance from_point, to_point
|
34
|
+
puts dist
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'geo_magic'
|
3
|
+
|
4
|
+
class Locate
|
5
|
+
include GeoMagic::Remote
|
6
|
+
end
|
7
|
+
|
8
|
+
class Finder
|
9
|
+
geo_magic :remote
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
describe "GeoMagic Remote" do
|
14
|
+
it "should get the remote IP address" do
|
15
|
+
ip = Locate.my_ip
|
16
|
+
puts "ip: #{ip.inspect}"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should get other location" do
|
20
|
+
location = Finder.location_of '74.200.247.59'
|
21
|
+
puts "location: #{location['city']}"
|
22
|
+
puts "location: #{location[:city]}"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should get my location" do
|
26
|
+
location = Finder.my_location
|
27
|
+
puts location
|
28
|
+
puts "location: #{location.city}"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'geo_magic'
|
3
|
+
|
4
|
+
describe "GeoMagic Calculate" do
|
5
|
+
before do
|
6
|
+
@long1 = -104.88544
|
7
|
+
@lat1 = 39.06546
|
8
|
+
|
9
|
+
@long2 = -104.80
|
10
|
+
@lat2 = @lat1
|
11
|
+
end
|
12
|
+
|
13
|
+
it "calculates distance using array args" do
|
14
|
+
dist = GeoMagic::Calculate.plane_distance [@long1, @lat1], [@long2, @lat2]
|
15
|
+
puts dist
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'geo_magic'
|
3
|
+
require 'geo_magic/remote'
|
4
|
+
|
5
|
+
describe "GeoMagic Remote" do
|
6
|
+
it "should get the remote IP address" do
|
7
|
+
ip = GeoMagic::Remote.my_ip
|
8
|
+
puts "ip: #{ip.inspect}"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should get other location" do
|
12
|
+
location = GeoMagic::Remote.location_of '74.200.247.59'
|
13
|
+
puts "location:\n#{location}"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should get my location" do
|
17
|
+
location = GeoMagic::Remote.my_location
|
18
|
+
puts "location:\n#{location}"
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'geo_magic'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geo_magic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kristian Mandrup
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-13 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: geo-distance
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
version: 0.1.0
|
32
|
+
type: :runtime
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: httparty
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
- 6
|
45
|
+
- 0
|
46
|
+
version: 0.6.0
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: rspec
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 2
|
59
|
+
- 3
|
60
|
+
- 0
|
61
|
+
version: 2.3.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: bundler
|
67
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ~>
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 1
|
74
|
+
- 0
|
75
|
+
- 0
|
76
|
+
version: 1.0.0
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *id004
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: jeweler
|
82
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 1
|
89
|
+
- 5
|
90
|
+
- 2
|
91
|
+
version: 1.5.2
|
92
|
+
type: :development
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: *id005
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: rcov
|
97
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: *id006
|
108
|
+
- !ruby/object:Gem::Dependency
|
109
|
+
name: geo-distance
|
110
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
- 1
|
118
|
+
- 0
|
119
|
+
version: 0.1.0
|
120
|
+
type: :runtime
|
121
|
+
prerelease: false
|
122
|
+
version_requirements: *id007
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
name: httparty
|
125
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
segments:
|
131
|
+
- 0
|
132
|
+
- 6
|
133
|
+
- 0
|
134
|
+
version: 0.6.0
|
135
|
+
type: :runtime
|
136
|
+
prerelease: false
|
137
|
+
version_requirements: *id008
|
138
|
+
description: Get IP and location data using freegeoip.net - can also calculate of distance between map points using haversine supporting multiple distance units
|
139
|
+
email: kmandrup@gmail.com
|
140
|
+
executables: []
|
141
|
+
|
142
|
+
extensions: []
|
143
|
+
|
144
|
+
extra_rdoc_files:
|
145
|
+
- LICENSE.txt
|
146
|
+
- README.rdoc
|
147
|
+
files:
|
148
|
+
- .document
|
149
|
+
- .rspec
|
150
|
+
- Gemfile
|
151
|
+
- Gemfile.lock
|
152
|
+
- LICENSE.txt
|
153
|
+
- README.rdoc
|
154
|
+
- Rakefile
|
155
|
+
- VERSION
|
156
|
+
- geo_magic.gemspec
|
157
|
+
- lib/geo_magic.rb
|
158
|
+
- lib/geo_magic/calculate.rb
|
159
|
+
- lib/geo_magic/distance.rb
|
160
|
+
- lib/geo_magic/location.rb
|
161
|
+
- lib/geo_magic/meta.rb
|
162
|
+
- lib/geo_magic/point.rb
|
163
|
+
- lib/geo_magic/remote.rb
|
164
|
+
- lib/geo_magic/util.rb
|
165
|
+
- spec/geo_magic_calculate_spec.rb
|
166
|
+
- spec/geo_magic_include_calc_spec.rb
|
167
|
+
- spec/geo_magic_include_remote_spec.rb
|
168
|
+
- spec/geo_magic_plane_dist_spec.rb
|
169
|
+
- spec/geo_magic_remote_spec.rb
|
170
|
+
- spec/spec_helper.rb
|
171
|
+
has_rdoc: true
|
172
|
+
homepage: http://github.com/kristianmandrup/geo_magic
|
173
|
+
licenses:
|
174
|
+
- MIT
|
175
|
+
post_install_message:
|
176
|
+
rdoc_options: []
|
177
|
+
|
178
|
+
require_paths:
|
179
|
+
- lib
|
180
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
181
|
+
none: false
|
182
|
+
requirements:
|
183
|
+
- - ">="
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
hash: 2192296416920126926
|
186
|
+
segments:
|
187
|
+
- 0
|
188
|
+
version: "0"
|
189
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
190
|
+
none: false
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
segments:
|
195
|
+
- 0
|
196
|
+
version: "0"
|
197
|
+
requirements: []
|
198
|
+
|
199
|
+
rubyforge_project:
|
200
|
+
rubygems_version: 1.3.7
|
201
|
+
signing_key:
|
202
|
+
specification_version: 3
|
203
|
+
summary: Get your IP and location data and calculate distances on the globe
|
204
|
+
test_files:
|
205
|
+
- spec/geo_magic_calculate_spec.rb
|
206
|
+
- spec/geo_magic_include_calc_spec.rb
|
207
|
+
- spec/geo_magic_include_remote_spec.rb
|
208
|
+
- spec/geo_magic_plane_dist_spec.rb
|
209
|
+
- spec/geo_magic_remote_spec.rb
|
210
|
+
- spec/spec_helper.rb
|