Class: MyShows::TitleMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/myshows/title_matcher.rb

Constant Summary collapse

MATCHING_PROCS =

You could extend this list

[
  # simple
  proc {|src, title| src == title },

  # flash forward => FlashForward
  # lietome => Lie to Me
  proc {|src, title| src.gsub(' ', '') == title.gsub(' ', '') },

  # tbbt => The Big Bang Theory
  proc {|src, title| src == title.split.map{|w| w[0,1]} * '' },

  # avatar => Avatar: The Last Airbender
  proc {|src, title| title.split.include? src },

  # house m d => House
  proc {|src, title| src.split.include? title },
]

Instance Method Summary collapse

Constructor Details

#initialize(titles) ⇒ TitleMatcher

Returns a new instance of TitleMatcher.



22
23
24
25
# File 'lib/myshows/title_matcher.rb', line 22

def initialize(titles)
  @titles = titles.map {|s| strip(s)}
  @titles_wo_articles = @titles.map {|t| wo_articles(t) }
end

Instance Method Details

#match(src) ⇒ Object

Tries to find title closest to src, returns list of indexes of titles



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/myshows/title_matcher.rb', line 29

def match(src)
  matched = []

  src = strip(src)


  MATCHING_PROCS.each do |matcher|
    @titles.each_with_index do |title, i|
      matched << i if matcher.call(src, title)
    end
    return matched unless matched.empty?
  end

  src_wo_articles = wo_articles(src)
  MATCHING_PROCS.each do |matcher|
    @titles_wo_articles.each_with_index do |title_wo_articles, i|
      matched << i if matcher.call(src_wo_articles, title_wo_articles)
    end
    return matched unless matched.empty?
  end

  []
end