Class: Rapinoe::Keynote

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Keynote

Create a new Keynote instance.

path - The path to the .key file on disk.

Returns a Keynote.



17
18
19
20
21
# File 'lib/rapinoe/keynote.rb', line 17

def initialize(path)
  @path = path
  @name = File.basename(path, ".key")
  extract_key_file
end

Instance Attribute Details

#dataObject

The (first stage) of uncompressed Keynote data in memory.



10
11
12
# File 'lib/rapinoe/keynote.rb', line 10

def data
  @data
end

#nameObject

The name of the file.



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

def name
  @name
end

#pathObject

The path where we can find the .key file.



4
5
6
# File 'lib/rapinoe/keynote.rb', line 4

def path
  @path
end

Instance Method Details

#aspect_ratioObject

The aspect ratio of the deck.

Returns a Symbol, either :widescreen or :standard (4:3).



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rapinoe/keynote.rb', line 31

def aspect_ratio
  path = "/tmp/rapinoe-aspect"
  write_preview_to_file(path)

  dimensions = FastImage.size(path)
  widescreen = (16/9.0)

  if widescreen == (dimensions[0] / dimensions[1].to_f)
    :widescreen
  else
    :standard
  end
end

#colorsObject

The top colors present in the title slide.

This returns a Hash of the color (in RGB) and its percentage in the frame. For example:

{
  [1, 1, 1]   => 0.7296031746031746,
  [8, 12, 15] => 0.13706349206349205,
  [ … ]
}


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rapinoe/keynote.rb', line 60

def colors
  return @colors if @colors

  path = "/tmp/rapinoe-aspect"
  write_preview_to_file(path)

  colors = Miro::DominantColors.new(path)
  by_percentage = colors.by_percentage
  hash   = {}

  colors.to_rgb.each_with_index do |hex, i|
    hash[hex] = by_percentage[i]
  end

  @colors = hash
end

#inspectObject



105
106
107
# File 'lib/rapinoe/keynote.rb', line 105

def inspect
  "<Rapinoe::Keynote: @name=\"#{@name}\", @path=\"#{@path}\", @data=[…]>"
end

#preview_dataObject

The binary data associated with the .jpg Keynote writes to make a high(ish) quality preview version of the deck. You likely will want to access this via #write_preview_to_file, unless you have specific needs for the binary data.

Returns the contents of preview.jpg.



89
90
91
# File 'lib/rapinoe/keynote.rb', line 89

def preview_data
  @data.find_entry("preview.jpg").get_input_stream.read
end

#sizeObject

Returns the file size of the Keynote file in bytes.



24
25
26
# File 'lib/rapinoe/keynote.rb', line 24

def size
  File.size(path)
end

#slidesObject



77
78
79
80
81
# File 'lib/rapinoe/keynote.rb', line 77

def slides
  @data.glob("Data/st*").map do |preview_jpg_data|
    Slide.new(preview_jpg_data.get_input_stream.read)
  end
end

#widescreen?Boolean

Is it widescreen? Does it blend?

Returns:

  • (Boolean)


46
47
48
# File 'lib/rapinoe/keynote.rb', line 46

def widescreen?
  aspect_ratio == :widescreen
end

#write_preview_to_file(path) ⇒ Object

Writes Keynote’s preview.jpg to disk somewhere.

path - The path to the new file you want to write.

Returns nothing.



98
99
100
101
102
103
# File 'lib/rapinoe/keynote.rb', line 98

def write_preview_to_file(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'wb') do |out|
    out.write(preview_data)
  end
end