Skip to content

Commit

Permalink
Use GOOGLE_MAP_API_KEY when loading JS source
Browse files Browse the repository at this point in the history
Google Maps recently changed their billing model which resulted in an API key always being required when using the Javascript API.

If you want to see the map in development mode then set GOOGLE_MAP_API_KEY in $APP_ROOT/.env.local or change the one in $APP_ROOT/.env.development.
  • Loading branch information
thehenster committed Aug 23, 2018
1 parent b26961f commit 3dc26fd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GOOGLE_MAP_API_KEY=fake-google-map-api-key
7 changes: 7 additions & 0 deletions app/helpers/google_maps_api_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module GoogleMapsApiHelper
def google_maps_api_source_url
api_key = ENV.fetch('GOOGLE_MAP_API_KEY')

"https://maps.googleapis.com/maps/api/js?key=#{api_key}&region=GB&libraries=geometry"
end
end
2 changes: 1 addition & 1 deletion app/views/locators/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@


<% content_for :body_end do %>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?region=GB&libraries=geometry"></script>
<%= javascript_include_tag google_maps_api_source_url %>
<% end %>
11 changes: 11 additions & 0 deletions spec/helpers/google_maps_api_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'rails_helper'

RSpec.describe GoogleMapsApiHelper, '#google_maps_api_source_url' do
it 'returns a URL which includes the API key' do
allow(ENV).to receive(:fetch).with('GOOGLE_MAP_API_KEY').and_return('fake-key')

expect(google_maps_api_source_url).to eq(
'https://maps.googleapis.com/maps/api/js?key=fake-key&region=GB&libraries=geometry'
)
end
end

0 comments on commit 3dc26fd

Please sign in to comment.