Commit 5f4b782

mo khan <mo@mokhan.ca>
2016-04-30 04:57:17
use geocoder to search coordinates by address
1 parent ea86df6
Changed files (4)
app/models/location.rb
@@ -1,4 +1,6 @@
 class Location
   def self.from(address, city, state, country)
+    results = Geocoder.search("#{address}, #{city}, #{state}, #{country}")
+    results.any? ? results.first.coordinates : [nil, nil]
   end
 end
spec/models/location_spec.rb
@@ -0,0 +1,12 @@
+require 'rails_helper'
+
+describe Location do
+  describe '.from' do
+    it 'returns the correct lat/long' do
+      latitude, longitude = Location.
+        from('1817 Crowchild Trail NW', 'Calgary', 'AB', 'Canada')
+      expect(latitude).to be_within(0.1).of(51.0706973)
+      expect(longitude).to be_within(0.1).of(-114.1178249)
+    end
+  end
+end
Gemfile
@@ -21,6 +21,7 @@ source 'https://rubygems.org' do
   gem 'font-awesome-rails'
   gem 'foreman', group: :development
   gem 'foundation-rails', '~> 5.5'
+  gem 'geocoder'
   gem 'griddler'
   gem 'griddler-mandrill'
   gem 'groupdate'
Gemfile.lock
@@ -148,6 +148,7 @@ GEM
     foundation-rails (5.5.3.2)
       railties (>= 3.1.0)
       sass (>= 3.3.0, < 3.5)
+    geocoder (1.3.4)
     gherkin (3.2.0)
     git-version-bump (0.15.1)
     globalid (0.3.6)
@@ -395,6 +396,7 @@ DEPENDENCIES
   font-awesome-rails!
   foreman!
   foundation-rails (~> 5.5)!
+  geocoder!
   griddler!
   griddler-mandrill!
   groupdate!