Class: NyaaScraper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(search) ⇒ NyaaScraper

Returns a new instance of NyaaScraper.



8
9
10
11
# File 'lib/nyaa_scraper.rb', line 8

def initialize(search)
  @search = search
  self.base = "https://nyaa.si"
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



6
7
8
# File 'lib/nyaa_scraper.rb', line 6

def base
  @base
end

Instance Method Details

#htmlObject



17
18
19
# File 'lib/nyaa_scraper.rb', line 17

def html
  uri.read
end


21
22
23
24
25
# File 'lib/nyaa_scraper.rb', line 21

def links
  Nokogiri::HTML(html)\
    .css(".torrent-list tr a") \
    .map { |link| link["href"] }
end

#magnetsObject



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

def magnets
  links.select { |href| href =~ /^magnet/ }
end

#torrentsObject



31
32
33
34
35
# File 'lib/nyaa_scraper.rb', line 31

def torrents
  links.select { |href| href =~ /\.torrent$/ } \
    .map { |href| URI.join self.base, "#{href}" } \
    .map { |uri| uri.to_s }
end

#uriObject



13
14
15
# File 'lib/nyaa_scraper.rb', line 13

def uri
  URI.join self.base, "?#{@search.querystring}"
end