Sha256: 39d811bd59a70a8ff151605aec816b7e89c5a0d41a6bbdeb7c7a1b4b60a25d2a

Contents?: true

Size: 973 Bytes

Versions: 1

Compression:

Stored size: 973 Bytes

Contents

class UrlResolver
  def initialize(options = {})
    @options = options
  end

  def resolve(given_url)
    normal_url(given_url) ||
      url_for_double_dashed(given_url) ||
      slash_based_url(given_url) ||
      non_prefixed_url(given_url) ||
      given_url
  end

  def base_url
    @base_url ||= base_url_for(referer)
  end

  private

  def referer
    @referer ||= @options.fetch(:referer)
  end

  def base_url_for(url)
    matches = url.match(/(http(s)?:\/\/[^\/]+)/)
    return matches[1] if matches
  end

  def url_for_double_dashed(url)
    if url.match(/^\/\/[\w]+/)
      "http:#{url}"
    end
  end

  def slash_based_url(url)
    if url.match(/^\/[\w]+/)
      "#{base_url}#{url}"
    end
  end

  def normal_url(url)
    is_url = base_url_for(url)
    url if is_url
  end

  def non_prefixed_url(url)
    expanded = File.expand_path(url, referer)
    expanded = expanded.split("http")
    expanded.shift
    expanded.unshift("").join("http")
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
page_downloader-1.0 lib/page_downloader/url_resolver.rb