Class: Mangdown::Page

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Equality, Logging
Defined in:
lib/mangdown/page.rb

Overview

Mangdown page

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Equality

#<=>

Constructor Details

#initialize(page) ⇒ Page

Returns a new instance of Page.



17
18
19
# File 'lib/mangdown/page.rb', line 17

def initialize(page)
  @page = page
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



11
12
13
# File 'lib/mangdown/page.rb', line 11

def page
  @page
end

Instance Method Details

#append_file_data(_dir, data) ⇒ Object

rubocop:enable Metrics/MethodLength



64
65
66
# File 'lib/mangdown/page.rb', line 64

def append_file_data(_dir, data)
  File.open(to_path, 'ab') { |file| file.write(data) }
end

#append_file_ext(dir = nil) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/mangdown/page.rb', line 68

def append_file_ext(dir = nil)
  setup_path(dir) if dir
  path = to_path
  ext = Tools.image_extension(path)
  filename = "#{path}.#{ext}"

  FileUtils.mv(path, filename)
end

#chapterObject



21
22
23
# File 'lib/mangdown/page.rb', line 21

def chapter
  @chapter ||= Mangdown.chapter(page.chapter)
end

#delete_files!(dir) ⇒ Object

cleanup existing file (all extensions)



84
85
86
# File 'lib/mangdown/page.rb', line 84

def delete_files!(dir)
  File.delete(to_path) while setup_path(dir) && File.exist?(to_path)
end

#download_to(dir = Dir.pwd, opts = { force_download: false }) ⇒ Object

rubocop:disable Metrics/MethodLength



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mangdown/page.rb', line 43

def download_to(dir = Dir.pwd, opts = { force_download: false })
  delete_files!(dir) if opts[:force_download]

  return if file_exist?(dir)

  image = Tools.get(uri)

  append_file_data(dir, image)
  append_file_ext(dir)
rescue StandardError => error
  logger.error({
    msg: 'Failed to download page',
    page: self,
    uri: uri,
    error: error,
    error_msg: error.message,
    backtrace: error.backtrace
  }.to_s)
end

#file_exist?(dir = nil) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/mangdown/page.rb', line 77

def file_exist?(dir = nil)
  setup_path(dir) if dir

  Dir.entries(dir).any? { |file| file.to_s[to_path.basename.to_s] }
end

#pathObject Also known as: to_path



25
26
27
# File 'lib/mangdown/page.rb', line 25

def path
  @path ||= setup_path
end

#setup_path(dir = nil) ⇒ Object

Set path of page to file path if a file exists or to path without file extension if file is not found. File extensions are appended only after the file has been downloaded.



33
34
35
36
37
38
39
40
# File 'lib/mangdown/page.rb', line 33

def setup_path(dir = nil)
  dir ||= chapter.path
  dir = Tools.valid_path_name(dir)
  name = self.name.tr('/', '')
  file = Dir.entries(dir).find { |f| f[name] } if Dir.exist?(dir)
  path = Tools.file_join(dir, file || name)
  @path = Tools.relative_or_absolute_path(path)
end