Class: Miyuki::Tracker

Inherits:
Object
  • Object
show all
Includes:
Yamazaki::Core
Defined in:
lib/miyuki/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watch_dir, track_file, series, &callback) ⇒ Tracker

Returns a new instance of Tracker.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/miyuki/tracker.rb', line 21

def initialize(watch_dir, track_file, series, &callback)
  Yamazaki.class.instance_eval do # breaking the rules is so funny sometimes w
    remove_const(:WATCH_DIR) if const_defined?(:WATCH_DIR)
    const_set(:WATCH_DIR, watch_dir)

    remove_const(:TRACK_FILE) if const_defined?(:TRACK_FILE)
    const_set(:TRACK_FILE, track_file)
  end

  Yamazaki.load_database

  @series   = series || []
  @callback = callback if block_given?
end

Instance Attribute Details

#torrentsObject (readonly)

Returns the value of attribute torrents.



19
20
21
# File 'lib/miyuki/tracker.rb', line 19

def torrents
  @torrents
end

Instance Method Details

#for_every_torrent(&callback) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/miyuki/tracker.rb', line 36

def for_every_torrent(&callback)
  if block_given?
    @callback = callback
  else
    yield
  end
end

#refresh!Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/miyuki/tracker.rb', line 44

def refresh!
  old_torrents = @torrents || []

  @torrents = []

  fetch_torrents!
  @torrents -= old_torrents

  @torrents.each do |torrent|
    downloaded = Yamazaki.download_torrent(torrent.title, torrent.link)

    @callback.call(torrent) if downloaded && @callback
  end
end