Class: SubtitleSourceModule::Subtitles

Inherits:
Array
  • Object
show all
Defined in:
lib/subtitle_source/array.rb

Instance Method Summary collapse

Instance Method Details

#based_on(string, args = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/subtitle_source/array.rb', line 5

def based_on(string, args = {})    
  result = self.any? ? self.map do |s|
    [Levenshtein.distance(s.release_name, string, args[:limit] || 0.4), s]
  end.reject do |value|
    value.first.nil?
  end.sort_by do |value|
    value.first
  end.map(&:last).first : nil

  return if result.nil?

  # If the string contains a release date, then the results should do it to
  if result.release_name =~ /(s\d{2}e\d{2})/i
    return unless string.match(/#{$1}/i)
  end

  # If the {string} contains a year, then the found movie should return one to
  if result.release_name =~ /((19|20)\d{2})/
    return unless string.match(/#{$1}/)
  end

  # Yes i know, return isn't needed
  return result
end