Module: Github::Utils::Url

Extended by:
Url
Included in:
Client::Search, Client::Search::Legacy, PageIterator, Request::OAuth2, Url
Defined in:
lib/github_api2/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



23
24
25
26
27
28
29
30
31
# File 'lib/github_api2/utils/url.rb', line 23

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



17
# File 'lib/github_api2/utils/url.rb', line 17

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

#escape_uri(s) ⇒ Object



15
# File 'lib/github_api2/utils/url.rb', line 15

def escape_uri(s) Addressable::URI.escape(s.to_s) end

#normalize(s) ⇒ Object



21
# File 'lib/github_api2/utils/url.rb', line 21

def normalize(s) Addressable::URI.parse(s.to_s).normalize.path end

#parse_query(query_string) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/github_api2/utils/url.rb', line 33

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



53
54
55
56
57
58
59
# File 'lib/github_api2/utils/url.rb', line 53

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



19
# File 'lib/github_api2/utils/url.rb', line 19

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