Module: Plumnailer::Doc

Defined in:
lib/plumnailer/doc.rb

Overview

Nokogiri::HTML:Document mixin.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#source_urlObject

Returns the value of attribute source_url.



40
41
42
# File 'lib/plumnailer/doc.rb', line 40

def source_url
  @source_url
end

Instance Method Details

#doc_base_hrefObject

Get the href attribute of the base tag from the head of the document.



9
10
11
12
# File 'lib/plumnailer/doc.rb', line 9

def doc_base_href
  base = at('//head/base')
  base['href']  if base
end

#img_abs_urls(base_url = nil) ⇒ Object

Return a list of the absolute urls of all imgs in the document.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/plumnailer/doc.rb', line 20

def img_abs_urls(base_url=nil)
  result = []

  img_srcs.each do |i|
    begin
      u = URI(i)
    rescue URI::InvalidURIError
      next
    end

    result <<  if u.is_a?(URI::HTTP)
      u
    else
      URI.join(base_url || doc_base_href || source_url, i)
    end
  end

  result
end

#img_srcsObject

Return a list of the src attributes of all img tags.



15
16
17
# File 'lib/plumnailer/doc.rb', line 15

def img_srcs
  search('//img').map { |x| x['src'] }.compact
end