2012-06-03 56 views
3

我想用Faraday發送帶有JSON正文(用於搜索)的獲取請求,但正在獲取上述錯誤。我認爲Proc內部的self搞亂了事情,但這與它無關。我正在關注[法拉第github頁面] [1]上的文檔,但都陷在了這個問題上。NoMethodError:undefined方法`split'爲#<Proc: ...>與法拉第

def perform_query 
    response = self.database.connection.get do |request| 
    request.url self.path 
    request.headers['Content-Type'] = 'application/json' 
    request.body(self.to_json) 
    end 
end 

def terms_to_json 
    terms_array = self.terms.keys.inject([]) do |terms_array, field| 
    value = self.terms[field] 
    terms_array.tap do |ary| 
     if value 
     ary << "\"#{field}\": \"#{value}\"" 
     end 
    end 
    end 

    "{ #{terms_array.join ','} }" 
end 

def to_json 
    "{ \"queryb\" : #{self.terms_to_json} }" 
end 

這裏是堆棧跟蹤,用錯誤來某處獲取PROC在#perform_query

from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/request.rb:60:in `url' 
    from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:219:in `block in run_request' 
    from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:237:in `block in build_request' 
    from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/request.rb:35:in `block in create' 
    from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/request.rb:34:in `tap' 
    from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/request.rb:34:in `create' 
    from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:233:in `build_request' 
    from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:218:in `run_request' 
    from /Users/chrismaddox/.rvm/gems/ruby-1.9.3-p125/gems/faraday-0.8.1/lib/faraday/connection.rb:87:in `get' 
    from /Users/chrismaddox/Dropbox/LivingSocial/Hungry Academy/Projects/hackchat/search_ruby/elastic.rb:83:in `method_missing' 
    from /Users/chrismaddox/Dropbox/LivingSocial/Hungry Academy/Projects/hackchat/search_ruby/elastic.rb:112:in `perform_query' 
    from /Users/chrismaddox/Dropbox/LivingSocial/Hungry Academy/Projects/hackchat/search_ruby/elastic.rb:61:in `send_query' 

UPDATE:

路徑方法返回路徑的字符串搜索對於給定的索引。例如/wombats/animals/_search

彈性::數據庫#路徑的呼叫彈性::指數#index_path:

module Elastic 
    ELASTIC_URL = "http://localhost:9200" 


    class Index 
    attr_reader :index_name, :type_name, :last 

    def initialize(type) 
     @index_name = "#{type}-index" 
     @type_name = type 
     @last = 0 
     add_to_elastic 
    end 

    def add_to_elastic 
     index_url = URI.parse "#{ELASTIC_URL}#{index_path}/" 
     Connection.new(index_url).put() 
    end 

    def index_path 
     "/#{self.index_name}" 
    end 

    def search_path 
     "#{type_path}/_search/" 
    end 

    def type_path 
     "#{self.index_path}/#{type_name}/" 
    end 

    end 
end 
+1

從棧跟蹤來判斷你的'path'方法可能會返回一些可疑的東西。它是什麼樣子的? –

+0

好吧我在問題 – Chris

+0

中添加了路徑方法。發佈路徑*包含的內容可能會更好,而不是生成代碼的代碼是什麼。 – Makoto

回答

1

的調用SEARCH_PATH = "#{type_path}/_search/" 的調用type_path = "#{self.index_path}/#{type_name}/" 到index_path調用= "/#{self.index_name}"

因此,如果索引名稱是袋熊,並且類型名稱是動物,search_path計算結果爲/wombat/animal//_search

事實證明, s不是顯示錯誤的問題,而是因法拉第的方法不一致而引起的。 Faraday::Request#urlFaraday::Request#headers本身是setter方法,而Faraday::Request#body=是body的setter方法。

相關問題