Class: BrandEins::Pages::Cover

Inherits:
Object
  • Object
show all
Defined in:
lib/brandeins/pages/cover.rb

Overview

Represents the magazine cover

Instance Method Summary collapse

Constructor Details

#initialize(magazine) ⇒ Cover

Returns a new instance of Cover.



13
14
15
# File 'lib/brandeins/pages/cover.rb', line 13

def initialize(magazine)
  @magazine = magazine
end

Instance Method Details

#cover_file_path_for_path(path) ⇒ Object



53
54
55
# File 'lib/brandeins/pages/cover.rb', line 53

def cover_file_path_for_path(path)
  Pathname.new(path) + file_name
end

#cover_image_urlObject



17
18
19
# File 'lib/brandeins/pages/cover.rb', line 17

def cover_image_url
  @magazine.cover_url
end

#cover_titleObject



21
22
23
# File 'lib/brandeins/pages/cover.rb', line 21

def cover_title
  @magazine.title
end

#create_cover_pdf(image) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/brandeins/pages/cover.rb', line 34

def create_cover_pdf(image)
  Prawn::Document.new do |pdf|
    pdf.text '<font size="18"><b>' + cover_title + '</b></font>',
             align: :center,
             inline_format: true
    if image
      # TODO: get Null Byte?
      # pdf.image image, position: :center, vposition: :center
    end
  end.render
end

#download_cover_imageObject



30
31
32
# File 'lib/brandeins/pages/cover.rb', line 30

def download_cover_image
  fetcher.fetch(cover_image_url)
end

#fetcherObject



61
62
63
# File 'lib/brandeins/pages/cover.rb', line 61

def fetcher
  @fetcher ||= BrandEins::Utils::Fetcher.instance
end

#file_nameObject



57
58
59
# File 'lib/brandeins/pages/cover.rb', line 57

def file_name
  "magazine-cover-#{@magazine.month}-#{@magazine.year}.pdf"
end

#save_to(path) ⇒ Object



46
47
48
49
50
51
# File 'lib/brandeins/pages/cover.rb', line 46

def save_to(path)
  cover_file_path = cover_file_path_for_path(path)
  return cover_file_path if File.exists? cover_file_path
  File.binwrite(cover_file_path, to_pdf)
  cover_file_path
end

#to_pdfObject



25
26
27
28
# File 'lib/brandeins/pages/cover.rb', line 25

def to_pdf
  cover_image = download_cover_image
  create_cover_pdf(cover_image)
end