Class: Mangdown::Page
- Inherits:
-
Object
- Object
- Mangdown::Page
- Extended by:
- Forwardable
- Defined in:
- lib/mangdown/page.rb
Overview
Mangdown page
Instance Attribute Summary collapse
-
#page ⇒ Object
readonly
Returns the value of attribute page.
Instance Method Summary collapse
-
#append_file_data(_dir, data) ⇒ Object
rubocop:enable Metrics/MethodLength.
- #append_file_ext(dir = nil) ⇒ Object
- #chapter ⇒ Object
-
#delete_files!(dir) ⇒ Object
cleanup existing file (all extensions).
-
#download_to(dir = Dir.pwd, opts = { force_download: false }) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #file_exist?(dir = nil) ⇒ Boolean
-
#initialize(page) ⇒ Page
constructor
A new instance of Page.
- #path ⇒ Object (also: #to_path)
-
#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.
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
#page ⇒ Object (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 |
#chapter ⇒ Object
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., backtrace: error.backtrace }.to_s) end |
#file_exist?(dir = nil) ⇒ 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 |
#path ⇒ Object 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 |