0

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

我有以下内容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_keydevelopmentproduction领域中有我,我在我的方法中secrets.yml调用 Zillow 客户端 API :values#showvalues_controller

定义显示

begin
  now = Time.now.to_f
  @results  = Zillow::Api::Client.get_search_results params.slice(:address, :citystatezip)
  @results  = [ @results ] unless @results.is_a?(Array)
  @duration = ( Time.now.to_f - now ).round(2)
rescue Zillow::Api::Exception::ZillowError => e
  @exception = e
end

结尾

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

/Users/lizbayardelle/Dropbox/Code/OCR/config/initializers/zillow_api.rb:5:in `block in <top (required)>': uninitialized constant Zillow (NameError)
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:446:in `instance_exec'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:446:in `block in make_lambda'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:192:in `block in simple'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `block in call'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `each'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `call'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_prepare_callbacks'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/actionpack-4.2.6/lib/action_dispatch/middleware/reloader.rb:83:in `prepare!'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/actionpack-4.2.6/lib/action_dispatch/middleware/reloader.rb:55:in `prepare!'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/application/finisher.rb:50:in `block in <module:Finisher>'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:347:in `each'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:347:in `call'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
    from /Users/lizbayardelle/.rvm/rubies/ruby-2.3.3/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
    from /Users/lizbayardelle/Dropbox/Code/OCR/config/environment.rb:5:in `<top (required)>'
    from /Users/lizbayardelle/Dropbox/Code/OCR/config.ru:3:in `block in <main>'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:55:in `initialize'
    from /Users/lizbayardelle/Dropbox/Code/OCR/config.ru:in `new'
    from /Users/lizbayardelle/Dropbox/Code/OCR/config.ru:in `<main>'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:49:in `eval'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/builder.rb:40:in `parse_file'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/server.rb:208:in `app'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/server.rb:61:in `app'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/rack-1.6.5/lib/rack/server.rb:336:in `wrapped_app'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/server.rb:139:in `log_to_stdout'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/server.rb:78:in `start'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/lizbayardelle/.rvm/gems/ruby-2.3.3/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:9:in `require'
    from bin/rails:9:in `<main>'

谁能明白为什么它不能识别 Zillow API?

4

1 回答 1

1

在您config/application.rblib目录中添加了config.autoload_paths吗?如果没有,请添加此行(就像在这里一样)

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

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

于 2017-05-26T13:40:36.873 回答