Class: NauktisUtils::DateExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/nauktis_utils/date_extractor.rb

Constant Summary collapse

DATETIME_FORMAT =
"%Y-%m-%d_%H-%M-%S_"
DATETIME_REGEX =
/^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDateExtractor

Returns a new instance of DateExtractor.



7
8
9
# File 'lib/nauktis_utils/date_extractor.rb', line 7

def initialize
  @counters = Hash.new(0)
end

Instance Attribute Details

#countersObject (readonly)

Returns the value of attribute counters.



3
4
5
# File 'lib/nauktis_utils/date_extractor.rb', line 3

def counters
  @counters
end

Instance Method Details

#extract_datetime(file_path) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/nauktis_utils/date_extractor.rb', line 11

def extract_datetime(file_path)
  meta = exiftool(FileBrowser.ensure_valid_file(file_path))
  ['DateTimeOriginal', 'MediaCreateDate'].each do |tag|
    return DateTime.parse(meta[tag]) if meta[tag]
  end
  Tracer.warn "Could not extract date from #{file_path}"
  nil
end

#rename(file_path) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/nauktis_utils/date_extractor.rb', line 20

def rename(file_path)
  file_path = FileBrowser.ensure_valid_file(file_path)
  @counters[:total] += 1
  unless File.basename(file_path) =~ DATETIME_REGEX
    prepend_date(file_path)
  end
end