class Embiggen::URI

Attributes

http_client[R]
uri[R]

Public Class Methods

new(uri) click to toggle source
# File lib/embiggen/uri.rb, line 11
def initialize(uri)
  @uri = URI(::Addressable::URI.parse(uri).normalize.to_s)
  @http_client = HttpClient.new(@uri)
end

Public Instance Methods

expand(request_options = {}) click to toggle source
# File lib/embiggen/uri.rb, line 16
def expand(request_options = {})
  return uri unless shortened?

  redirects = extract_redirects(request_options)
  location = follow(request_options)

  self.class.new(location).
    expand(request_options.merge(:redirects => redirects - 1))
end
shortened?() click to toggle source
# File lib/embiggen/uri.rb, line 26
def shortened?
  Configuration.shorteners.include?(uri)
end

Private Instance Methods

extract_redirects(request_options = {}) click to toggle source
# File lib/embiggen/uri.rb, line 32
def extract_redirects(request_options = {})
  redirects = request_options.fetch(:redirects) { Configuration.redirects }
  fail TooManyRedirects.new(
    "following #{uri} reached the redirect limit", uri) if redirects.zero?

  redirects
end
follow(request_options = {}) click to toggle source
# File lib/embiggen/uri.rb, line 40
def follow(request_options = {})
  timeout = request_options.fetch(:timeout) { Configuration.timeout }

  location = http_client.follow(timeout)
  fail BadShortenedURI.new(
    "following #{uri} did not redirect", uri) unless followable?(location)

  location
end
followable?(location) click to toggle source
# File lib/embiggen/uri.rb, line 50
def followable?(location)
  return unless location

  Addressable::URI.parse(location).absolute?
rescue Addressable::URI::InvalidURIError => e
  raise BadShortenedURI.new(
    "following #{uri} returns an invalid URI: #{e}", uri)
end