Class: NewsScraper::URIParser
- Inherits:
-
Object
- Object
- NewsScraper::URIParser
- Defined in:
- lib/news_scraper/uri_parser.rb
Instance Method Summary collapse
-
#host ⇒ Object
Returns the URI’s host, removing paths, params, and schemes.
-
#initialize(url) ⇒ URIParser
constructor
Initialize a URIParser.
-
#with_scheme ⇒ Object
Returns the URI with a scheme, adding http:// if no scheme is present.
-
#without_scheme ⇒ Object
Removes the scheme from the URI.
Constructor Details
#initialize(url) ⇒ URIParser
Initialize a URIParser
Params
-
url
: the url to parse to a uri
10 11 12 |
# File 'lib/news_scraper/uri_parser.rb', line 10 def initialize(url) @uri = URI.parse(url) end |
Instance Method Details
#host ⇒ Object
Returns the URI’s host, removing paths, params, and schemes
Returns
-
The URI’s host, e.g. google.ca/search&q=query will return google.ca
37 38 39 |
# 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 ⇒ Object
Returns the URI with a scheme, adding http:// if no scheme is present
Returns
-
A URI string, with http:// if no scheme was specified
28 29 30 |
# File 'lib/news_scraper/uri_parser.rb', line 28 def with_scheme @uri.scheme ? @uri.to_s : "http://#{@uri}" end |
#without_scheme ⇒ Object
Removes the scheme from the URI
Returns
-
A schemeless URI string, e.g. google.ca will return google.ca
19 20 21 |
# 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 |