module ElasticSearch::Transport::ProtocolHelpers
Private Instance Methods
bytesize(string)
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 280 def bytesize(string) string.bytesize end
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 266 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
faster than CGI.escape stolen from RSolr, who stole it from Rack
# File lib/elasticsearch/transport/base_protocol.rb, line 253 def escape(string) string.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) { #'%'+$1.unpack('H2'*$1.size).join('%').upcase '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase }.tr(' ', '+') 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 237 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 226 def generate_uri(options) path = "" path << "/#{Array(options[:index]).collect { |i| escape(i.downcase) }.join(",")}" if options[:index] && !options[:index].empty? path << "/_all" 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 218 def handle_error(response) raise RequestError, "(#{response.status}) #{response.body}" end
set_encoding!(hit)
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 246 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 212 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 260 def unescape(string) CGI.unescape(string) end
unescape_id!(hit)
click to toggle source
# File lib/elasticsearch/transport/base_protocol.rb, line 241 def unescape_id!(hit) hit["_id"] = unescape(hit["_id"]) nil end