Module: BitBucket::Utils::Url

Extended by:
Url
Included in:
Request::OAuth, Url
Defined in:
lib/bitbucket_rest_api/utils/url.rb

Constant Summary collapse

DEFAULT_QUERY_SEP =
/[&;] */n
KEY_VALUE_SEP =
'='.freeze

Instance Method Summary collapse

Instance Method Details

#build_query(params) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/bitbucket_rest_api/utils/url.rb', line 16

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

#escape(s) ⇒ Object



12
# File 'lib/bitbucket_rest_api/utils/url.rb', line 12

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

#parse_query(query_string) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bitbucket_rest_api/utils/url.rb', line 26

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

#parse_query_for_param(query_string, name) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/bitbucket_rest_api/utils/url.rb', line 46

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

#unescape(s) ⇒ Object



14
# File 'lib/bitbucket_rest_api/utils/url.rb', line 14

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