Module: Domainatrix

Defined in:
lib/domainatrix.rb,
lib/domainatrix/url.rb,
lib/domainatrix/version.rb,
lib/domainatrix/domain_parser.rb

Defined Under Namespace

Classes: DomainParser, Error, ParseError, Url

Constant Summary collapse

DOMAIN_PARSER =

Keep Constant for backwards compat

DomainParser.new("#{File.dirname(__FILE__)}/effective_tld_names.dat")
VERSION =
"0.0.14"

Class Method Summary collapse

Class Method Details

.custom_parse(url, dat = "#{File.dirname(__FILE__)}/effective_tld_names.dat", sections = ["ICANN DOMAINS"]) ⇒ Object



22
23
24
# File 'lib/domainatrix.rb', line 22

def self.custom_parse(url, dat = "#{File.dirname(__FILE__)}/effective_tld_names.dat", sections = ["ICANN DOMAINS"])
  Url.new(DomainParser.new(dat, sections).parse(url))
end

.icann_parse(url, dat = "#{File.dirname(__FILE__)}/effective_tld_names.dat", sections = ["ICANN DOMAINS"]) ⇒ Object



18
19
20
# File 'lib/domainatrix.rb', line 18

def self.icann_parse(url, dat = "#{File.dirname(__FILE__)}/effective_tld_names.dat", sections = ["ICANN DOMAINS"])
  Url.new(DomainParser.new(dat, sections).parse(url))
end

.parse(url) ⇒ Object



26
27
28
29
# File 'lib/domainatrix.rb', line 26

def self.parse(url)
  #Url.new(DomainParser.parse(url)) #<-- Still slow implementation at this point
  Url.new(DOMAIN_PARSER.parse(url))
end

.scan(text, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/domainatrix.rb', line 31

def self.scan(text, &block)
  return [] unless text
  @schemes ||= %w(http https)
  all_trailing_clutter = /[.,:);]+$/
  clutter_without_parens = /[.,:);]+$/

  candidate_urls = ::URI.extract(text, @schemes)
  candidate_urls.map! do |url|
    # If the URL has an open paren, allow closing parens.
    if url.include?("(")
      url.gsub(clutter_without_parens, '')
    else
      url.gsub(all_trailing_clutter, '')
    end
  end

  urls = candidate_urls.map do |url|
    begin
      parse(url)
    rescue Addressable::URI::InvalidURIError
    end
  end.compact

  urls.map!(&block) if block
  urls
end