Class: Couchbase::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/utils.rb

Class Method Summary (collapse)

Class Method Details

+ (Object) build_query(uri, params = nil)



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/couchbase/utils.rb', line 22

def self.build_query(uri, params = nil)
  uri = uri.dup
  return uri if params.nil? || params.empty?
  uri << "?"
  uri << params.map do |k, v|
    if %w{key keys startkey endkey start_key end_key}.include?(k.to_s)
      v = MultiJson.dump(v)
    end
    if v.class == Array
      build_query(v.map { |x| [k, x] })
    else
      "#{escape(k)}=#{escape(v)}"
    end
  end.join("&")
end

+ (Object) bytesize(string)



47
48
49
# File 'lib/couchbase/utils.rb', line 47

def self.bytesize(string)
  string.bytesize
end

+ (Object) escape(s)



38
39
40
41
42
# File 'lib/couchbase/utils.rb', line 38

def self.escape(s)
  s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/nu) {
    '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
  }.tr(' ', '+')
end