Skip to content
This repository
Browse code

Merge pull request #4 from ethansr/from_hash

from_hash applies type conversion to values
  • Loading branch information...
commit d911d564adab47825a2957f025d6425eb8b4812d 2 parents 36f9351 + 73e8cef
authored July 09, 2012
18  lib/mini_exiftool.rb
@@ -65,7 +65,7 @@ def initialize filename=nil, opts={}
65 65
 
66 66
   def initialize_from_hash hash # :nodoc:
67 67
     hash.each_pair do |tag,value|
68  
-      set_value tag, value
  68
+      set_value tag, perform_conversions(value)
69 69
     end
70 70
     set_attributes_by_heuristic
71 71
     self
@@ -198,7 +198,7 @@ def to_yaml
198 198
     to_hash.to_yaml
199 199
   end
200 200
 
201  
-  # Create a MiniExiftool instance from a hash
  201
+  # Create a MiniExiftool instance from a hash. Default value conversions will be applied if neccesary.
202 202
   def self.from_hash hash
203 203
     instance = MiniExiftool.new
204 204
     instance.initialize_from_hash hash
@@ -325,7 +325,14 @@ def parse_output
325 325
 
326 326
   def parse_line line
327 327
     if line =~ /^([^\t]+)\t(.*)$/
328  
-      tag, value = $1, $2
  328
+      tag, value = $1, perform_conversions($2)
  329
+    else
  330
+      raise MiniExiftool::Error.new("Malformed line #{line.inspect} of exiftool output.")
  331
+    end
  332
+    return [tag, value]
  333
+  end
  334
+
  335
+  def perform_conversions(value)
329 336
       case value
330 337
       when /^\d{4}:\d\d:\d\d \d\d:\d\d:\d\d/
331 338
         s = value.sub(/^(\d+):(\d+):/, '\1-\2-')
@@ -353,10 +360,7 @@ def parse_line line
353 360
       when /#{@@separator}/
354 361
         value = value.split @@separator
355 362
       end
356  
-    else
357  
-      raise MiniExiftool::Error.new("Malformed line #{line.inspect} of exiftool output.")
358  
-    end
359  
-    return [tag, value]
  363
+      value
360 364
   end
361 365
 
362 366
   def set_value tag, value
106  test/data/test.jpg.json
... ...
@@ -0,0 +1,106 @@
  1
