Class: MediaInfo::Tracks::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/mediainfo/tracks.rb

Defined Under Namespace

Classes: Extra

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Attributes

Returns a new instance of Attributes.



81
82
83
84
85
86
87
88
89
# File 'lib/mediainfo/tracks.rb', line 81

def initialize(params)
  params.each{ |param|
    if param[1].is_a?(Array)
      MediaInfo.set_singleton_method(self,param[0],Extra.new(param[1]))
    else
      MediaInfo.set_singleton_method(self,param[0],MediaInfo::Tracks::Attributes.sanitize_element_value(param))
    end
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Needed so that sanitize_elements doesn’t throw NoMethodError



77
78
79
# File 'lib/mediainfo/tracks.rb', line 77

def method_missing( name, *args )
  nil # We use nil here instead of false as nil should be understood by the client/requester as false. We might not want to specifically return false for other missing methods
end

Class Method Details

.sanitize_element_value(param) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/mediainfo/tracks.rb', line 97

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
  end
end

.standardize_to_milliseconds(value) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mediainfo/tracks.rb', line 121

def self.standardize_to_milliseconds(value)
  # TODO iphone video has a float as the duration
  # UPDATE THE README IF YOU'RE CHANGING THIS
  milliseconds = 0
  value.scan(/\d+\s?\w+/).each do |chunk|
    case chunk
    when /\d+\s?h/    then milliseconds += chunk.to_i * 60 * 60 * 1000
    when /\d+\s?hour/ then milliseconds += chunk.to_i * 60 * 60 * 1000
    when /\d+\s?m/    then milliseconds += chunk.to_i * 60 * 1000
    when /\d+\s?mn/   then milliseconds += chunk.to_i * 60 * 1000
    when /\d+\s?min/  then milliseconds += chunk.to_i * 60 * 1000
    when /\d+\s?s/    then milliseconds += chunk.to_i * 1000
    when /\d+\s?sec/  then milliseconds += chunk.to_i * 1000
    when /\d+\s?ms/   then milliseconds += chunk.to_i
    end
  end
  milliseconds = value if milliseconds == 0 # We don't raise anymore. It's much better for the gem to work, returning the original MediaInfo attribute, than raise.
  return milliseconds
end