Skip to content

Instantly share code, notes, and snippets.

@guanhui07
Created October 15, 2019 06:27
Show Gist options
  • Save guanhui07/48094946ebf0191c1161159c56cdc193 to your computer and use it in GitHub Desktop.
Save guanhui07/48094946ebf0191c1161159c56cdc193 to your computer and use it in GitHub Desktop.
ruby_fakeaddressgenerator
require 'curb'
require 'json'
require 'pry'
TOKEN = 'AIzaSyBH8rt__HdxDXldlreAzCL6ZxsyRk0mJXk'
class PlaceSearch
attr_accessor :token
def initialize(token)
@token = token
end
def search_random_nearly_address(query)
location = get_place_location(query)
loop {
random_location = get_random_location(location)
geocode_answer = get_geocode_answer(random_location)
place = create_place_object(geocode_answer)
return place if place.address?
}
end
def get_random_location(location)
lat = get_random_value(location.lat)
lng = get_random_value(location.lng)
Location.new(lat, lng)
end
def get_random_value(value)
plus = Random.rand(2)
new_value = Random.rand(0.01)
return value + new_value if plus
return value - new_value unless plus
end
def get_geocode_answer(location)
http = Curl::Easy.new(get_geocode_url(location))
http.perform
JSON.parse(http.body_str)['results'].first
end
def get_geocode_url(location)
"https://maps.googleapis.com/maps/api/geocode/json?latlng=#{location.lat},#{location.lng}&key=#{@token}"
end
def search_place(query)
binding.pry
location = get_place_location(query)
nearest_places = get_nearest_places(location)
places = get_places(nearest_places)
binding.pry
end
def get_place_location(query)
http = Curl::Easy.new(get_text_search_url(query))
http.perform
get_location_from_text_search(http.body_str)
end
def get_places(nearest_places)
places = Array.new
nearest_places.each{ |near_place|
place_details = get_place_details(near_place['place_id'])
places << create_place_object(place_details)
}
places
end
def create_place_object(near_place)
place = Place.new
place.name = near_place['name']
place.adr_address = near_place['adr_address']
place.formatted_address = near_place['formatted_address']
place.international_phone_number = near_place['international_phone_number']
place.formatted_phone_number = near_place['formatted_phone_number']
place.place_id = near_place['place_id']
place.url = near_place['url']
place.vicinity = near_place['vicinity']
place.location = Location.new(near_place['geometry']['location']['lat'], near_place['geometry']['location']['lng'])
place.postal_code = get_postal_code(near_place['address_components'])
place
end
def get_postal_code(address_components)
address_components.each { |address_component|
return address_component['long_name'] if address_component['types'].include?('postal_code')
}
nil
end
def get_place_details(placeid)
http = Curl::Easy.new(get_place_details_url(placeid))
http.perform
JSON.parse(http.body_str)['result']
end
def get_text_search_url(query)
URI.escape("https://maps.googleapis.com/maps/api/place/textsearch/json?query=#{query}&key=#{@token}")
end
def get_location_from_text_search(response)
responseJson = JSON.parse(response)
location = responseJson['results'].first['geometry']['location']
Location.new(location['lat'], location['lng'])
end
def get_nearest_places(location)
http = Curl::Easy.new(get_near_search_url(location))
http.perform
JSON.parse(http.body_str)['results']
end
def get_near_search_url(location)
URI.escape("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=#{location.lat},#{location.lng}&rankby=distance&key=#{@token}")
end
def get_place_details_url(placeid)
URI.escape("https://maps.googleapis.com/maps/api/place/details/json?placeid=#{placeid}&key=#{@token}")
end
end
class Location
attr_accessor :lat, :lng
def initialize(lat, lng)
@lat = lat
@lng = lng
end
end
class Place
attr_accessor :location, :adr_address, :formatted_address, :international_phone_number, :formatted_phone_number, :name, :place_id, :url, :vicinity, :postal_code
def address?
@formatted_address && !formatted_address.empty?
end
end
#PlaceSearch.new(TOKEN).search_place('Беларусь Борисов 222512')
binding.pry
address = PlaceSearch.new(TOKEN).search_random_nearly_address('Беларусь Минск Народная 23')
binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment