Sha256: 5619483ec49768ce63f484b0cb0fcf417ab709f5fd427df1e30e11ef0d27ef39

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

module Rapinoe
  class Keynote
    # The path where we can find the .key file.
    attr_accessor :path

    # The name of the file.
    attr_accessor :name

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

    # Create a new Keynote instance.
    #
    #   path - The path to the .key file on disk.
    #
    # Returns a Keynote.
    def initialize(path)
      @path = path
      @name = File.basename(path, ".key")
      extract_key_file
    end

    # Returns the file size of the Keynote file in bytes.
    def size
      File.size(path)
    end

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

    # 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.
    def preview_data
      @data.find_entry("preview.jpg").get_input_stream.read
    end

    # Writes Keynote's preview.jpg to disk somewhere.
    #
    #   path - The path to the new file you want to write.
    #
    # Returns nothing.
    def write_preview_to_file(path)
      File.open(path, 'wb') do |out|
        out.write(preview_data)
      end
    end

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

  private

    # .key files basically just try to masquerade as .zip files. Once we extract
    # the main directory structure (in memory), we can start looking into what's
    # inside.
    #
    # Returns a complex data structure that maps to Zip::File.
    def extract_key_file
      @data = Zip::File.open(path)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rapinoe-0.0.1 lib/rapinoe/keynote.rb