Class: MotionResource::UrlEncoder

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-resource/url_encoder.rb

Instance Method Summary collapse

Instance Method Details

#build_query_string(url, params = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/motion-resource/url_encoder.rb', line 23

def build_query_string(url, params = {})
  return url if params.keys.empty?
  # fake a url so we avoid regex nastiness with URL's
  url = NSURL.URLWithString("http://blah.com/#{url}")
  # build our query string (needs encoding support!)
  query_string = params.to_query
  if url.query.nil? || url.query.empty?
    # strip the beginning / and add the query
    "#{url.path[1..-1]}?#{query_string}"
  else
    "#{url.path[1..-1]}?#{url.query}&#{query_string}"
  end
end

#fill_url_params(url, params = {}, delegate = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/motion-resource/url_encoder.rb', line 3

def fill_url_params(url, params = {}, delegate = nil)
  params ||= {}
  url = url.split('/').collect { |path|
    ret = path
    if path[0] == ':'
      path_sym = path[1..-1].to_sym

      curr = nil
      if delegate && delegate.respond_to?(path_sym)
        curr = delegate.send(path_sym)
      end

      ret = (curr || params.delete(path_sym) || path).to_s
    end

    ret
  }.join '/'
  self.build_query_string(url, params)
end

#insert_extension(url, extension) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/motion-resource/url_encoder.rb', line 37

def insert_extension(url,extension)
  return url if extension.blank?

  url = NSURL.URLWithString(url)
  extension = extension.gsub(".", "")
  url.URLByAppendingPathExtension(extension).absoluteString
end