class TrueURL

Constants

OPTIONS
QUERY_VALUES_TO_REMOVE
VERSION

Attributes

context[RW]
strategies[RW]

Public Class Methods

new(url, options = {}) click to toggle source
# File lib/true_url.rb, line 26
def initialize(url, options = {})
  @context = TrueURL::Context.new(url, OPTIONS.merge(options))
  @strategies = TrueURL::Strategy.default_list
  @executed = false
end

Public Instance Methods

attributes() click to toggle source
# File lib/true_url.rb, line 37
def attributes
  execute
  @context.attributes
end
canonical() click to toggle source
# File lib/true_url.rb, line 32
def canonical
  execute
  @context.working_url.to_s
end

Private Instance Methods

attempt_fetch?() click to toggle source
# File lib/true_url.rb, line 79
def attempt_fetch?
  return false unless @context.options[:fetch]

  # Must at least have a host, otherwise we can't find the site to crawl
  return false if @context.working_url.host.nil?

  # We only support HTTP or HTTPS
  %w[http https].include?(@context.working_url.scheme)
end
clean_query_values() click to toggle source
# File lib/true_url.rb, line 97
def clean_query_values
  query_values = @context.working_url.query_values

  unless query_values.nil?
    QUERY_VALUES_TO_REMOVE.each { |p| query_values.delete(p) }
    @context.working_url.query_values = query_values.empty? ? nil : query_values
  end
end
execute() click to toggle source
# File lib/true_url.rb, line 44
def execute
  return if @executed

  execute_strategies

  unless @context.finalized?
    if attempt_fetch?
      TrueURL::Fetch.execute(@context)
      execute_strategies
    end
  end

  scheme_override
  remove_fragments
  clean_query_values

  @executed = true
end
execute_strategies() click to toggle source
# File lib/true_url.rb, line 63
def execute_strategies
  @strategies.each do |s|
    match_criteria = s[0]
    strategy = s[1]

    strategy.execute(@context) unless @context.finalized? || !strategy_match?(match_criteria)
  end
end
remove_fragments() click to toggle source
# File lib/true_url.rb, line 93
def remove_fragments
  @context.working_url.fragment = nil
end
scheme_override() click to toggle source
# File lib/true_url.rb, line 89
def scheme_override
  @context.working_url.scheme = @context.options[:scheme_override] unless @context.options[:scheme_override].nil?
end
strategy_match?(match_criteria) click to toggle source
# File lib/true_url.rb, line 72
def strategy_match?(match_criteria)
  return true if match_criteria.nil?

  host = @context.working_url.host
  host.nil? ? false : host.match(match_criteria)
end