class NewsScraper::URIParser

Public Class Methods

new(url) click to toggle source

Initialize a URIParser

Params

  • url: the url to parse to a uri

# File lib/news_scraper/uri_parser.rb, line 10
def initialize(url)
  @uri = URI.parse(url)
end

Public Instance Methods

host() click to toggle source

Returns the URI's host, removing paths, params, and schemes

Returns

# File lib/news_scraper/uri_parser.rb, line 37
def host
  without_scheme.downcase.match(/^(?:[\w\d-]+\.)?(?<host>[\w\d-]+\.\w{2,})/)['host']
end
with_scheme() click to toggle source

Returns the URI with a scheme, adding http:// if no scheme is present

Returns

  • A URI string, with http:// if no scheme was specified

# File lib/news_scraper/uri_parser.rb, line 28
def with_scheme
  @uri.scheme ? @uri.to_s : "http://#{@uri}"
end
without_scheme() click to toggle source

Removes the scheme from the URI

Returns

  • A schemeless URI string, e.g. google.ca will return google.ca

# File lib/news_scraper/uri_parser.rb, line 19
def without_scheme
  @uri.scheme ? @uri.to_s.gsub(%r{^#{@uri.scheme}://}, '') : @uri.to_s
end