+[{
  2
+  "SourceFile": "test/data/test.jpg",
  3
+  "ExifToolVersion": 8.77,
  4
+  "FileName": "test.jpg",
  5
+  "Directory": "test/data",
  6
+  "FileSize": "46 kB",
  7
+  "FileModifyDate": "2012:07:05 20:28:24-07:00",
  8
+  "FilePermissions": "rw-r--r--",
  9
+  "FileType": "JPEG",
  10
+  "MIMEType": "image/jpeg",
  11
+  "JFIFVersion": 1.01,
  12
+  "ExifByteOrder": "Big-endian (Motorola, MM)",
  13
+  "ImageDescription": "KONICA MINOLTA DIGITAL CAMERA",
  14
+  "Make": "KONICA MINOLTA",
  15
+  "Model": "DYNAX 7D",
  16
+  "Orientation": "Horizontal (normal)",
  17
+  "XResolution": 72,
  18
+  "YResolution": 72,
  19
+  "ResolutionUnit": "inches",
  20
+  "Software": "DYNAX 7D v1.10",
  21
+  "ModifyDate": "2005:09:13 20:08:50",
  22
+  "YCbCrPositioning": "Centered",
  23
+  "ExposureTime": "1/60",
  24
+  "FNumber": 9.5,
  25
+  "ExposureProgram": "Program AE",
  26
+  "ISO": 400,
  27
+  "ExifVersion": "0221",
  28
+  "DateTimeOriginal": "2005:09:13 20:08:50",
  29
+  "CreateDate": "2005:09:13 20:08:50",
  30
+  "ComponentsConfiguration": "Y, Cb, Cr, -",
  31
+  "BrightnessValue": 4.5,
  32
+  "ExposureCompensation": -1,
  33
+  "MaxApertureValue": 4.5,
  34
+  "MeteringMode": "Multi-segment",
  35
+  "LightSource": "Unknown",
  36
+  "Flash": "Off, Did not fire",
  37
+  "FocalLength": "75.0 mm",
  38
+  "SubjectArea": "1504 1000 256 304",
  39
+  "MakerNoteVersion": "MLT0",
  40
+  "MinoltaImageSize": "Large",
  41
+  "WhiteBalance": "Auto",
  42
+  "FocusMode": "AF-A",
  43
+  "AFPoints": "Center",
  44
+  "FlashMode": "Normal",
  45
+  "ISOSetting": 400,
  46
+  "FreeMemoryCardImages": 202,
  47
+  "HueAdjustment": 0,
  48
+  "Rotation": "Horizontal (normal)",
  49
+  "ImageNumber": 6,
  50
+  "NoiseReduction": "Unknown (2)",
  51
+  "ImageNumber2": 50,
  52
+  "ZoneMatchingOn": "Off",
  53
+  "CompressedImageSize": 1598477,
  54
+  "PreviewImageStart": 39152,
  55
+  "PreviewImageLength": 0,
  56
+  "SceneMode": "Standard",
  57
+  "ColorMode": "Natural sRGB",
  58
+  "MinoltaQuality": "Fine",
  59
+  "FlashExposureComp": 0,
  60
+  "Teleconverter": "None",
  61
+  "ImageStabilization": "On",
  62
+  "ZoneMatching": "ISO Setting Used",
  63
+  "ColorTemperature": 0,
  64
+  "LensType": "Minolta AF 28-135mm F4-4.5 or Sigma Lens",
  65
+  "UserComment": "",
  66
+  "FlashpixVersion": "0100",
  67
+  "ColorSpace": "sRGB",
  68
+  "ExifImageWidth": 3008,
  69
+  "ExifImageHeight": 2000,
  70
+  "CustomRendered": "Normal",
  71
+  "ExposureMode": "Auto",
  72
+  "DigitalZoomRatio": 0,
  73
+  "FocalLengthIn35mmFormat": "112 mm",
  74
+  "SceneCaptureType": "Standard",
  75
+  "GainControl": "Low gain up",
  76
+  "Contrast": "Normal",
  77
+  "Saturation": "Normal",
  78
+  "Sharpness": "Normal",
  79
+  "PrintIMVersion": "0300",
  80
+  "Compression": "JPEG (old-style)",
  81
+  "ThumbnailOffset": 39274,
  82
+  "ThumbnailLength": 1820,
  83
+  "CurrentIPTCDigest": "dd8d51d28ddf04f08f870e5ff2f64d01",
  84
+  "Keywords": ["Orange","Rot"],
  85
+  "ApplicationRecordVersion": 4,
  86
+  "SupplementalCategories": "Natur",
  87
+  "XMPToolkit": "Image::ExifTool 7.03",
  88
+  "Title": "Abenddämmerung",
  89
+  "ImageWidth": 300,
  90
+  "ImageHeight": 199,
  91
+  "EncodingProcess": "Baseline DCT, Huffman coding",
  92
+  "BitsPerSample": 8,
  93
+  "ColorComponents": 3,
  94
+  "YCbCrSubSampling": "YCbCr4:2:0 (2 2)",
  95
+  "Aperture": 9.5,
  96
+  "ImageSize": "300x199",
  97
+  "LensID": "Minolta AF 28-135mm F4-4.5",
  98
+  "ScaleFactor35efl": 1.5,
  99
+  "ShutterSpeed": "1/60",
  100
+  "ThumbnailImage": "(Binary data 1820 bytes)",
  101
+  "CircleOfConfusion": "0.020 mm",
  102
+  "FOV": "18.3 deg",
  103
+  "FocalLength35efl": "75.0 mm (35 mm equivalent: 112.0 mm)",
  104
+  "HyperfocalDistance": "29.43 m",
  105
+  "LightValue": 10.4
  106
+}]
22  test/test_from_hash.rb
... ...
@@ -0,0 +1,22 @@
  1
+require 'helpers_for_test'
  2
+require 'json'
  3
+
  4
+class TestFromHash < TestCase
  5
+  def setup
  6
+    @data_dir = File.dirname(__FILE__) + '/data'
  7
+    hash_data = JSON.parse(File.read( @data_dir + '/test.jpg.json')).first
  8
+    @mini_exiftool = MiniExifTool.from_hash hash_data
  9
+  end
  10
+
  11
+  def test_conversion
  12
+    assert_kind_of String, @mini_exiftool.model
  13
+    assert_kind_of Time, @mini_exiftool['DateTimeOriginal']
  14
+    assert_kind_of Float, @mini_exiftool['MaxApertureValue']
  15
+    assert_kind_of String, @mini_exiftool.flash
  16
+    assert_kind_of Fixnum, @mini_exiftool['ExposureCompensation']
  17
+    assert_kind_of String, (@mini_exiftool['SubjectLocation'] || @mini_exiftool['SubjectArea'])
  18
+    assert_kind_of Array, @mini_exiftool['Keywords']
  19
+    assert_kind_of String, @mini_exiftool['SupplementalCategories']
  20
+    assert_kind_of Rational, @mini_exiftool.shutterspeed
  21
+  end
  22
+end

0 notes on commit d911d56

Please sign in to comment.
Something went wrong with that request. Please try again.