Sha256: 6475ce36c6fdf240492b4b61b1bcbe0a77f9bddcde7cbf714fe8bfcfbefc07bf

Contents?: true

Size: 1.27 KB

Versions: 21

Compression:

Stored size: 1.27 KB

Contents

require 'cgi'

module BitBucket
  module Utils
    module Url
      extend self

      DEFAULT_QUERY_SEP = /[&;] */n

      KEY_VALUE_SEP = '='.freeze

      def escape(s) CGI.escape s.to_s end

      def unescape(s) CGI.unescape s.to_s end

      def build_query(params)
        params.map { |k, v|
          if v.class == Array
            build_query(v.map { |x| [k, x] })
          else
            v.nil? ? escape(k) : "#{escape(k)}=#{escape(v)}"
          end
        }.join("&")
      end

      def parse_query(query_string)
        return '' if query_string.nil? || query_string.empty?
        params = {}

        query_string.split(DEFAULT_QUERY_SEP).each do |part|
          k, v = part.split(KEY_VALUE_SEP, 2).map { |el| unescape(el) }

          if cur = params[k]
            if cur.class == Array
              params[k] << v
            else
              params[k] = [cur, v]
            end
          else
            params[k] = v
          end
        end
        params
      end

      def parse_query_for_param(query_string, name)
        parse_query(query_string).each do |key, val|
          return val.first if (name == key) && val.is_a?(Array)
          return val if (name == key)
        end
        return nil
      end

    end # Url
  end # Utils
end # BitBucket

Version data entries

21 entries across 21 versions & 5 rubygems

Version Path
codenamev_bitbucket_api-0.4.1 lib/bitbucket_rest_api/utils/url.rb
codenamev_bitbucket_api-0.4.0 lib/bitbucket_rest_api/utils/url.rb
zz_bitbucket_rest_api-0.1.11 lib/bitbucket_rest_api/utils/url.rb
zz_bitbucket_rest_api-0.1.10 lib/bitbucket_rest_api/utils/url.rb
zz_bitbucket_rest_api-0.1.9 lib/bitbucket_rest_api/utils/url.rb
zz_bitbucket_rest_api-0.1.8 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api2-0.9.5 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api2-0.9.1 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api2-0.2.2 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api2-0.2.1 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api2-0.2.0 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api-0.1.7 lib/bitbucket_rest_api/utils/url.rb
reenhanced_bitbucket_api-0.3.2 lib/bitbucket_rest_api/utils/url.rb
reenhanced_bitbucket_api-0.3.1 lib/bitbucket_rest_api/utils/url.rb
reenhanced_bitbucket_api-0.3.0 lib/bitbucket_rest_api/utils/url.rb
reenhanced_bitbucket_api-0.1.6 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api-0.1.5 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api-0.1.4 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api-0.1.2 lib/bitbucket_rest_api/utils/url.rb
bitbucket_rest_api-0.1.1 lib/bitbucket_rest_api/utils/url.rb