如何在 If Else 语句检查中分配一个局部变量,然后可以在所述 If Else块中使用该变量。这就是我的意思:
def try_if_else(foo)
if bar = other_method(foo) == true
do_something_with_bar(bar)
else
do_something_else(foo)
end
end
这是我试图开始工作的实际方法,但没有任何成功:
def try_to_find_site(location)
if (response = findplacefromtext_textquery(location)[:status]) == 'OK'
convert_findplacefromtext_to_struct(response)
elsif (response = findplacefromtext_phonenumber(location)[:status]) == 'OK'
convert_findplacefromtext_to_struct(response)
elsif (response = textsearch(location)[:status]) == 'OK'
convert_textsearch_to_struct(response)
else
[LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
end
这是我设法开始工作的,但这并不理想,因为它两次调用外部 API 调用以返回我正在寻找的结果:
def try_to_find_site(location)
if findplacefromtext_textquery(location)[:status] == 'OK'
convert_findplacefromtext_to_struct(findplacefromtext_textquery(location))
elsif findplacefromtext_phonenumber(location)[:status] == 'OK'
convert_findplacefromtext_to_struct(findplacefromtext_phonenumber(location))
elsif textsearch(location)[:status] == 'OK'
convert_textsearch_to_struct(textsearch(location))
else
[LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
end
如果需要查看我要执行的操作,这是整个文件:
# Handles consuming the Google Api.
# require 'httparty'
module ApiHandlers
module GoogleMaps
class GoogleBusinessFinder
include HTTParty
debug_output $stdout
base_uri 'https://maps.googleapis.com'
default_params key: Rails.application.credentials.development[:google][:secret_key]
def initialize(location)
@location = location
end
def call
try_to_find_site(location)
end
# THESE BE PRIVATE Matey! YARR!!
private
LocationStruct = Struct.new(:name, :formatted_address, :place_id, :rating)
# Tries various the various (findplacefromtext_textquery, findplacefromtext_phonenumber, textsearch)
# methods of finding the business and returns an array of structs with findings.
def try_to_find_site(location)
if findplacefromtext_textquery(location)[:status] == 'OK'
convert_findplacefromtext_to_struct(findplacefromtext_textquery(location))
elsif findplacefromtext_phonenumber(location)[:status] == 'OK'
convert_findplacefromtext_to_struct(findplacefromtext_phonenumber(location))
elsif textsearch(location)[:status] == 'OK'
convert_textsearch_to_struct(textsearch(location))
else
[LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end
end
def convert_findplacefromtext_to_struct(response)
response = response[:candidates][0]
[LocationStruct.new(response[:name], response[:formatted_address], response[:place_id], response[:rating])]
end
def convert_textsearch_to_struct(response)
response = response[:results]
response.map { |response| LocationStruct.new(response[:name], response[:formatted_address], response[:place_id], response[:rating]) }
end
# Tries to find the business using the business name and address
def findplacefromtext_textquery(location)
@options = {
query: { inputtype: 'textquery',
input: "#{location.location_name} #{readable_address(location)}",
fields: 'name,formatted_address,name,types,rating,place_id,id' }
}
self.class.get('/maps/api/place/findplacefromtext/json', @options).parsed_response.deep_symbolize_keys
end
# Tries to find the business using the business phone number
def findplacefromtext_phonenumber(location)
@options = {
query: { inputtype: 'phonenumber',
input: "+1#{location.phone_number}",
fields: 'name,formatted_address,name,types,rating,place_id' }
}
self.class.get('/maps/api/place/findplacefromtext/json', @options).parsed_response.deep_symbolize_keys
end
# Finds an array of businesses that match the parameters. Last chance to find it.
def textsearch(location)
@options = {
query: { query: "#{location.location_name} #{location.city} #{location.country}",
fields: 'name,formatted_address,name,types,rating,place_id,id' }
}
self.class.get('/maps/api/place/textsearch/json', @options).parsed_response.deep_symbolize_keys
end
def readable_address(location)
"#{location.address_line_1} #{location.city} #{location.region} #{location.country} #{location.postal_code}"
end
attr_reader :location
# is equal to:
# def location
# @location
# end
end
end
end
谢谢!