Module: EbookRenamer
- Defined in:
- lib/ebook_renamer/cli.rb,
lib/ebook_renamer/errors.rb,
lib/ebook_renamer/version.rb,
lib/ebook_renamer/ebook_renamer.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.2.5"
Instance Method Summary collapse
-
#formatted_name(meta_hash = {}, fields = {}) ⇒ Object
Return formatted file name using the metadata values.
-
#meta(filename, binary = "ebook-meta") ⇒ String
Extract meta data from the input file using the ebook-meta tool.
-
#meta_to_hash(text) ⇒ Hash<String,String>
Convert the output string to hash.
Instance Method Details
#formatted_name(meta_hash = {}, fields = {}) ⇒ Object
Return formatted file name using the metadata values
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ebook_renamer/ebook_renamer.rb', line 49 def formatted_name( = {}, fields = {}) if hash.nil? || fields.nil? fail ArgumentError.new("Argument must not be nil") end # Let's not continue if we have no title metadata fail "No title found" unless .fetch("title", nil) # The keys that we get from the 'mdls' or 'exiftool' args = { keys: [ "title", "author(s)" ], sep_char: " " }.merge(fields) keys = args[:keys] sep_char = args[:sep_char] # Note: only show if we have the value for title result = [] keys.each do |key| value = .fetch(key, nil) # Note: don't add 'Author(s)' => 'Unknown' to keep the result clean if value && value.downcase != "unknown" result << [key] end end result.join(sep_char) end |
#meta(filename, binary = "ebook-meta") ⇒ String
Extract meta data from the input file using the ebook-meta tool
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/ebook_renamer/ebook_renamer.rb', line 7 def (filename, binary = "ebook-meta") fail Errors::EbookMetaNotInstall, "Need to install ebook-meta to use this gem" if AgileUtils::Helper.which(binary).nil? command = [ binary, Shellwords.escape(filename) ] stdout_str, stderr_str, status = Open3.capture3(command.join(" ")) fail "Problem processing #{filename}" unless status.success? stdout_str end |
#meta_to_hash(text) ⇒ Hash<String,String>
Convert the output string to hash
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ebook_renamer/ebook_renamer.rb', line 23 def (text) hash = {} return hash if text.nil? result_list = [] text.split(/\n/).each do || # split by the first ':' string list = .split /^(.*?):/ # ignore the empty string element list.delete_at(0) unless list.empty? list.map(&:strip!) # downcase the first item to make it easy result_list << [list[0].downcase, list[1]] hash = Hash[*result_list.flatten] end end hash end |