Class: EmbedHtml::Embeder

Inherits:
Object
  • Object
show all
Defined in:
lib/embed_html/embeder.rb

Constant Summary collapse

MAX_CONCURRENCY =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, logger = Logger.new($stdout), concurrency = MAX_CONCURRENCY) ⇒ Embeder

Returns a new instance of Embeder.



17
18
19
20
21
# File 'lib/embed_html/embeder.rb', line 17

def initialize(url, logger=Logger.new($stdout), concurrency=MAX_CONCURRENCY)
  @logger = logger
  @url = url
  @concurrency = concurrency
end

Instance Attribute Details

#concurrencyObject

Returns the value of attribute concurrency.



15
16
17
# File 'lib/embed_html/embeder.rb', line 15

def concurrency
  @concurrency
end

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/embed_html/embeder.rb', line 14

def logger
  @logger
end

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/embed_html/embeder.rb', line 13

def url
  @url
end

Instance Method Details

#processObject



23
24
25
26
27
28
29
30
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
57
58
59
# File 'lib/embed_html/embeder.rb', line 23

def process
  @logger.info "downloading url: #{@url}"
  html = Typhoeus::Request.get(@url.to_s).body
  doc = Hpricot(html)
  
  hydra = Typhoeus::Hydra.new(:max_concurrency => @concurrency)
  doc.search("//img").each do |img|                
    begin
      hydra.queue create_fetch_file_request(img, 'src')
    rescue StandardError => e
      @logger.error "failed download image: #{img['src']} #{e.inspect}"
    end
  end

  doc.search("//script").each do |script|                
    begin
      if script['src']
        hydra.queue create_fetch_file_request(script, 'src')
      end
    rescue StandardError => e
      @logger.error "failed download script: #{script['src']} #{e.inspect}"
    end
  end

  doc.search("//link").each do |link|
    begin
      hydra.queue create_fetch_file_request(link, 'href')
    rescue StandardError => e
      @logger.error "failed download linked resource: #{link['href']} #{e.inspect}"
    end
  end
  
  hydra.run

  @logger.info "done"            
  doc.to_html      
end

#process_localObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/embed_html/embeder.rb', line 61

def process_local
  @logger.info "downloading url: #{@url}"
  html = open(@url).read
  doc = Hpricot(html)

  doc.search("//img").each do |img|                
    begin
      fetch_file(img, 'src')
    rescue StandardError => e
      @logger.error "failed download image: #{img['src']} #{e.inspect}"
    end
  end

  doc.search("//script").each do |script|                
    begin
      fetch_file(script, 'src')
    rescue StandardError => e
      @logger.error "failed download script: #{script['src']} #{e.inspect}"
    end
  end

  doc.search("//link").each do |link|
    begin
      fetch_file(link, 'href')
    rescue StandardError => e
      @logger.error "failed download linked resource: #{link['href']} #{e.inspect}"
    end
  end

  @logger.info "done"            
  doc.to_html      
end