Class: GreenOnion::Screenshot
- Inherits:
-
Object
- Object
- GreenOnion::Screenshot
- Defined in:
- lib/green_onion/screenshot.rb
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#dimensions ⇒ Object
readonly
Returns the value of attribute dimensions.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#paths_hash ⇒ Object
readonly
Returns the value of attribute paths_hash.
-
#skin_name ⇒ Object
readonly
Returns the value of attribute skin_name.
Instance Method Summary collapse
- #create_dir(dir) ⇒ Object
- #destroy(url) ⇒ Object
- #destroy_files(org, fresh) ⇒ Object
- #file_namer ⇒ Object
- #get_path(url) ⇒ Object
-
#initialize(params = {}) ⇒ Screenshot
constructor
A new instance of Screenshot.
- #test_screenshot(url) ⇒ Object
- #url_matcher(url) ⇒ Object
- #url_to_path(url) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Screenshot
Returns a new instance of Screenshot.
8 9 10 11 12 13 |
# File 'lib/green_onion/screenshot.rb', line 8 def initialize(params={}) @dir = params[:dir] @skin_name = params[:skin_name] @browser = params[:browser] @paths_hash = {} end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
6 7 8 |
# File 'lib/green_onion/screenshot.rb', line 6 def browser @browser end |
#dimensions ⇒ Object (readonly)
Returns the value of attribute dimensions.
6 7 8 |
# File 'lib/green_onion/screenshot.rb', line 6 def dimensions @dimensions end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
6 7 8 |
# File 'lib/green_onion/screenshot.rb', line 6 def dir @dir end |
#paths_hash ⇒ Object (readonly)
Returns the value of attribute paths_hash.
6 7 8 |
# File 'lib/green_onion/screenshot.rb', line 6 def paths_hash @paths_hash end |
#skin_name ⇒ Object (readonly)
Returns the value of attribute skin_name.
6 7 8 |
# File 'lib/green_onion/screenshot.rb', line 6 def skin_name @skin_name end |
Instance Method Details
#create_dir(dir) ⇒ Object
47 48 49 50 51 |
# File 'lib/green_onion/screenshot.rb', line 47 def create_dir(dir) unless Dir.exist?(dir) FileUtils.mkdir(dir) end end |
#destroy(url) ⇒ Object
62 63 64 65 |
# File 'lib/green_onion/screenshot.rb', line 62 def destroy(url) get_path(url) destroy_files(@paths_hash[:original], @paths_hash[:fresh]) end |
#destroy_files(org, fresh) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/green_onion/screenshot.rb', line 67 def destroy_files(org, fresh) if File.exist?(org) FileUtils.rm(org) if File.exist?(fresh) FileUtils.rm(fresh) end end end |
#file_namer ⇒ Object
40 41 42 43 44 45 |
# File 'lib/green_onion/screenshot.rb', line 40 def file_namer @filename.slice!(/^\//) # remove the beginning "/" if there is one @filename = @filename.gsub(@skin_name[:match], @skin_name[:replace]) # by default, all "/" in a URI string will be replaced with "_" @filename = @skin_name[:prefix] + @filename if @skin_name[:prefix] # add on a prefix defined in the configuration block @paths_hash[:original] = "#{@dir}/#{@filename}.png" end |
#get_path(url) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/green_onion/screenshot.rb', line 53 def get_path(url) url_matcher(url) if @filename.empty? || @filename == '/' @paths_hash[:original] = "#{@dir}/#{@skin_name[:root]}.png" else file_namer end end |
#test_screenshot(url) ⇒ Object
15 16 17 18 19 |
# File 'lib/green_onion/screenshot.rb', line 15 def test_screenshot(url) url_to_path(url) create_dir(@dir) @browser.snap_screenshot(url, @shot_path) end |
#url_matcher(url) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/green_onion/screenshot.rb', line 31 def url_matcher(url) url_match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/).to_a.compact if url_match.length >= 5 @filename = url_match[5] else raise Errors::IllformattedURL.new "Your URL is incorrectly formatted. Please make sure to use http://" end end |
#url_to_path(url) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/green_onion/screenshot.rb', line 21 def url_to_path(url) get_path(url) if File.exist?(@paths_hash[:original]) @paths_hash[:fresh] = @paths_hash[:original].dup.insert(-5, '_fresh') @shot_path = @paths_hash[:fresh] else @shot_path = @paths_hash[:original] end end |