Skip to content

Commit

Permalink
Add media_info objects to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
cantino committed Mar 27, 2017
1 parent d3197d9 commit fe0ef0f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ Gemfile.lock

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
.idea
1 change: 1 addition & 0 deletions lib/dropbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require_relative 'dropbox/errors'
require_relative 'dropbox/account'
require_relative 'dropbox/metadata'
require_relative 'dropbox/media_info'
require_relative 'dropbox/upload_session_cursor'

# A small library for accessing the new Dropbox API. All of the Dropbox API
Expand Down
54 changes: 54 additions & 0 deletions lib/dropbox/media_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module Dropbox
class MediaInfo
attr_reader :dimensions, :time_taken, :location

def initialize(attrs={})
if dimensions = attrs.delete('dimensions')
@dimensions = Dimensions.new(dimensions)
end

if location = attrs.delete('location')
@location = Location.new(location)
end

if time_taken = attrs.delete('time_taken')
@time_taken = Time.parse(time_taken)
end
end
end

class PhotoMetadata < MediaInfo
def initialize(attrs={})

super(attrs)
end
end

class VideoMetadata < MediaInfo
attr_reader :duration

def initialize(attrs={})
@duration = attrs.delete('duration')

super(attrs)
end
end

class Location
attr_reader :latitude, :longitude

def initialize(attrs={})
@latitude = attrs.delete('latitude')
@longitude = attrs.delete('longitude')
end
end

class Dimensions
attr_reader :width, :height

def initialize(attrs={})
@width = attrs.delete('width')
@height = attrs.delete('height')
end
end
end
12 changes: 11 additions & 1 deletion lib/dropbox/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(attrs={})

# Contains the metadata (but not contents) of a file.
class FileMetadata < Metadata
attr_reader :id, :client_modified, :server_modified, :rev, :size
attr_reader :id, :client_modified, :server_modified, :rev, :size, :media_info

def initialize(attrs={})
@id = attrs.delete('id')
Expand All @@ -24,6 +24,16 @@ def initialize(attrs={})
@server_modified = Time.parse(attrs.delete('server_modified'))
@rev = attrs.delete('rev')
@size = attrs.delete('size')
if attrs.has_key?('media_info') && attrs['media_info']['.tag'] == "metadata"
@media_info = case attrs['media_info']['metadata']['.tag']
when 'photo'
PhotoMetadata.new(attrs['media_info']['metadata'])
when 'video'
VideoMetadata.new(attrs['media_info']['metadata'])
else
raise ClientError.unknown_response_type(attrs['media_info']['metadata']['.tag'])
end
end
super(attrs)
end

Expand Down
23 changes: 23 additions & 0 deletions test/test_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ def test_file_initialize
assert_equal 11, file.size
end

def test_file_initialize_with_media_info
file = Dropbox::FileMetadata.new('id' => 'id:123', 'name' => 'file',
'path_lower' => '/folder/file', 'path_display' => '/folder/file',
'size' => 11, 'server_modified' => '2007-07-07T00:00:00Z',
'media_info' => {
'.tag' => 'photo',
'metadata' => {
'.tag'=>'video',
'dimensions'=>{'height'=>720, 'width'=>960},
'location'=>{'latitude'=>40.9170, 'longitude'=>-74.10123},
'time_taken'=>'2017-01-29T19:58:19Z',
'duration'=>1234
}
})
assert_equal Dropbox::VideoInfo, file.media_info.class
assert_equal Time.parse('2017-01-29T19:58:19Z'), file.media_info.time_taken
assert_equal 1234, file.media_info.duration
assert_equal 40.9170, file.media_info.location.latitude
assert_equal -74.10123, file.media_info.location.longitude
assert_equal 720, file.media_info.dimensions.height
assert_equal 960, file.media_info.dimensions.width
end

def test_folder_equality
a = Dropbox::FolderMetadata.new('id' => 'id:123', 'name' => 'child',
'path_lower' => '/parent/middle/child', 'path_display' => '/parent/middle/child')
Expand Down

0 comments on commit fe0ef0f

Please sign in to comment.