`block in <top(required)=“”>':未初始化的常量Zillow(NameError)

时间:2017-05-24 23:59:30

标签: ruby-on-rails zillow

我正在尝试在我的应用中复制this Zillow实现的版本,并且得到一个block in <top (required)>': uninitialized constant Zillow (NameError),这使我甚至无法启动我的服务器。

我的config/initializers/zillow_api.rb中有以下内容:

Rails.application.config.to_prepare do
  Zillow::Api::Client.config.api_key = Rails.application.secrets.zillow_api_key
end

这在lib/zillow/api/client.rb中(就像在示例应用中一样):

module Zillow
  module Api

    module Exception
      ZillowError = Class.new(StandardError)
      %w[InvalidRequestError ExactMatchNotFoundError NoResultsError
        UnableToParseResponseError ServiceError ZWSIDInvalid
        ZWSIDMissing ServiceUnavailable].each do |klass_name|
          const_set klass_name, Class.new(ZillowError)
      end
    end

    class Client < RestClient::Request
      extend Zillow::Api::Exception
      include ActiveSupport::Configurable

      class << self

        def url(endpoint_name,params={})
          raise ZWSIDMissing.new, 'Zillow API key (zws-id) not specified' unless config.api_key.present?
          raise InvalidRequestError.new, 'No endpoint specified' if endpoint_name.blank?
          params = { 'zws-id' => config.api_key }.merge(params)
          "http://www.zillow.com/webservice/#{endpoint_name}.htm?#{params.to_query}"
        end

        def get(url,params={})
          parse_response self.new(method: 'get', url: url, payload: params).execute
        end

        def parse_results(response_data)
          result_or_results = response_data['response']['results']['result']
          result_or_results.is_a?(Array) ? result_or_results : [ result_or_results ]
        rescue => e
          raise UnableToParseResponseError.new, "Unknown data format encountered: #{e.message}"
        end

        def parse_response(response)
          response_data = Nori.new.parse(response)

          # munge around the XML to get at the actual data
          begin
            response_data = response_data[response_data.keys.first]
          rescue => e
            raise UnableToParseResponseError.new , e.message
          end

          # seems like all responses are 200 OK, so check the response payload to see if
          # there was an error
          response_code = response_data['message']['code'].to_i
          message       = response_data['message']['text']

          return parse_results(response_data) if response_code == 0

          case response_code
          when 1
            raise ServiceError.new,               "Service error: #{message}"
          when 2
            raise ZWSIDInvalid.new,               "Invalid Zillow API key (zws-id)"
          when 3, 4, 505
            raise ServiceUnavailable.new,         "The Zillow API is currently unavailable"
          when 500, 501, 506
            raise InvalidRequestError.new,        message.gsub('Error: ','').capitalize
          when 502
            raise NoResultsError.new,             "Sorry, the address you provided is not found in Zillow's property database."
          when 503, 504
            raise InvalidRequestError.new,        "Failed to resolve city/state (or zip code), or no coverage: #{message}"
          when 507, 508
            raise ExactMatchNotFoundError.new,    "No exact match found. Verify that the given address is correct."
          else
            raise UnableToParseResponseError.new, "Unknown response code #{response_code}: #{message}"
          end
        end

        # as far as I can tell, all data APIs are GET requests
        def method_missing(m,params={})
          get url(m.to_s.camelize,params)
        end

      end
    end
  end
end

我在zillow_api_key的{​​{1}}和development区域中有我的production,我在我的secrets.yml方法中调用了Zillow客户端API {1}}:

def show

values#show

尝试启动服务器时的服务器输出是:

values_controller

有人能看出为什么它不能识别Zillow API吗?

1 个答案:

答案 0 :(得分:1)

在您的config/application.rb中,lib目录是否添加到config.autoload_paths?如果没有,请添加此行(就像在here中)

config.autoload_paths += Dir["#{config.root}/lib/**/"]

否则,来自lib的文件不会自动加载,初始化程序也不会知道它们。