-
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 7c74577
Showing
9 changed files
with
164 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
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
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 ||= fetch_places | ||
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 | ||
|
||
def fetch_places | ||
filter = params[:filter] || {} | ||
name_filter = filter[:name_i_cont] | ||
|
||
new_places = SpreeCmCommissioner::GooglePlacesFetcher.call(name: name_filter) | ||
new_places.success? ? struct_places_with_id(new_places.google_places) : [] | ||
end | ||
|
||
def struct_places_with_id(places) | ||
places.map do |place| | ||
place_struct = Struct.new(:id, :name, :base_64_content) | ||
place_struct.new( | ||
id: place.reference, | ||
name: place.name, | ||
base_64_content: Base64.strict_encode64(place.to_json) | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
module SpreeCmCommissioner | ||
class GooglePlacesFetcher < BaseInteractor | ||
delegate :name, to: :context | ||
|
||
def call | ||
json_response = fetch_google_place_by_name | ||
|
||
context.google_places = json_response['results'].map do |json| | ||
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'] | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
def google_map_key | ||
@google_map_key ||= ENV.fetch('GOOGLE_MAP_KEY') | ||
end | ||
|
||
def fetch_google_place_by_name | ||
queries = { | ||
query: name, | ||
key: google_map_key | ||
} | ||
response = Faraday.get("#{GOOGLE_MAP_API_URL}/place/textsearch/json", queries) | ||
JSON.parse(response.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