-
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.
close #2045 improve adding venue to product admin
- Loading branch information
1 parent
d5fea78
commit a5c3476
Showing
9 changed files
with
171 additions
and
90 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
59 changes: 59 additions & 0 deletions
59
app/controllers/spree/api/v2/platform/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,59 @@ | ||
module Spree | ||
module Api | ||
module V2 | ||
module Platform | ||
class PlacesController < 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::GooglePlacesFetcher.call(name: name_filter) | ||
|
||
if new_places.success? | ||
struct_places_with_id(new_places.google_places) | ||
else | ||
[] | ||
end | ||
end | ||
end | ||
|
||
def model_class | ||
SpreeCmCommissioner::Place | ||
end | ||
|
||
def resource_serializer | ||
SpreeCmCommissioner::Api::V2::Platform::PlaceSerializer | ||
end | ||
|
||
def collection_serializer | ||
SpreeCmCommissioner::Api::V2::Platform::PlaceSerializer | ||
end | ||
|
||
private | ||
|
||
Place = Struct.new(:id, :name, :base_64_content) | ||
def struct_places_with_id(places) | ||
places.map do |place| | ||
Place.new( | ||
id: place.reference, | ||
name: place.name, | ||
base_64_content: Base64.strict_encode64(place.to_json) | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
41 changes: 41 additions & 0 deletions
41
app/interactors/spree_cm_commissioner/google_places_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 GooglePlacesFetcher < BaseInteractor | ||
delegate :name, to: :context | ||
|
||
def call | ||
context.google_places = [] | ||
json_response = fetch_google_place_by_name | ||
|
||
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_place_by_name | ||
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 |
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
11 changes: 11 additions & 0 deletions
11
app/serializers/spree_cm_commissioner/api/v2/platform/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 PlaceSerializer < ::Spree::Api::V2::Platform::BaseSerializer | ||
attributes :name, :base_64_content | ||
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