geo-calculator 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.
@@ -0,0 +1,126 @@
1
+ module Geocoder
2
+ module Store
3
+ module Base
4
+
5
+ ##
6
+ # Is this object geocoded? (Does it have latitude and longitude?)
7
+ #
8
+ def geocoded?
9
+ to_coordinates.compact.size > 0
10
+ end
11
+
12
+ ##
13
+ # Coordinates [lat,lon] of the object.
14
+ #
15
+ def to_coordinates
16
+ [:latitude, :longitude].map{ |i| send self.class.geocoder_options[i] }
17
+ end
18
+
19
+ ##
20
+ # Calculate the distance from the object to an arbitrary point.
21
+ # See Geocoder::Calculations.distance_between for ways of specifying
22
+ # the point. Also takes a symbol specifying the units
23
+ # (:mi or :km; can be specified in Geocoder configuration).
24
+ #
25
+ def distance_to(point, units = nil)
26
+ units ||= self.class.geocoder_options[:units]
27
+ return nil unless geocoded?
28
+ Geocoder::Calculations.distance_between(
29
+ to_coordinates, point, :units => units)
30
+ end
31
+
32
+ alias_method :distance_from, :distance_to
33
+
34
+ ##
35
+ # Calculate the bearing from the object to another point.
36
+ # See Geocoder::Calculations.distance_between for
37
+ # ways of specifying the point.
38
+ #
39
+ def bearing_to(point, options = {})
40
+ options[:method] ||= self.class.geocoder_options[:method]
41
+ return nil unless geocoded?
42
+ Geocoder::Calculations.bearing_between(
43
+ to_coordinates, point, options)
44
+ end
45
+
46
+ ##
47
+ # Calculate the bearing from another point to the object.
48
+ # See Geocoder::Calculations.distance_between for
49
+ # ways of specifying the point.
50
+ #
51
+ def bearing_from(point, options = {})
52
+ options[:method] ||= self.class.geocoder_options[:method]
53
+ return nil unless geocoded?
54
+ Geocoder::Calculations.bearing_between(
55
+ point, to_coordinates, options)
56
+ end
57
+
58
+ ##
59
+ # Get nearby geocoded objects.
60
+ # Takes the same options hash as the near class method (scope).
61
+ # Returns nil if the object is not geocoded.
62
+ #
63
+ def nearbys(radius = 20, options = {})
64
+ return nil unless geocoded?
65
+ options.merge!(:exclude => self) unless send(self.class.primary_key).nil?
66
+ self.class.near(self, radius, options)
67
+ end
68
+
69
+ ##
70
+ # Look up coordinates and assign to +latitude+ and +longitude+ attributes
71
+ # (or other as specified in +geocoded_by+). Returns coordinates (array).
72
+ #
73
+ def geocode
74
+ fail
75
+ end
76
+
77
+ ##
78
+ # Look up address and assign to +address+ attribute (or other as specified
79
+ # in +reverse_geocoded_by+). Returns address (string).
80
+ #
81
+ def reverse_geocode
82
+ fail
83
+ end
84
+
85
+ private # --------------------------------------------------------------
86
+
87
+ ##
88
+ # Look up geographic data based on object attributes (configured in
89
+ # geocoded_by or reverse_geocoded_by) and handle the results with the
90
+ # block (given to geocoded_by or reverse_geocoded_by). The block is
91
+ # given two-arguments: the object being geocoded and an array of
92
+ # Geocoder::Result objects).
93
+ #
94
+ def do_lookup(reverse = false)
95
+ options = self.class.geocoder_options
96
+ if reverse and options[:reverse_geocode]
97
+ query = to_coordinates
98
+ elsif !reverse and options[:geocode]
99
+ query = send(options[:user_address])
100
+ else
101
+ return
102
+ end
103
+
104
+ query_options = [:lookup, :ip_lookup, :language].inject({}) do |hash, key|
105
+ if options.has_key?(key)
106
+ val = options[key]
107
+ hash[key] = val.respond_to?(:call) ? val.call(self) : val
108
+ end
109
+ hash
110
+ end
111
+ results = Geocoder.search(query, query_options)
112
+
113
+ # execute custom block, if specified in configuration
114
+ block_key = reverse ? :reverse_block : :geocode_block
115
+ if custom_block = options[block_key]
116
+ custom_block.call(self, results)
117
+
118
+ # else execute block passed directly to this method,
119
+ # which generally performs the "auto-assigns"
120
+ elsif block_given?
121
+ yield(self, results)
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,3 @@
1
+ module GeoCalculator
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ class TestClass < ActiveRecord::Base
4
+ include Geocoder::Store::ActiveRecord
5
+
6
+ def self.geocoder_options
7
+ {
8
+ latitude: 'latitude',
9
+ longitude: 'longitude',
10
+ units: :mi
11
+ }
12
+ end
13
+ end
14
+
15
+ describe GeoCalculator do
16
+ let(:latlng) { [34.095809, -118.287558] }
17
+ it 'makes sure you can mix in the methods' do
18
+ expect(TestClass).to respond_to(:near)
19
+ expect(TestClass.near(latlng)).to be_a(Array)
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ require 'geo-calculator'
2
+ require 'byebug'
3
+ require 'active_record'
4
+
5
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3",
6
+ database: ":memory:")
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geo-calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan MacInnes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Use ruby geocoder calculation methods without geocoding
98
+ email:
99
+ - ryan.macinnes@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rbenv-version"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - geo-calculator.gemspec
111
+ - lib/geo-calculator.rb
112
+ - lib/geo-calculator/calculations.rb
113
+ - lib/geo-calculator/configuration.rb
114
+ - lib/geo-calculator/configuration_hash.rb
115
+ - lib/geo-calculator/hash_recursive_merge.rb
116
+ - lib/geo-calculator/sql.rb
117
+ - lib/geo-calculator/store/active_record.rb
118
+ - lib/geo-calculator/store/base.rb
119
+ - lib/geo-calculator/version.rb
120
+ - spec/includes_spec.rb
121
+ - spec/spec_helper.rb
122
+ homepage: https://github.com/goddamnyouryan/geo-calculator
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.4.5
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: This gem uses the ruby geocoder gem calculation methods to allow calculation
146
+ of nearby locations without needing all the geocoding code.
147
+ test_files:
148
+ - spec/includes_spec.rb
149
+ - spec/spec_helper.rb