class HTTPUtils::URLUtils

class URLUtils - for dealing with urls

Public Class Methods

extract_url_params(url) click to toggle source
# File lib/utils/http_utils.rb, line 10
def self.extract_url_params(url)
  url_params = {}

  if url.split('?')
        .length > 1
    url.split('?')[1].split('&').map do |e|
      key, value = e.split('=')

      break if value.nil?

      url_params[key] = value
    end
  end

  url_params
end
get_match_indices(url, path) click to toggle source
# File lib/utils/http_utils.rb, line 38
def self.get_match_indices(url, path)
  split_url = url.split '/'
  split_path = path.split '/'

  hash_with_variables = {}

  return unless url_path_matches? url, path

  split_url.each_with_index do |pname, idx|
    part = pname.sub(':', '')
    hash_with_variables[part] = split_path[idx] if pname[0] == ':'
  end

  hash_with_variables
end
matches_url_regex?(url, regex) click to toggle source
# File lib/utils/http_utils.rb, line 54
def self.matches_url_regex?(url, regex)
  return unless url_path_matches? url, regex

  matches = url.scan %r{((?<=\/):[^\/]+)}
  newregexp = url.dup

  return url.match? Regexp.new("^#{regex}$") if matches.empty?

  matches.each do |mat|
    newregexp.gsub!(Regexp.new(mat[0].to_s), '.+')
  end

  url.match? Regexp.new(newregexp)
end
url_path_matches?(url, path) click to toggle source
# File lib/utils/http_utils.rb, line 27
def self.url_path_matches?(url, path)
  return true if url == path

  split_url = url.split '/'
  split_path = path.split '/'

  return false if split_url.empty? || split_url.length != split_path.length

  true
end