Module: Ubersmith::UrlMaker

Included in:
Client
Defined in:
lib/ubersmith/url_maker.rb

Instance Method Summary collapse

Instance Method Details

#_escape(value, method) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ubersmith/url_maker.rb', line 20

def _escape(value, method)
  case method.to_s
  when 'cgi'
    CGI.escape(value.to_s)
  when 'uri'
    URI.escape(value.to_s, Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}]"))
  when ''
    value.to_s
  else
    raise ArgumentError.new("unknown escape method: #{method}")
  end
end

#append_url_param(url, param) ⇒ Object



14
15
16
17
18
# File 'lib/ubersmith/url_maker.rb', line 14

def append_url_param(url, param)
  return url if param.to_s =~ /^\s*$/
  joiner = url.index('?') ? '&' : '?'
  [url, param].join(joiner)
end

#build_query_string(pars, escape_method = :cgi) ⇒ Object



6
7
8
# File 'lib/ubersmith/url_maker.rb', line 6

def build_query_string(pars, escape_method = :cgi)
  pars.map {|key, val| _escape(key, escape_method) + '=' + _escape(val, escape_method) }.join("&")
end

#build_url(url, pars, escape = :cgi) ⇒ Object



10
11
12
# File 'lib/ubersmith/url_maker.rb', line 10

def build_url(url, pars, escape = :cgi)
  append_url_param(url, build_query_string(pars, escape))
end