Class: Clickmap::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/clickmap/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_html, links, extras = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
# File 'lib/clickmap/base.rb', line 7

def initialize(input_html, links, extras = {})
  self.input_html = input_html
  self.links = links.map { |l| symbolize_keys(l) }
  update_extras(extras)
  validate_links
end

Instance Attribute Details

#css_classObject

Returns the value of attribute css_class.



5
6
7
# File 'lib/clickmap/base.rb', line 5

def css_class
  @css_class
end

#input_htmlObject

Returns the value of attribute input_html.



5
6
7
# File 'lib/clickmap/base.rb', line 5

def input_html
  @input_html
end

Returns the value of attribute links.



5
6
7
# File 'lib/clickmap/base.rb', line 5

def links
  @links
end

Instance Method Details

#generateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/clickmap/base.rb', line 14

def generate
  doc = Nokogiri::HTML(self.input_html)
  doc.encoding = 'utf-8'
  doc.css("a").each do |link|
    link.attributes["class"] = link.attributes["class"].to_s + " " + self.css_class
    self.links.each do |l|
      if link.attributes['href'].value.match l[:url]
        link.set_attribute("class", "clickmap-link #{self.css_class}")
        link.set_attribute("data-clickmap-count", l[:count])
      end
    end
  end
  doc.to_html
end