Commit 91c08389

mo khan <mo@mokhan.ca>
2014-06-01 20:44:09
parse and store exif information from photos.
1 parent b39f111
Changed files (3)
app
services
application
infrastructure
spec
services
infrastructure
app/services/application/handlers/process_photo.rb
@@ -1,5 +1,5 @@
 class ProcessPhoto
-  def initialize(photos = Photo, exif_parser)
+  def initialize(photos = Photo, exif_parser = ExifParser.new)
     @photos = photos
     @exif_parser = exif_parser
   end
@@ -18,11 +18,4 @@ class ProcessPhoto
     photo.latitude, photo.longitude = @exif_parser.parse_geolocation_from(file)
     photo.save!
   end
-
-  private
-
-  def parse_exif_from(file)
-    exif = EXIFR::JPEG.new(file)
-    [exif.gps.latitude, exif.gps.longitude]
-  end
 end
app/services/infrastructure/exif_parser.rb
@@ -0,0 +1,9 @@
+class ExifParser
+  def parse_geolocation_from(file)
+    exif = EXIFR::JPEG.new(file)
+    return [exif.gps.latitude, exif.gps.longitude] if exif.gps.present?
+    [0, 0]
+  rescue EXIFR::MalformedJPEG
+    [0, 0]
+  end
+end
spec/services/infrastructure/exif_parser_spec.rb
@@ -0,0 +1,25 @@
+require "spec_helper"
+
+describe ExifParser do
+  let(:jpg_with_gps) { File.join(Rails.root, 'spec/fixtures/images/gps.jpg') }
+  let(:jpg_no_gps) { File.join(Rails.root, 'spec/fixtures/images/gorilla.jpg') }
+  let(:png_file) { File.join(Rails.root, 'spec/fixtures/images/example.png') }
+
+  it "parses the latitude and longitude" do
+    latitude, longitude = subject.parse_geolocation_from(jpg_with_gps)
+    latitude.should == 51.07296369444445
+    longitude.should == -114.101799
+  end
+
+  it "ignores png files" do
+    latitude, longitude = subject.parse_geolocation_from(png_file)
+    latitude.should == 0
+    longitude.should == 0
+  end
+
+  it "ignores jpg files with no gps info" do
+    latitude, longitude = subject.parse_geolocation_from(jpg_no_gps)
+    latitude.should == 0
+    longitude.should == 0
+  end
+end