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
# File 'lib/mediainfo/tracks.rb', line 97

def self.sanitize_element_value(param)
  name = param[0]
  value = param[1]

  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.sub(/^UTC\s+(.*)$/, '\1 UTC'))
  end

  value
end

.standardize_float_to_milliseconds(v) ⇒ Object



138
139
140
# File 'lib/mediainfo/tracks.rb', line 138

def self.standardize_float_to_milliseconds(v)
  (v*1000).to_i
end

.standardize_string_to_milliseconds(v, base_msec = 0) ⇒ Object



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

def self.standardize_string_to_milliseconds(v, base_msec = 0)
  v.scan(/\d+\s?\D+/).each do |chunk|
    base_msec += case chunk
    when /\d+\s?ms/         then chunk.to_i
    when /\d+\s?s(ec)?/     then chunk.to_i * 1000
    when /\d+\s?m(i?n)?/    then chunk.to_i * 60 * 1000
    when /\d+\s?h(our)?/    then chunk.to_i * 60 * 60 * 1000
    end.to_i
  end
  # We don't raise anymore. It's much better for the gem to
  # return the original MediaInfo attribute, than raise.
  base_msec == 0 ? v : base_msec
end

.standardize_to_milliseconds(value) ⇒ Object



118
119
120
121
122
# File 'lib/mediainfo/tracks.rb', line 118

def self.standardize_to_milliseconds(value)
  return standardize_float_to_milliseconds(value.to_f) if value =~ /^\d+\.?\d*$/
  return standardize_string_to_milliseconds(value)
  value
end