Class: SnapUrl::Camera::LoadDelegate
- Inherits:
-
OSX::NSObject
- Object
- OSX::NSObject
- SnapUrl::Camera::LoadDelegate
- Defined in:
- lib/core/camera.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#options ⇒ Object
Returns the value of attribute options.
-
#urls ⇒ Object
Returns the value of attribute urls.
Instance Method Summary collapse
- #captureView(view) ⇒ Object
- #fetchUrl(webview) ⇒ Object
- #init ⇒ Object
- #makeFilename(url, bitmap, format) ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #ping(url) ⇒ Object
- #resetWebView(view) ⇒ Object
- #resizeWebView(view) ⇒ Object
- #saveImages(url, bitmap) ⇒ Object
- #webView_didFailLoadWithError_forFrame(webview, error, frame) ⇒ Object
- #webView_didFailProvisionalLoadWithError_forFrame(webview, error, frame) ⇒ Object
- #webView_didFinishLoadForFrame(webview, frame) ⇒ Object
- #writeToFile(url, bitmap, format) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
215 216 217 218 219 220 221 222 |
# File 'lib/core/camera.rb', line 215 def method_missing(name, *args) if args.empty? k = name.to_sym super(name, args) unless @options.keys.include?(k) return @options[k] end super(name, args) end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
71 72 73 |
# File 'lib/core/camera.rb', line 71 def logger @logger end |
#options ⇒ Object
Returns the value of attribute options.
71 72 73 |
# File 'lib/core/camera.rb', line 71 def @options end |
#urls ⇒ Object
Returns the value of attribute urls.
71 72 73 |
# File 'lib/core/camera.rb', line 71 def urls @urls end |
Instance Method Details
#captureView(view) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/core/camera.rb', line 132 def captureView(view) view.lockFocus bitmap = OSX::NSBitmapImageRep.alloc bitmap.initWithFocusedViewRect(view.bounds) view.unlockFocus bitmap end |
#fetchUrl(webview) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/core/camera.rb', line 140 def fetchUrl(webview) OSX::NSApplication.sharedApplication.terminate(self) if @urls.empty? url = String.new(@urls.shift) url.gsub!(/^/, "http:\/\/") unless url =~ /^(http(s)?|file):\/\// @logger.info "Fetching #{url}..." resetWebView(webview) webview.mainFrame.loadRequest(OSX::NSURLRequest.requestWithURL(OSX::NSURL.URLWithString(url))) return if webview.mainFrame.provisionalDataSource fetchUrl(webview) end |
#init ⇒ Object
89 90 91 92 |
# File 'lib/core/camera.rb', line 89 def init @logger = Logger.new(STDOUT) self end |
#makeFilename(url, bitmap, format) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/core/camera.rb', line 155 def makeFilename(url, bitmap, format) if !filename.empty? name = filename elsif useHashForFilename? name = Digest::MD5.new.hexdigest(url) else name = String.new(url) name.gsub!(/\W/, ''); name.gsub!(/^http(s)?/i, ''); name.gsub!(/\//, '.'); end name = "#{Time.now.strftime('%H%M%S')}-#{name}" if name = "#{Time.now.strftime('%Y%m%d')}-#{name}" if datestampInFilename? name = "#{name}-#{bitmap.pixelsWide}x#{bitmap.pixelsHigh}" if sizeInFilename? name = "#{name}-#{format}" if snapFormatInFilename? name = "#{name}.png" name = File.join(File.(outputDirectory), name) unless outputDirectory.empty? name end |
#ping(url) ⇒ Object
211 212 213 |
# File 'lib/core/camera.rb', line 211 def ping(url) open(url) rescue nil end |
#resetWebView(view) ⇒ Object
119 120 121 122 123 |
# File 'lib/core/camera.rb', line 119 def resetWebView(view) rect = OSX::NSMakeRect(0, 0, browser[:width], browser[:height]) view.window.setContentSize([browser[:width], browser[:height]]) view.setFrame(rect) end |
#resizeWebView(view) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/core/camera.rb', line 125 def resizeWebView(view) view.setNeedsDisplay(true) view.displayIfNeeded view.window.setContentSize(view.bounds.size) view.setFrame(view.bounds) end |
#saveImages(url, bitmap) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/core/camera.rb', line 180 def saveImages(url, bitmap) writeToFile(url, bitmap, :fullsize) if snapFormat.include?(:fullsize) if snapFormat.include?(:thumbnail) || snapFormat.include?(:clip) # work out how big the thumbnail is thumbWidth = bitmap.pixelsWide * scaleFactor thumbHeight = bitmap.pixelsHigh * scaleFactor # make the thumbnails in a scratch image scratch = OSX::NSImage.alloc.initWithSize(OSX::NSMakeSize(thumbWidth, thumbHeight)) scratch.lockFocus OSX::NSGraphicsContext.currentContext.setImageInterpolation(OSX::NSImageInterpolationHigh) thumbRect = OSX::NSMakeRect(0.0, 0.0, thumbWidth, thumbHeight) clipRect = OSX::NSMakeRect(0.0, thumbHeight - clip[:height], clip[:width], clip[:height]) bitmap.drawInRect(thumbRect) thumbnail = OSX::NSBitmapImageRep.alloc.initWithFocusedViewRect(thumbRect) clipping = OSX::NSBitmapImageRep.alloc.initWithFocusedViewRect(clipRect) scratch.unlockFocus # save the thumbnails as pngs writeToFile(url, thumbnail, :thumbnail) if snapFormat.include?(:thumbnail) writeToFile(url, clipping, :clip) if snapFormat.include?(:clip) end end |
#webView_didFailLoadWithError_forFrame(webview, error, frame) ⇒ Object
94 95 96 97 |
# File 'lib/core/camera.rb', line 94 def webView_didFailLoadWithError_forFrame(webview, error, frame) @logger.warn "#{error.localizedDescription}" fetchUrl(webview) end |
#webView_didFailProvisionalLoadWithError_forFrame(webview, error, frame) ⇒ Object
99 100 101 102 |
# File 'lib/core/camera.rb', line 99 def webView_didFailProvisionalLoadWithError_forFrame(webview, error, frame) @logger.warn "#{error.localizedDescription}" fetchUrl(webview) end |
#webView_didFinishLoadForFrame(webview, frame) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/core/camera.rb', line 104 def webView_didFinishLoadForFrame(webview, frame) # don't care about subframes return unless (frame == webview.mainFrame) docview = frame.frameView.documentView resizeWebView(docview) @logger.info "Capturing..." url = frame.dataSource.initialRequest.URL.absoluteString bitmap = captureView(docview) saveImages(url, bitmap) @logger.info "Done!" fetchUrl(webview) end |
#writeToFile(url, bitmap, format) ⇒ Object
205 206 207 208 209 |
# File 'lib/core/camera.rb', line 205 def writeToFile(url, bitmap, format) name = makeFilename(url, bitmap, format) FileUtils.mkdir_p File.dirname(name) bitmap.representationUsingType_properties(OSX::NSPNGFileType, nil).writeToFile_atomically(name, true) end |