Commit 91c9b717

mo khan <mo@mokhan.ca>
2014-06-07 04:20:36
move exif parsing to photo class.
1 parent ea37203
Changed files (5)
app
models
services
application
infrastructure
spec
models
services
application
app/models/photo.rb
@@ -15,6 +15,8 @@ class Photo < ActiveRecord::Base
     image = Image.new(file)
     self.image = self.original_filename = image.filename
     self.content_type = image.content_type
+    self.latitude, self.longitude = image.geolocation
+    self.image_processing = nil
     versions.each do |version|
       version.adjust(image)
       blob_storage.upload(create_key(version.prefix), image.path)
app/services/application/handlers/process_photo.rb
@@ -1,7 +1,6 @@
 class ProcessPhoto
-  def initialize(photos, exif_parser, blob_storage = BlobStorage.new)
+  def initialize(photos, blob_storage = BlobStorage.new)
     @photos = photos
-    @exif_parser = exif_parser
     @blob_storage = blob_storage
   end
 
@@ -11,8 +10,6 @@ class ProcessPhoto
 
   def handle(message)
     photo = @photos.find(message[:photo_id])
-    photo.image_processing = nil
-    photo.latitude, photo.longitude = @exif_parser.parse_geolocation_from(message[:file_path])
     photo.upload(message[:file_path], @blob_storage)
     photo.save!
   end
app/services/infrastructure/image.rb
@@ -1,8 +1,9 @@
 class Image
   attr_reader :path
 
-  def initialize(path)
+  def initialize(path, exif = ExifParser.new)
     @path = path
+    @exif = exif
   end
 
   def filename
@@ -13,6 +14,10 @@ class Image
     @content_type ||= ::MIME::Types.type_for(filename).first.to_s
   end
 
+  def geolocation
+    @exif.parse_geolocation_from(@path)
+  end
+
   def resize_to_fit(width, height)
     manipulate! do |img|
       img.resize "#{width}x#{height}"
spec/models/photo_spec.rb
@@ -26,6 +26,12 @@ describe Photo do
       subject.content_type.should == "image/jpeg"
     end
 
+    it "applies the gps coordinates" do
+      subject.upload(file, blob_storage)
+      expect(subject.latitude).to eql(51.07296369444445)
+      expect(subject.longitude).to eql(-114.101799)
+    end
+
     def upload_key(prefix = '')
       "uploads/photo/image/#{subject.id}/#{prefix}gps.jpg"
     end
spec/services/application/handlers/process_photo_spec.rb
@@ -2,8 +2,8 @@ require "spec_helper"
 
 describe ProcessPhoto do
   let(:photos) { double }
-  let(:exif_parser) { double }
-  subject { ProcessPhoto.new(photos, exif_parser) }
+  let(:blob_storage) { double(upload: true) }
+  subject { ProcessPhoto.new(photos, blob_storage) }
 
   describe "#handles?" do
     it "handles photo uploads" do
@@ -19,7 +19,6 @@ describe ProcessPhoto do
 
     before :each do
       photos.stub(:find).with(photo.id).and_return(photo)
-      exif_parser.stub(:parse_geolocation_from).and_return([latitude, longitude])
       message = {
         photo_id: photo.id,
         file_path: image_path,
@@ -50,4 +49,3 @@ describe ProcessPhoto do
     end
   end
 end
-