-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP#2045 add nearby places suggestion
- Loading branch information
1 parent
d5fea78
commit 5f9dc0f
Showing
9 changed files
with
223 additions
and
21 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
66 changes: 66 additions & 0 deletions
66
app/controllers/spree/api/v2/platform/product_places_controller.rb
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,66 @@ | ||
module Spree | ||
module Api | ||
module V2 | ||
module Platform | ||
class ProductPlacesController < ResourceController | ||
respond_to :json | ||
|
||
def index | ||
result = collection_serializer.new(collection).serializable_hash | ||
|
||
respond_with(result) do |format| | ||
format.json { render json: result } | ||
end | ||
end | ||
|
||
def collection | ||
@collection ||= begin | ||
filter = params[:filter] || {} | ||
name_filter = filter[:name_i_cont] | ||
|
||
new_places = SpreeCmCommissioner::GooglePlaceFetcher.call(name: name_filter) | ||
|
||
if new_places.success? | ||
place_struct(new_places.google_places) | ||
else | ||
[] | ||
end | ||
end | ||
end | ||
|
||
def model_class | ||
SpreeCmCommissioner::ProductPlace | ||
end | ||
|
||
def resource_serializer | ||
SpreeCmCommissioner::Api::V2::Platform::ProductPlaceSerializer | ||
end | ||
|
||
def collection_serializer | ||
SpreeCmCommissioner::Api::V2::Platform::ProductPlaceSerializer | ||
end | ||
|
||
private | ||
|
||
def place_struct(places) | ||
places.each_with_index.map do |place| | ||
PlaceWithId.new( | ||
place.name, | ||
place.reference, | ||
place.name, | ||
place.vicinity, | ||
place.lat, | ||
place.lon, | ||
place.icon, | ||
place.rating, | ||
place.types | ||
) | ||
end | ||
end | ||
|
||
PlaceWithId = Struct.new(:id, :reference, :name, :vicinity, :lat, :lon, :icon, :rating, :types) | ||
end | ||
end | ||
end | ||
end | ||
end |
41 changes: 41 additions & 0 deletions
41
app/interactors/spree_cm_commissioner/google_place_fetcher.rb
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,41 @@ | ||
module SpreeCmCommissioner | ||
class GooglePlaceFetcher < BaseInteractor | ||
delegate :vendor, :name, to: :context | ||
def call | ||
context.google_places = [] | ||
json_response = fetch_google_places | ||
|
||
json_response['results'].each_with_index do |json, _| | ||
place = SpreeCmCommissioner::Place.new( | ||
reference: json['reference'], | ||
types: json['types'].blank? ? '' : json['types'][0], | ||
name: json['name'], | ||
vicinity: json['vicinity'], | ||
lat: json['geometry']['location']['lat'], | ||
lon: json['geometry']['location']['lng'], | ||
icon: json['icon'], | ||
rating: json['rating'] | ||
) | ||
|
||
context.google_places << place | ||
end | ||
end | ||
|
||
private | ||
|
||
def google_map_key | ||
@google_map_key ||= ENV.fetch('GOOGLE_MAP_KEY') | ||
end | ||
|
||
def fetch_google_places | ||
url = URI("#{GOOGLE_MAP_API_URL}/place/textsearch/json?query=#{name}&key=#{google_map_key}") | ||
|
||
https = Net::HTTP.new(url.host, url.port) | ||
https.use_ssl = true | ||
|
||
request = Net::HTTP::Get.new(url) | ||
response = https.request(request) | ||
JSON.parse(response.read_body) | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
app/serializers/spree_cm_commissioner/api/v2/platform/product_place_serializer.rb
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,11 @@ | ||
module SpreeCmCommissioner | ||
module Api | ||
module V2 | ||
module Platform | ||
class ProductPlaceSerializer < ::Spree::Api::V2::Platform::BaseSerializer | ||
attributes :id, :name, :reference, :lat, :lon, :rating, :types | ||
end | ||
end | ||
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
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