NoMethodError:未定义的方法`split'用## proc:... =“”>和法拉第</proc:>

时间:2012-06-03 23:38:14

标签: ruby faraday

我想使用Faraday发送带有JSON正文(用于搜索)的get请求,但是我收到了上述错误。我认为Proc中的self搞砸了,但这与它无关。我正在关注[法拉第github页面] [1]上的文档,但是已经陷入了困境。

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

这是堆栈跟踪,错误出现在#perform_query中的get proc中:

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'

更新:

path方法返回搜索给定索引的路径字符串。例如/wombats/animals/_search

Elastic :: Database#path调用Elastic :: Index #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 个答案:

答案 0 :(得分:1)

对search_path = "#{type_path}/_search/"的调用 对type_path = "#{self.index_path}/#{type_name}/"的调用 对index_path = "/#{self.index_name}"

的调用

因此,如果索引名称为wombat且类型名称为animal,则search_path将计算为/wombat/animal//_search

事实证明,这不是显示错误的问题,而是由于法拉第的方法不一致而引起的。 Faraday::Request#urlFaraday::Request#headers本身就是setter方法,而Faraday::Request#body=是body的setter方法。