Sha256: dc489d7bbfcba829f0a1042563fec5b83905b054839e6397eb8bf411cc659470
Contents?: true
Size: 1.68 KB
Versions: 1
Compression:
Stored size: 1.68 KB
Contents
require 'capybara/poltergeist' require 'mini_magick' Capybara.default_wait_time = 15 module Photograph class Artist attr_accessor :options attr_reader :image MissingUrlError = Class.new(Exception) DefaultOptions = { :x => 0, # top left position :y => 0, :w => 1280, # bottom right position :h => 1024, :wait => 0.5, # if selector is nil, wait 1 seconds before taking the screenshot :selector => nil # wait until the selector matches to take the screenshot } def self.browser @browser ||= Capybara::Session.new :poltergeist end def browser self.class.browser end def initialize options={} raise MissingUrlError unless options[:url] @options = DefaultOptions.merge(options) end def shoot! &block @image = capture if block_given? yield @image clean! else @image end end def capture browser.visit @options[:url] if @options[:selector] browser.wait_until do browser.has_css? @options[:selector] end else sleep @options[:wait] end @tempfile = Tempfile.new(['photograph','.png']) browser.driver.render @tempfile.path, :width => options[:w] + options[:x], :height => options[:h] + options[:y] @image = adjust_image end def adjust_image image = MiniMagick::Image.read @tempfile if options[:h] && options[:w] image.crop "#{options[:w]}x#{options[:h]}+#{options[:x]}+#{options[:y]}" image.write @tempfile end image end def clean! @tempfile.unlink end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
photograph-0.0.2 | lib/photograph/artist.rb |