class Exiftool::FieldParser

Exiftool FiledParser Class

Constants

FRACTION_RE
WORD_BOUNDARY_RES
YMD_RE
ZERO_DATE_RE

Attributes

display_key[R]
key[R]
raw_value[R]
sym_key[R]

Public Class Methods

new(key, raw_value) click to toggle source
# File lib/exiftool/field_parser.rb, line 15
def initialize(key, raw_value)
  @key = key
  @display_key = WORD_BOUNDARY_RES.inject(key) { |k, regex| k.gsub(regex, '\1 \2') }
  @sym_key = display_key.downcase.gsub(' ', '_').to_sym
  @raw_value = raw_value
end

Public Instance Methods

civil_date() click to toggle source
# File lib/exiftool/field_parser.rb, line 38
def civil_date
  return unless date? && !zero_date?

  ymd = raw_value.scan(YMD_RE).first
  return unless ymd

  ymd_a = ymd.map(&:to_i)
  Date.civil(*ymd_a) if Date.valid_civil?(*ymd_a)
end
value() click to toggle source
# File lib/exiftool/field_parser.rb, line 22
def value
  @value ||= if lat_long?
               as_lat_long
             elsif date?
               as_date
             elsif fraction?
               as_fraction
             else
               raw_value
             end
rescue StandardError => e
  # :nocov:
  "Warning: Parsing '#{raw_value}' for attribute '#{key}' raised #{e.message}"
  # :nocov:
end

Private Instance Methods

as_date() click to toggle source
# File lib/exiftool/field_parser.rb, line 71
def as_date
  try_parse { Time.strptime(raw_value, '%Y:%m:%d %H:%M:%S%z') } ||
    try_parse { Time.strptime(raw_value, '%Y:%m:%d %H:%M:%S.%L%z') } ||
    raw_value
end
as_fraction() click to toggle source
# File lib/exiftool/field_parser.rb, line 87
def as_fraction
  scan = raw_value.scan(FRACTION_RE).first
  Rational(*scan.map(&:to_i)) unless scan.nil?
end
as_lat_long() click to toggle source
# File lib/exiftool/field_parser.rb, line 54
def as_lat_long
  return raw_value if raw_value.is_a?(Numeric)

  value, direction = raw_value.split
  return unless value =~ /\A\d+\.?\d*\z/

  value.to_f * (%w[S W].include?(direction) ? -1 : 1)
end
date?() click to toggle source
# File lib/exiftool/field_parser.rb, line 63
def date?
  raw_value.is_a?(String) && display_key =~ /\bdate\b/i
end
fraction?() click to toggle source
# File lib/exiftool/field_parser.rb, line 83
def fraction?
  raw_value.is_a?(String) && raw_value =~ FRACTION_RE
end
lat_long?() click to toggle source
# File lib/exiftool/field_parser.rb, line 50
def lat_long?
  sym_key == :gps_latitude || sym_key == :gps_longitude
end
try_parse() { || ... } click to toggle source
# File lib/exiftool/field_parser.rb, line 77
def try_parse
  yield
rescue ArgumentError
  nil
end
zero_date?() click to toggle source
# File lib/exiftool/field_parser.rb, line 67
def zero_date?
  raw_value =~ ZERO_DATE_RE
end