Class: ISA::Session

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

Overview

Main class for managing the capture sessions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Session

Initialize a capture session Takes a number of args:

  • session (optional) – name for the capture session

  • dir (optional) – directory to save the screenshots

  • capture (required) – lambda for performing a screenshot

The lambda should take a single argument filename:

take_screenshot = ->(filename) {
  code_to_perform_screenshot( :save_to => filename )
}

20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/isa/session.rb', line 20

def initialize( args )
  @name = args[:session] || Time.now.to_i
  @code = args[:capture] or raise 'Need to provide a lambda to the constructor'
  @tmp_dir = "/tmp/isa/#{@name}"
  @dir = args[:dir] || @tmp_dir
  
  FileUtils.mkdir_p(@dir)
  FileUtils.mkdir_p(@tmp_dir)
  
  File.directory?(@dir) or raise "Couldn't create session directory for screen grabs"
  
  @images = Magick::ImageList.new
end

Instance Attribute Details

#checkpointObject

Returns the value of attribute checkpoint


9
10
11
# File 'lib/isa/session.rb', line 9

def checkpoint
  @checkpoint
end

#codeObject

Returns the value of attribute code


9
10
11
# File 'lib/isa/session.rb', line 9

def code
  @code
end

#dirObject

Returns the value of attribute dir


9
10
11
# File 'lib/isa/session.rb', line 9

def dir
  @dir
end

#imagesObject

Returns the value of attribute images


9
10
11
# File 'lib/isa/session.rb', line 9

def images
  @images
end

#last_captureObject

Returns the value of attribute last_capture


9
10
11
# File 'lib/isa/session.rb', line 9

def last_capture
  @last_capture
end

#nameObject

Returns the value of attribute name


9
10
11
# File 'lib/isa/session.rb', line 9

def name
  @name
end

#tmp_dirObject

Returns the value of attribute tmp_dir


9
10
11
# File 'lib/isa/session.rb', line 9

def tmp_dir
  @tmp_dir
end

Instance Method Details

#capture(checkpoint = nil) ⇒ Object

Perform a screenshot capture


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/isa/session.rb', line 35

def capture(checkpoint = nil)
  
  if checkpoint
    @checkpoint = checkpoint
  end
  
  filename = (@checkpoint ? @checkpoint.to_s + '-' : '') + Time.now.to_i.to_s + '.png'
  
  file = "#{tmp_dir}/#{filename}"
  
  capture_time = Time.now
  code.call(file)
  
  File.exist? file or raise "Couldn't capture screenshot"
  
  image = Magick::ImageList.new(file).first
  
  if @last_capture
    delay = (capture_time - @last_capture).to_i
    images.last.delay = delay
  end
  @last_capture = capture_time
  
  images << image
  image
end

#diffObject

Performs a capture and diffs it with the last image


63
64
65
66
67
68
69
70
71
72
# File 'lib/isa/session.rb', line 63

def diff
  if images.count == 0
    self.capture
  end
  
  last = images.last
  current = self.capture
  
  last.difference(current)[0].to_i
end

#end(filename = nil) ⇒ Object

Finalize the capture session, and create an animated gif of the capture session


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/isa/session.rb', line 76

def end(filename = nil)
  if !filename
    filename = "#{@dir}/#{@name}.gif"
  else
    filename = "#{@dir}/#{@filename}"
  end
  images.ticks_per_second = 2
  puts "Writing to #{filename}"
  width = 750
  height = 750
  images.each {|i|  i.resize_to_fit!(width, height) }
  images.write(filename)
end