Class: KatSearch::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/kat_search/link.rb

Overview

Object that contains the info for a torrent file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Link

Returns a new instance of Link.



12
13
14
15
16
17
18
19
20
# File 'lib/kat_search/link.rb', line 12

def initialize(params)
  @params = params
  @seeders = params['seeders']
  @leechers = params['leechers']
  @download_url = params['download_url']

  # Get the longest filename for pretty print
  set_max_length_name(filename.length)
end

Instance Attribute Details

#download_urlObject (readonly)

Returns the value of attribute download_url.



10
11
12
# File 'lib/kat_search/link.rb', line 10

def download_url
  @download_url
end

#leechersObject (readonly)

Returns the value of attribute leechers.



10
11
12
# File 'lib/kat_search/link.rb', line 10

def leechers
  @leechers
end

#seedersObject (readonly)

Returns the value of attribute seeders.



10
11
12
# File 'lib/kat_search/link.rb', line 10

def seeders
  @seeders
end

Instance Method Details

#download(path = './') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kat_search/link.rb', line 38

def download(path = './')
  response = HTTParty.get(@download_url)

  raise 'Wrong content-type. Aborting.' unless response.headers['content-type'].include? 'application/x-bittorrent'

  # Get file name from the url
  filename = @download_url[/\?title=(.+)/, 1] + '.torrent'
  open(File.join(path, filename), 'w') { |f| f << response }

  # return filename
  filename
end

#filenameObject



26
27
28
# File 'lib/kat_search/link.rb', line 26

def filename
  @filename ||= "#{CGI.unescape(@params['name'])}.#{@params['extension']}"
end

#info_hashObject



34
35
36
# File 'lib/kat_search/link.rb', line 34

def info_hash
  @info_hash ||= extract_hash
end

#magnetObject



30
31
32
# File 'lib/kat_search/link.rb', line 30

def magnet
  @magnet ||= @params['magnet']
end

#to_sObject



22
23
24
# File 'lib/kat_search/link.rb', line 22

def to_s
  "#{filename.ljust(@@max_length_name + 2)} (#{@seeders.green}/#{@leechers.red})"
end