Class: WebArchiver

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.archive(url, file, seconds) ⇒ Object



7
8
9
# File 'lib/webarchiver.rb', line 7

def self.archive url, file, seconds
  self.archiveURL url, toFile:file, timeout:seconds
end

.archiveURL(url, toFile: file, timeout: seconds) ⇒ Object



11
12
13
14
# File 'lib/webarchiver.rb', line 11

def self.archiveURL url, toFile:file, timeout:seconds
  archiver = WebArchiver.alloc.initWithURL(url, toFile:file)
  archiver.archiveWithTimeout(seconds.to_i)
end

Instance Method Details

#archiveWithTimeout(seconds) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/webarchiver.rb', line 27

def archiveWithTimeout seconds
  request = NSURLRequest.requestWithURL(NSURL.URLWithString(@url))
  @webView.mainFrame.loadRequest(request)

  runLoop = NSRunLoop.currentRunLoop
  toDate  = NSDate.dateWithTimeIntervalSinceNow(seconds)

  while !done?
    runLoop.runMode(NSDefaultRunLoopMode, beforeDate:toDate)
    break if NSDate.date.earlierDate(toDate) == toDate
  end

  @saved_file
end

#done?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/webarchiver.rb', line 42

def done?
  @done
end

#initWithURL(url, toFile: file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/webarchiver.rb', line 16

def initWithURL url, toFile:file
  @url, @file = url, file

  @webView = WebView.alloc.init
  @webView.frameLoadDelegate = self

  @done = false

  self
end

#webView(sender, didFailLoadWithError: error, forFrame: frame) ⇒ Object

frameLoadDelegate methods



48
49
50
51
52
53
54
55
# File 'lib/webarchiver.rb', line 48

def webView sender, didFinishLoadForFrame:frame
   data        = frame.dataSource.webArchive.data
   @saved_file = cleanseFilename(@file || frame.dataSource.pageTitle)

   data.writeToFile(@saved_file, atomically:false)

   @done = true
end