Skip to content

Commit

Permalink
Fix/sanitize element value (#35)
Browse files Browse the repository at this point in the history
* Fix MediaInfo::Tracks.sanitize_element_value Float/Duration issue.
  • Loading branch information
01max authored and NorseGaud committed Sep 12, 2018
1 parent 7df114c commit 1ce84df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
33 changes: 15 additions & 18 deletions lib/mediainfo/tracks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,22 @@ def initialize(params)
def self.sanitize_element_value(param)
name = param[0]
value = param[1]
case
# Convert String with integer in it to Integer.
## Don't to_f anything with 2 or more dots (versions,etc)
when value.match(/(?!\.)\D+/).nil? then
if value.scan(/\./).any? && value.scan(/\./).count > 1
value
elsif value.scan(/\./).any?
value.to_f
else # Prevent float if it's just a normal integer
value.to_i
end
# Duration
when ['Duration'].include?(name) then standardize_to_milliseconds(value)
# Dates
## Be sure the name has "date" and it has an integer and a dash, like a normal date would
when name.downcase.include?('date') && !value.match(/\d-/).nil? then Time.parse(value)
else
return value

if ['Duration'].include?(name)
# Duration
return standardize_to_milliseconds(value)
elsif value.to_s == value.to_i.to_s then value.to_i
# Convert String with integer in it to Integer.
return value.to_i
elsif value.to_s == value.to_f.to_s then value.to_f
# Convert String with integer in it to Integer.
return value.to_f
elsif name.downcase.include?('date') && !value.match(/\d-/).nil?
# Dates
return Time.parse(value)
end

value
end

def self.standardize_to_milliseconds(value)
Expand Down
9 changes: 4 additions & 5 deletions spec/mediainfo_tracks_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@
end

context 'when submitted a Float structured value' do
# TODO
# it 'converts it to milliseconds' do
# expect(MediaInfo.from(xml_files_content[:sample_iphone_mov]).video.duration).to be_a(Integer)
# expect(MediaInfo.from(xml_files_content[:sample_iphone_mov]).video.duration).to eq(194243)
# end
it 'converts it to milliseconds' do
expect(MediaInfo.from(xml_files_content[:sample_iphone_mov]).video.duration).to be_a(Integer)
expect(MediaInfo.from(xml_files_content[:sample_iphone_mov]).video.duration).to eq(194243)
end
end

end
Expand Down

0 comments on commit 1ce84df

Please sign in to comment.