2017-05-24 7 views
0

this私のアプリでZillowの実装を複製しようとしていて、サーバを起動しなくてもblock in <top (required)>': uninitialized constant Zillow (NameError) 。 (これはサンプルアプリケーションであるとして)ブロックが<top(必須)> ':初期化されていない定数Zillow(NameError)

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

そしてこのlib/zillow/api/client.rbで:

私は私のconfig/initializers/zillow_api.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 

私は私のdevelopmentproduction地域でzillow_api_key私のsecrets.ymlと私は私のvalues#showの方法で私のvalues_controllerのZillowクライアントAPIを呼び出します:

私は、サーバーを起動しようとするとデフ

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 

エンド

サーバー出力を表示するには、このです:それはZillowのAPIを認識していない理由

/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>' 

誰でも見ることができますか?

答えて

1

config/application.rblibディレクトリにconfig.autoload_pathsが追加されていますか?そうでない場合、(ちょうどhereのように)

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

をこの行を追加してくださいそれ以外の場合は、libからのファイルが自動的にロードされず、初期化子はそれらについて知りません。

+1

これはそれでした!ありがとうございました! – Liz

関連する問題