Class: Flare::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/flare/url.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: "", query: {}) ⇒ Url

Returns a new instance of Url.



43
44
45
46
47
48
49
# File 'lib/flare/url.rb', line 43

def initialize(url: "", query: {})
  @url = url
  @query = query

  @addressable = self.class.parsed(url)
  @addressable.query_values = query
end

Class Method Details

.host(string) ⇒ Object

Attempts to parse a string to get the ‘host` value from it. Typically this value is in the form of `google.com` or `inbox.google.com` with subdomains.



32
33
34
# File 'lib/flare/url.rb', line 32

def self.host(string)
  parsed(string)&.host
end

.is_valid?(url) ⇒ Boolean

Returns if a url is valid after being parsed.

Returns:

  • (Boolean)


23
24
25
# File 'lib/flare/url.rb', line 23

def self.is_valid?(url)
  %w(http https).include?(scheme(url))
end

.parse_host(string) ⇒ Object

Uses PublicSuffix to lookup validated domain names.



16
17
18
# File 'lib/flare/url.rb', line 16

def self.parse_host(string)
  PublicSuffix.parse(host(string))
end

.parsed(url) ⇒ Object

Base parse method which handles invalid uris.



7
8
9
10
11
# File 'lib/flare/url.rb', line 7

def self.parsed(url)
  Addressable::URI.heuristic_parse(url)
rescue Addressable::URI::InvalidURIError => error
  nil
end

.scheme(url) ⇒ Object

Returns the scheme (http, https, ftp) for a url.



39
40
41
# File 'lib/flare/url.rb', line 39

def self.scheme(url)
  parsed(url)&.scheme
end

Instance Method Details

#to_sObject



51
52
53
# File 'lib/flare/url.rb', line 51

def to_s
  @addressable.to_s
end