Class: Optic14n::CanonicalizedUrls

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/optic14n/canonicalized_urls.rb

Overview

Canonicalizes a set of URLs

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(urls, options) ⇒ CanonicalizedUrls

Returns a new instance of CanonicalizedUrls.



13
14
15
16
# File 'lib/optic14n/canonicalized_urls.rb', line 13

def initialize(urls, options)
  @urls = urls
  @options = options
end

Instance Attribute Details

#eachObject (readonly)

Returns the value of attribute each.



7
8
9
# File 'lib/optic14n/canonicalized_urls.rb', line 7

def each
  @each
end

#failuresObject (readonly)

Returns the value of attribute failures.



7
8
9
# File 'lib/optic14n/canonicalized_urls.rb', line 7

def failures
  @failures
end

#output_setObject (readonly)

Returns the value of attribute output_set.



7
8
9
# File 'lib/optic14n/canonicalized_urls.rb', line 7

def output_set
  @output_set
end

#seenObject (readonly)

Returns the value of attribute seen.



7
8
9
# File 'lib/optic14n/canonicalized_urls.rb', line 7

def seen
  @seen
end

Class Method Details

.from_urls(urls, options = {}) ⇒ Object

Canonicalize given urls. options will be passed to BLURI.parse



43
44
45
# File 'lib/optic14n/canonicalized_urls.rb', line 43

def self.from_urls(urls, options = {})
  CanonicalizedUrls.new(urls, options).tap(&:canonicalize!)
end

Instance Method Details

#canonicalize!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/optic14n/canonicalized_urls.rb', line 18

def canonicalize!
  @seen = 0
  @failures = {}
  @output_set = Set.new

  @urls.each do |url|
    begin
      @output_set.add(BLURI(url).canonicalize!(@options))
    rescue StandardError => e
      failures[url] = e
    end
    @seen += 1
  end
end

#write(filename) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/optic14n/canonicalized_urls.rb', line 33

def write(filename)
  File.open(filename, "w") do |file|
    @output_set.each do |url|
      file.puts url
    end
  end
end