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.



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

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.



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

def download_url
  @download_url
end

#leechersObject (readonly)

Returns the value of attribute leechers.



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

def leechers
  @leechers
end

#seedersObject (readonly)

Returns the value of attribute seeders.



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

def seeders
  @seeders
end

Instance Method Details

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



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

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



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

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

#info_hashObject



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

def info_hash
  @info_hash ||= extract_hash
end

#magnetObject



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

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

#to_sObject



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

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