module ElasticSearch::Transport::ProtocolHelpers

Private Instance Methods

encode_utf8(string) click to toggle source

encodes the string as utf-8 in Ruby 1.9

# File lib/elasticsearch/transport/base_protocol.rb, line 263
def encode_utf8(string)
  # ElasticSearch only ever returns json in UTF-8 (per the JSON spec) so we can use force_encoding here (#TODO what about ids? can we assume those are always ascii?)
  string.force_encoding(::Encoding::UTF_8)
end
escape(string) click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 253
def escape(string)
  Faraday::Utils.escape(string)
end
generate_query_string(params) click to toggle source

doesn’t handle arrays or hashes or what have you

# File lib/elasticsearch/transport/base_protocol.rb, line 239
def generate_query_string(params)
  params.collect { |k,v| "#{escape(k.to_s)}=#{escape(v.to_s)}" }.join("&")
end
generate_uri(options) click to toggle source

:index - one or many index names :type - one or many types :id - one id :op - optional operation

# File lib/elasticsearch/transport/base_protocol.rb, line 228
def generate_uri(options)
  path = ""
  path << "/#{Array(options[:index]).collect { |i| escape(i.downcase) }.join(",")}" if options[:index] && !options[:index].empty?
  path << "/*" if options[:index] && options[:index].empty?
  path << "/#{Array(options[:type]).collect { |t| escape(t) }.join(",")}" if options[:type] && !options[:type].empty?
  path << "/#{Array(options[:id]).collect { |id| escape(id) }.join(",")}" if options[:id] && !options[:id].to_s.empty?
  path << "/#{options[:op]}" if options[:op]
  path
end
handle_error(response) click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 220
def handle_error(response)
  raise RequestError.new(response.status), "(#{response.status}) #{response.body}"
end
set_encoding!(hit) click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 248
def set_encoding!(hit)
  encode_utf8(hit["_source"]) if hit["_source"].is_a?(String)
  nil
end
standard_request(*args) click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 214
def standard_request(*args)
  response = request(*args)
  handle_error(response) unless response.status >= 200 && response.status < 300
  encoder.decode(response.body)
end
unescape(string) click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 257
def unescape(string)
  Faraday::Utils.unescape(string)
end
unescape_id!(hit) click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 243
def unescape_id!(hit)
  hit["_id"] = unescape(hit["_id"])
  nil
end