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, :url, 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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/u_torrent/torrent.rb', line 42

def self.add(url)
  UTorrent::Http.get_with_authentication(
    action: 'add-url',
    s:      url,
    label: 'foobar'
  )

  Timeout.timeout(5) do
    while(true) do
      all_torrents = UTorrent::Torrent.all
      matching_torrent = all_torrents.detect do |torrent|
        torrent.url == url
      end

      return matching_torrent unless matching_torrent.nil?
      sleep 0.1
    end
  end
end

.allObject



32
33
34
35
36
# File 'lib/u_torrent/torrent.rb', line 32

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



38
39
40
# File 'lib/u_torrent/torrent.rb', line 38

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

Instance Method Details

#filesObject



82
83
84
85
86
# File 'lib/u_torrent/torrent.rb', line 82

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

#label=(label) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/u_torrent/torrent.rb', line 72

def label=(label)
  UTorrent::Http.get_with_authentication(
    action: 'setprops',
    hash:   id,
    s:      'label',
    v:      label
  )
  refresh!
end

#percentageObject



68
69
70
# File 'lib/u_torrent/torrent.rb', line 68

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

#refresh!Object



95
96
97
98
# File 'lib/u_torrent/torrent.rb', line 95

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

#statusesObject



62
63
64
65
66
# File 'lib/u_torrent/torrent.rb', line 62

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