0

如何在 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

谢谢!

4

3 回答 3

1

一种方法是拆分为两种方法:

def try_to_find_site(location)
  find_site(location) || [LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end

def find_site(location)

  text_query = findplacefromtext_textquery(location)
  return convert_findplacefromtext_to_struct(text_query) if text_query[:status] == 'OK'

  phone_number = findplacefromtext_phonenumber(location)
  return convert_findplacefromtext_to_struct(phone_number) if phone_number[:status] == 'OK'

  text_search = textsearch(location)
  return convert_textsearch_to_struct(text_search) if text_search[:status] == 'OK'

end

还要确保你遵循一些关于方法名称的约定,因为会让眼睛流血......

于 2018-08-16T19:28:15.033 回答
1

你在正确的轨道上,但你的括号在错误的地方,所以你设置response"OK". 你反而想要:

  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
于 2018-08-16T20:18:18.457 回答
0

我会这样实现:

FINDERS = {findplacefromtext_textquery: :convert_findplacefromtext_to_struct,
          findplacefromtext_phonenumber: :convert_findplacefromtext_to_struct,
          convert_textsearch_to_struct: :textsearch}

def try_to_find_site(location)
  response = FINDERS.detect do |finder,converter|
    r = send(finder,location)
    break send(converter,r) if r[:status] == 'OK'
  end 
  response || [LocationStruct.new('Sorry, your business was not found, try the manual method', '', '', '')]
end

在这里,我们只是循环遍历方法,返回的第一个也将转换并分配给响应。如果没有找到,则返回默认值

于 2018-08-16T21:10:17.347 回答