Class: Embiggen::URI

Inherits:
Object
  • Object
show all
Defined in:
lib/embiggen/uri.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ URI

Returns a new instance of URI.



8
9
10
# File 'lib/embiggen/uri.rb', line 8

def initialize(uri)
  @uri = uri.is_a?(::URI::Generic) ? uri : URI(uri.to_s)
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



6
7
8
# File 'lib/embiggen/uri.rb', line 6

def uri
  @uri
end

Instance Method Details

#expand(request_options = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/embiggen/uri.rb', line 12

def expand(request_options = {})
  expand!(request_options)
rescue TooManyRedirects => error
  error.uri
rescue StandardError, ::Timeout::Error
  uri
end

#expand!(request_options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/embiggen/uri.rb', line 20

def expand!(request_options = {})
  return uri unless shortened?

  redirects = request_options.fetch(:redirects) { Configuration.redirects }
  check_redirects(redirects)

  location = head_location(request_options)
  check_location(location)

  URI.new(location).
    expand!(request_options.merge(:redirects => redirects - 1))
end

#shortened?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/embiggen/uri.rb', line 33

def shortened?
  Configuration.shorteners.any? { |domain| uri.host =~ /\b#{domain}\z/i }
end