Class: Couchbase::Utils

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

Class Method Summary collapse

Class Method Details

.build_query(uri, params = nil) ⇒ Object



36
37
38
39
40
# File 'lib/couchbase/utils.rb', line 36

def self.build_query(uri, params = nil)
  uri = uri.dup
  return uri if params.nil? || params.empty?
  uri << "?" << encode_params(params)
end

.bytesize(string) ⇒ Object



51
52
53
# File 'lib/couchbase/utils.rb', line 51

def self.bytesize(string)
  string.bytesize
end

.encode_params(params) ⇒ Object



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

def self.encode_params(params)
  params.map do |k, v|
    next if !v && k.to_s == "group"
    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.compact.join("&")
end

.escape(s) ⇒ Object



42
43
44
45
46
# File 'lib/couchbase/utils.rb', line 42

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