Class: UTorrent::Torrent

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/u_torrent/torrent.rb

Constant Summary collapse

STATUSES =
{
  'Started'           => 1,
  'Checking'          => 2,
  'Start after check' => 4,
  'Checked'           => 8,
  'Error'             => 16,
  'Paused'            => 32,
  'Queued'            => 64,
  'Loaded'            => 128,
}
ACTIONS =
[
  :start, :stop, :force_start,
  :pause, :unpause, :recheck,
  :remove, :remove_data
]
ATTRIBUTES =
[
  :id, nil, :name, :size, nil, :downloaded, :uploaded, :ratio,
  :upload_speed, :download_speed, :eta, :label, :peers_connected,
  :peers_in_swarm, :seeds_connected, :seeds_in_swarm, :availability,
  :queue_order, :remaining, nil, nil, :status, nil, nil, nil, nil,
  :current_directory, nil, nil, nil
]

Instance Attribute Summary

Attributes included from Base

#raw_array

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

included, #initialize

Class Method Details

.add(url) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/u_torrent/torrent.rb', line 40

def self.add(url)
  UTorrent::Http.get_with_authentication(
    action: 'add-url',
    s:      url
  )
  all_torrents = UTorrent::Torrent.all
  max_queue_order = all_torrents.map(&:queue_order).max
  all_torrents.detect { |torrent| torrent.queue_order == max_queue_order }
end

.allObject



30
31
32
33
34
# File 'lib/u_torrent/torrent.rb', line 30

def self.all
  response = UTorrent::Http.get_with_authentication(list: 1)
  torrents_array = JSON.parse(response.body)['torrents']
  torrents_array.map { |torrent| self.new(torrent) }
end

.find(id) ⇒ Object



36
37
38
# File 'lib/u_torrent/torrent.rb', line 36

def self.find(id)
  all.detect { |torrent| torrent.id == id }
end

Instance Method Details

#filesObject



60
61
62
63
64
# File 'lib/u_torrent/torrent.rb', line 60

def files
  response = UTorrent::Http.get_with_authentication(action: 'getfiles', hash: id)
  files_array = JSON.parse(response.body)['files'].last
  files_array.each_with_index.map { |file, i| UTorrent::File.new(file, i, id) }
end

#percentageObject



56
57
58
# File 'lib/u_torrent/torrent.rb', line 56

def percentage
  @raw_array[4].to_f / 10
end

#refresh!Object



73
74
75
76
# File 'lib/u_torrent/torrent.rb', line 73

def refresh!
  torrent = self.class.find(id)
  @raw_array = torrent.raw_array
end

#statusesObject



50
51
52
53
54
# File 'lib/u_torrent/torrent.rb', line 50

def statuses
  status_code = @raw_array[1]
  status = STATUSES.select { |_, value| status_code & value == value }
  status.keys.reverse
end