Sha256: c686113e95375bc13fbcfe82c00a6fd0ae4020b8439cb719bf770753dbf10b60

Contents?: true

Size: 1.67 KB

Versions: 72

Compression:

Stored size: 1.67 KB

Contents

# Author:: Couchbase <info@couchbase.com>
# Copyright:: 2011-2012 Couchbase, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module Couchbase

  class Utils

    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

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

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

    # Return the bytesize of String; uses String#size under Ruby 1.8 and
    # String#bytesize under 1.9.
    if ''.respond_to?(:bytesize)
      def self.bytesize(string)
        string.bytesize
      end
    else
      def self.bytesize(string)
        string.size
      end
    end

  end

end

Version data entries

72 entries across 72 versions & 3 rubygems

Version Path
couchbase-1.3.15 lib/couchbase/utils.rb
couchbase-1.3.15-x64-mingw32 lib/couchbase/utils.rb
couchbase-1.3.15-x86-mingw32 lib/couchbase/utils.rb
couchbase-1.3.14 lib/couchbase/utils.rb
couchbase-1.3.14-x64-mingw32 lib/couchbase/utils.rb
couchbase-1.3.14-x86-mingw32 lib/couchbase/utils.rb
couchbase-1.3.13-x86-mingw32 lib/couchbase/utils.rb
couchbase-1.3.13-x64-mingw32 lib/couchbase/utils.rb
couchbase-1.3.13 lib/couchbase/utils.rb
couchbase-1.3.12 lib/couchbase/utils.rb
couchbase-1.3.12-x86-mingw32 lib/couchbase/utils.rb
couchbase-1.3.12-x64-mingw32 lib/couchbase/utils.rb
couchbase-1.3.11-x86-mingw32 lib/couchbase/utils.rb
couchbase-1.3.11-x64-mingw32 lib/couchbase/utils.rb
couchbase-1.3.11 lib/couchbase/utils.rb
couchbase-jruby-client-0.2.2-java lib/couchbase/utils.rb
couchbase-1.3.10 lib/couchbase/utils.rb
couchbase-1.3.10-x86-mingw32 lib/couchbase/utils.rb
couchbase-1.3.10-x64-mingw32 lib/couchbase/utils.rb
couchbase-1.3.9 lib/couchbase/utils.rb