Class: Nyaa::Search
- Inherits:
-
Object
- Object
- Nyaa::Search
- Defined in:
- lib/nyaa/search.rb
Instance Attribute Summary collapse
-
#cachedir ⇒ Object
Returns the value of attribute cachedir.
-
#category ⇒ Object
Returns the value of attribute category.
-
#count ⇒ Object
Returns the value of attribute count.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#query ⇒ Object
Returns the value of attribute query.
-
#results ⇒ Object
Returns the value of attribute results.
-
#runid ⇒ Object
Returns the value of attribute runid.
Instance Method Summary collapse
-
#cached(page) ⇒ Object
TODO: Deprecated function.
-
#get_results ⇒ Object
Returns current batch (page) results.
-
#initialize(query, cat = nil, fil = nil) ⇒ Search
constructor
A new instance of Search.
- #more ⇒ Object
- #purge ⇒ Object
Constructor Details
#initialize(query, cat = nil, fil = nil) ⇒ Search
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/nyaa/search.rb', line 8 def initialize(query, cat = nil, fil = nil) self.query = URI.escape(query) self.category = cat ? CATS[cat.to_sym][:id] : '0_0' self.filter = fil ? FILS[fil.to_sym][:id] : '0' self.offset = 0 self.results = [] self.count = 1.0/0.0 self.runid = Time.new.to_i self.cachedir = File.('~/.nyaa/cache') FileUtils.mkdir_p(cachedir) unless File.directory?(cachedir) end |
Instance Attribute Details
#cachedir ⇒ Object
Returns the value of attribute cachedir.
6 7 8 |
# File 'lib/nyaa/search.rb', line 6 def cachedir @cachedir end |
#category ⇒ Object
Returns the value of attribute category.
4 5 6 |
# File 'lib/nyaa/search.rb', line 4 def category @category end |
#count ⇒ Object
Returns the value of attribute count.
5 6 7 |
# File 'lib/nyaa/search.rb', line 5 def count @count end |
#filter ⇒ Object
Returns the value of attribute filter.
4 5 6 |
# File 'lib/nyaa/search.rb', line 4 def filter @filter end |
#offset ⇒ Object
Returns the value of attribute offset.
5 6 7 |
# File 'lib/nyaa/search.rb', line 5 def offset @offset end |
#query ⇒ Object
Returns the value of attribute query.
4 5 6 |
# File 'lib/nyaa/search.rb', line 4 def query @query end |
#results ⇒ Object
Returns the value of attribute results.
5 6 7 |
# File 'lib/nyaa/search.rb', line 5 def results @results end |
#runid ⇒ Object
Returns the value of attribute runid.
6 7 8 |
# File 'lib/nyaa/search.rb', line 6 def runid @runid end |
Instance Method Details
#cached(page) ⇒ Object
TODO: Deprecated function
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/nyaa/search.rb', line 42 def cached(page) cachefile = "#{self.cachedir}/cache_#{self.runid}_p#{page}" p cachefile return nil unless File.exists?(cachefile) File.open(cachefile, 'rb') do |file| begin results = Marshal.load(file) rescue => e puts "ERROR: Failed to load #{cachefile}" puts "#{e.backtrace}: #{e.message} (#{e.class})" end end results end |
#get_results ⇒ Object
Returns current batch (page) results
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/nyaa/search.rb', line 22 def get_results if self.offset.zero? batch = [] elsif self.offset == 1 batch = self.results[0, 100] else # self.offset > 1 batch = self.results[(self.offset - 1) * 100, 100] end batch end |
#more ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/nyaa/search.rb', line 33 def more self.offset += 1 if self.results.length < self.count extract(self.offset) end self end |
#purge ⇒ Object
58 59 60 |
# File 'lib/nyaa/search.rb', line 58 def purge FileUtils.rm_rf Dir.glob("#{self.cachedir}/*") end |