-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add 'details' command Includes: - Refactoring Google Maps API piece into separate file and class - Updating Sorbet - Updating README - Adding tests and related VCR cassettes * Replace triple equals with more suitable options * Fix rescue statement * Refactor possible responses as private methods
- Loading branch information
Showing
12 changed files
with
1,235 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# typed: ignore | ||
# frozen_string_literal: true | ||
|
||
require_relative './google_maps_api' | ||
|
||
module Commands | ||
class Details < GoogleMapsAPI | ||
extend T::Sig | ||
|
||
sig { override.returns(String) } | ||
def self.name | ||
'details' | ||
end | ||
|
||
sig { override.returns(String) } | ||
def self.help | ||
<<~HELP | ||
details <place>: returns available info about a specific place listed on Google Maps. | ||
If there are more than one match, the command returns a list of the top 10 results instead. | ||
Examples: | ||
details living computers museum, seattle wa | ||
details korean restaurant victoria bc | ||
HELP | ||
end | ||
|
||
sig { override.returns(String) } | ||
def response_body | ||
return api_key_missing unless api_key_exists? | ||
|
||
return help if arg_text.empty? | ||
|
||
begin | ||
places = Google::Maps.places(arg_text) | ||
place = Google::Maps.place(places.first.place_id) | ||
rescue Google::Maps::ZeroResultsException | ||
return "NO RESULTS FOR \"#{arg_text}\"" | ||
rescue => error | ||
return error.message | ||
end | ||
|
||
return list_of_places(places) if places.length > 1 | ||
|
||
build_response(place) | ||
end | ||
|
||
private | ||
|
||
sig { params(place: Google::Maps::PlaceDetails).returns(String) } | ||
def build_response(place) | ||
place_name = place.name | ||
place_address = place.address | ||
data = place.data | ||
data_phone = data.formatted_phone_number ? "\nPhone: #{data.formatted_phone_number}" : '' | ||
data_rating = data.rating ? "\nRating: #{data.rating}" : '' | ||
data_price_level = data.price_level ? "\nPrice level (0\{free\}-4): #{data.price_level}" : '' | ||
data_permanently_closed = data.permanently_closed ? 'PERMAMENTLY CLOSED' : '' | ||
data_hours = data.opening_hours | ||
open_now = defined?(data_hours.open_now) ? (data_hours.open_now ? "\nOpen now" : "\nClosed now") : '' | ||
weekday_hours = defined?(data_hours.weekday_text) ? | ||
"\nHours:\n* #{data.opening_hours.weekday_text.join(%Q{\n* })}" | ||
: | ||
'' | ||
|
||
"#{place_name}\n#{place_address}#{data_phone}#{data_rating}"\ | ||
"#{data_price_level}#{data_permanently_closed}#{open_now}#{weekday_hours}" | ||
end | ||
|
||
sig { params(places: T::Array[Google::Maps::Place]).returns(String) } | ||
def list_of_places(places) | ||
places.slice(0, 10).map.with_index { |place, index| "(#{index + 1}) #{place}" }.join(%Q{\n}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# typed: ignore | ||
# frozen_string_literal: true | ||
|
||
require_relative './base' | ||
require 'google-maps' | ||
|
||
Google::Maps.configure do |config| | ||
config.authentication_mode = Google::Maps::Configuration::API_KEY | ||
config.api_key = ENV['GOOGLE_MAPS_API_KEY'] | ||
end | ||
|
||
module Commands | ||
class GoogleMapsAPI < Base | ||
private | ||
|
||
def api_key_missing | ||
<<~MSG | ||
Your Google Maps API key is missing. | ||
Create a new key at: https://developers.google.com/maps/gmp-get-started | ||
Once you get a key, set it to ENV['GOOGLE_MAPS_API_KEY'] on your server. | ||
MSG | ||
end | ||
|
||
def api_key_exists? | ||
ENV.key?('GOOGLE_MAPS_API_KEY') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.