Class: Twingly::URL

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/twingly/url.rb,
lib/twingly/version.rb,
lib/twingly/url/error.rb,
lib/twingly/url/hasher.rb,
lib/twingly/url/null_url.rb,
lib/twingly/url/utilities.rb

Defined Under Namespace

Modules: Error, Hasher, Utilities Classes: NullURL

Constant Summary collapse

SCHEMES =
%w(http https)
ENDS_WITH_SLASH =
/\/+$/
VERSION =
"2.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addressable_uri, public_suffix_domain) ⇒ URL



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/twingly/url.rb', line 43

def initialize(addressable_uri, public_suffix_domain)
  unless addressable_uri.is_a?(Addressable::URI)
    raise ArgumentError, "First parameter must be an Addressable::URI"
  end

  unless public_suffix_domain.is_a?(PublicSuffix::Domain)
    raise ArgumentError, "Second parameter must be a PublicSuffix::Domain"
  end

  @addressable_uri      = addressable_uri
  @public_suffix_domain = public_suffix_domain
end

Class Method Details

.internal_parse(potential_url) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/twingly/url.rb', line 26

def self.internal_parse(potential_url)
  if potential_url.is_a?(Addressable::URI)
    addressable_uri = potential_url
  else
    addressable_uri = Addressable::URI.heuristic_parse(potential_url)
  end

  raise Twingly::Error::ParseError if addressable_uri.nil?

  public_suffix_domain = PublicSuffix.parse(addressable_uri.display_uri.host)

  self.new(addressable_uri, public_suffix_domain)
rescue Addressable::URI::InvalidURIError, PublicSuffix::DomainInvalid => error
  error.extend(Twingly::URL::Error)
  raise
end

.parse(potential_url) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/twingly/url.rb', line 16

def self.parse(potential_url)
  potential_url = String(potential_url)
  potential_url = potential_url.scrub
  potential_url = potential_url.strip

  internal_parse(potential_url)
rescue Twingly::URL::Error, Twingly::URL::Error::ParseError => error
  NullURL.new
end

Instance Method Details

#<=>(other) ⇒ Object



129
130
131
# File 'lib/twingly/url.rb', line 129

def <=>(other)
  self.to_s <=> other.to_s
end

#domainObject



72
73
74
# File 'lib/twingly/url.rb', line 72

def domain
  public_suffix_domain.domain
end

#hostObject



76
77
78
# File 'lib/twingly/url.rb', line 76

def host
  addressable_uri.host
end

#inspectObject



137
138
139
# File 'lib/twingly/url.rb', line 137

def inspect
  sprintf("#<%s:0x%x %s>", self.class.name, __id__, self.to_s)
end

#normalizedObject



92
93
94
95
96
97
98
99
100
# File 'lib/twingly/url.rb', line 92

def normalized
  normalized_url = addressable_uri.dup

  normalized_url.scheme = normalized_scheme
  normalized_url.host   = normalized_host
  normalized_url.path   = normalized_path

  self.class.internal_parse(normalized_url)
end

#normalized_hostObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/twingly/url.rb', line 106

def normalized_host
  host   = addressable_uri.normalized_host
  domain = public_suffix_domain

  unless domain.subdomain?
    host = "www.#{host}"
  end

  host = normalize_blogspot(host, domain)

  host
end

#normalized_pathObject



119
120
121
122
123
# File 'lib/twingly/url.rb', line 119

def normalized_path
  path = strip_trailing_slashes(addressable_uri.path)

  (path.empty?) ? "/" : path
end

#normalized_schemeObject



102
103
104
# File 'lib/twingly/url.rb', line 102

def normalized_scheme
  addressable_uri.scheme.downcase
end

#originObject



80
81
82
# File 'lib/twingly/url.rb', line 80

def origin
  addressable_uri.origin
end

#pathObject



84
85
86
# File 'lib/twingly/url.rb', line 84

def path
  addressable_uri.path
end

#schemeObject



56
57
58
# File 'lib/twingly/url.rb', line 56

def scheme
  addressable_uri.scheme
end

#sldObject



64
65
66
# File 'lib/twingly/url.rb', line 64

def sld
  public_suffix_domain.sld
end

#tldObject



68
69
70
# File 'lib/twingly/url.rb', line 68

def tld
  public_suffix_domain.tld
end

#to_sObject



133
134
135
# File 'lib/twingly/url.rb', line 133

def to_s
  addressable_uri.to_s
end

#trdObject



60
61
62
# File 'lib/twingly/url.rb', line 60

def trd
  public_suffix_domain.trd
end

#valid?Boolean



125
126
127
# File 'lib/twingly/url.rb', line 125

def valid?
  addressable_uri && public_suffix_domain && SCHEMES.include?(normalized_scheme)
end

#without_schemeObject



88
89
90
# File 'lib/twingly/url.rb', line 88

def without_scheme
  self.to_s.sub(/\A#{scheme}:/, "")
end