Class: EmbedHtml::Embeder
- Inherits:
-
Object
- Object
- EmbedHtml::Embeder
- Defined in:
- lib/embed_html/embeder.rb
Constant Summary collapse
- MAX_CONCURRENCY =
5
Instance Attribute Summary collapse
-
#base_dirname ⇒ Object
Returns the value of attribute base_dirname.
-
#concurrency ⇒ Object
Returns the value of attribute concurrency.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#url_or_html ⇒ Object
Returns the value of attribute url_or_html.
Instance Method Summary collapse
-
#initialize(url_or_html, logger = Logger.new($stdout), concurrency = MAX_CONCURRENCY) ⇒ Embeder
constructor
A new instance of Embeder.
- #process ⇒ Object
- #process_local ⇒ Object
Constructor Details
#initialize(url_or_html, logger = Logger.new($stdout), concurrency = MAX_CONCURRENCY) ⇒ Embeder
Returns a new instance of Embeder.
18 19 20 21 22 |
# File 'lib/embed_html/embeder.rb', line 18 def initialize(url_or_html, logger=Logger.new($stdout), concurrency=MAX_CONCURRENCY) @logger = logger @url_or_html = url_or_html @concurrency = concurrency end |
Instance Attribute Details
#base_dirname ⇒ Object
Returns the value of attribute base_dirname.
16 17 18 |
# File 'lib/embed_html/embeder.rb', line 16 def base_dirname @base_dirname end |
#concurrency ⇒ Object
Returns the value of attribute concurrency.
15 16 17 |
# File 'lib/embed_html/embeder.rb', line 15 def concurrency @concurrency end |
#logger ⇒ Object
Returns the value of attribute logger.
14 15 16 |
# File 'lib/embed_html/embeder.rb', line 14 def logger @logger end |
#url_or_html ⇒ Object
Returns the value of attribute url_or_html.
13 14 15 |
# File 'lib/embed_html/embeder.rb', line 13 def url_or_html @url_or_html end |
Instance Method Details
#process ⇒ Object
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 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/embed_html/embeder.rb', line 24 def process # @logger.info "downloading url: #{@url_or_html}" html = (@url_or_html =~ /$http/) ? Typhoeus::Request.get(@url_or_html.to_s).body : @url_or_html doc = Hpricot(html) hydra = Typhoeus::Hydra.new(:max_concurrency => @concurrency) doc.search("//img").each do |img| begin if img['src']=~ /^http/ hydra.queue create_fetch_file_request(img, 'src') else fetch_file(img, 'src') end rescue StandardError => e @logger.error "failed download image: #{img['src']} #{e.inspect}" end end doc.search("//script").each do |script| begin if script['src'] and script['src'] =~ /^http/ hydra.queue create_fetch_file_request(script, 'src') elsif script['src'] fetch_file(script, 'src') end rescue StandardError => e @logger.error "failed download script: #{script['src']} #{e.inspect}" end end doc.search("//link").each do |link| begin url = link['href'] if url =~ /^http/ hydra.queue create_fetch_file_request(link, 'href') else fetch_file(link, 'href') end 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_local ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/embed_html/embeder.rb', line 73 def process_local # @logger.info "downloading url: #{@url_or_html}" html = open(@url_or_html).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 |