Class: URLArchiver
- Inherits:
-
Object
- Object
- URLArchiver
- Defined in:
- lib/urlarchiver.rb
Instance Method Summary collapse
-
#archiveone(url) ⇒ Object
Archive a single url.
-
#genJSON ⇒ Object
Generate JSON from output.
-
#initialize(type) ⇒ URLArchiver
constructor
A new instance of URLArchiver.
-
#multiarchive(json, field) ⇒ Object
Archive multiple fields in a json.
Constructor Details
#initialize(type) ⇒ URLArchiver
Returns a new instance of URLArchiver.
8 9 10 |
# File 'lib/urlarchiver.rb', line 8 def initialize(type) @type = type end |
Instance Method Details
#archiveone(url) ⇒ Object
Archive a single url
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/urlarchiver.rb', line 13 def archiveone(url) begin html = Nokogiri::HTML(open(url)) url.gsub!("https", "http") k = PDFKit.new(url, :page_size => "Letter") pdf = k.to_pdf # Save files FileUtils::mkdir_p 'public/uploads/archive' filepath = "public/uploads/archive/"+url.gsub("http", "https").gsub("/", "").gsub(".", "").gsub(":", "")+".pdf" File.open(filepath.gsub(".pdf", ".html"), 'w') { |file| file.write(html) } file = k.to_file(filepath) # Save output if single url if @type == "single" @output = Hash.new @output[:pdf_path] = filepath @output[:html_path] = filepath.gsub(".pdf", ".html") @output[:text] = html.text end # Return file paths out = Hash.new out[:pdf_path] = filepath out[:html_path] = filepath.gsub(".pdf", ".html") return out rescue end end |
#genJSON ⇒ Object
Generate JSON from output
70 71 72 |
# File 'lib/urlarchiver.rb', line 70 def genJSON JSON.pretty_generate(@output) end |
#multiarchive(json, field) ⇒ Object
Archive multiple fields in a json
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/urlarchiver.rb', line 44 def multiarchive(json, field) pjson = JSON.parse(json) htmlfield = field+"_htmlpath" pdffield = field+"_pdfpath" @output = Array.new pjson.each do |p| pitem = Hash.new paths = archiveone(p[field]) # Save paths if !(paths == nil) pitem[htmlfield] = paths[:pdf_path] pitem[pdffield] = paths[:html_path] end # Save other fields p.each do |key, value| pitem[key] = value end @output.push(pitem) end end |