Class: ATDIS::SeparatedURL

Inherits:
Object
  • Object
show all
Defined in:
lib/atdis/separated_url.rb

Class Method Summary collapse

Class Method Details

.combine(url, url_params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/atdis/separated_url.rb', line 10

def self.combine(url, url_params)
  # Doing this jiggery pokery to ensure the params are sorted alphabetically (even on Ruby 1.8)
  query = url_params.map { |k, v| [k.to_s, v] }
                    .sort
                    .map { |k, v| "#{CGI.escape(k)}=#{CGI.escape(v.to_s)}" }
                    .join("&")
  if url_params.empty?
    url
  else
    url + "?" + query
  end
end

.merge(full_url, params) ⇒ Object



5
6
7
8
# File 'lib/atdis/separated_url.rb', line 5

def self.merge(full_url, params)
  url, url_params = split(full_url)
  combine(url, url_params.merge(params))
end

.split(full_url) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/atdis/separated_url.rb', line 23

def self.split(full_url)
  uri = URI.parse(full_url)
  url = if (uri.scheme == "http" && uri.port == 80) ||
           (uri.scheme == "https" && uri.port == 443)
          "#{uri.scheme}://#{uri.host}#{uri.path}"
        else
          "#{uri.scheme}://#{uri.host}:#{uri.port}#{uri.path}"
        end
  url_params = if uri.query
                 Hash[*CGI.parse(uri.query).map { |k, v| [k.to_sym, v.first] }.flatten]
               else
                 {}
               end
  [url, url_params]
end