Class: EPUBInfo::Models::Cover

Inherits:
Object
  • Object
show all
Defined in:
lib/epubinfo/models/cover.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Cover

Returns a new instance of Cover.



16
17
18
19
20
# File 'lib/epubinfo/models/cover.rb', line 16

def initialize(parser)
  @parser = parser
  @path   = epub_cover_file_path
  @content_type = epub_cover_content_type
end

Instance Attribute Details

#content_typeString

Content type of cover file

Returns:

  • (String)


30
31
32
# File 'lib/epubinfo/models/cover.rb', line 30

def content_type
  @content_type
end

Class Method Details

.new(parser) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/epubinfo/models/cover.rb', line 4

def self.new(parser)
  return nil unless EPUBInfo::Parser === parser

  cover = super(parser)

  if cover.exists?
    cover
  else
    nil
  end
end

Instance Method Details

#original_file_nameString

Original name of cover file

Returns:

  • (String)


24
25
26
# File 'lib/epubinfo/models/cover.rb', line 24

def original_file_name
  File.basename(@path) if @path
end

#tempfile(&block) ⇒ File

Cover file Tempfile is used to enable access to cover file If block is passed, the tempfile is passed to it and closed after the block is executed

cover.file do { |f| puts f.size }

Otherwise user is responsible to unlink and close tempfile

file = book.cover.file
file.size
file.close!

Returns:

  • (File)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/epubinfo/models/cover.rb', line 49

def tempfile(&block)
  tempfile = Tempfile.new('epubinfo')
  tempfile.binmode

  cover_file = @parser.zip_file.read(zip_file_path)
  tempfile.write(cover_file)

  if block_given?
    yield tempfile
    tempfile.close!
  else
    # user is responsible for closing file
    tempfile
  end
end