class TTWatcher::Parsers::SimpleParser
Attributes
encoding[R]
@return [Encoding] (Encoding::UTF_8)
Parser encoding preferences.
page[R]
@return [String]
Current page.
Public Class Methods
new(site)
click to toggle source
Calls superclass method
TTWatcher::Parsers::Base::new
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 122 def initialize(site) # no-doc super self.encoding = settings[:encoding] ? settings[:encoding] : Encoding::UTF_8 end
Public Instance Methods
parse(page)
click to toggle source
@param [String] page
Url to initial page for parsing.
@return [TorrentList, nil]
When any exception raised it returns nil Otherwise it returns TorrentList instance.
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 14 def parse(page) return nil if page.is_a? NilClass self.page = page torrents = TorrentList.new loop do torrents << extract_torrents_from_page next_page? ? continue_with_next_page : break end return torrents rescue Exception => exception notificate_about_crash! exception return nil end
Private Instance Methods
continue_with_next_page()
click to toggle source
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 117 def continue_with_next_page # no-doc url = new_pages_list.pop self.page = assigned_site.download_page url end
encoding=(new_encoding)
click to toggle source
@param [Encoding, String] new_encoding
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 65 def encoding=(new_encoding) @encoding = if new_encoding.is_a?(Encoding) new_encoding else Encoding.find new_encoding end end
extract_torrent(unparsed_data)
click to toggle source
Extracts single torrent from unparsed_data
.
@param [Nokogiri::Node] unparsed_data
@return [Torrent]
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 54 def extract_torrent(unparsed_data) raise NotImplementedError, "Abstract method called!" end
extract_torrents_from_page()
click to toggle source
@return [TorrentList]
Returns extracted torrents from current +page+.
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 100 def extract_torrents_from_page list = TorrentList.new torrents_unparsed.each do |unparsed_torrent| torrent = extract_torrent unparsed_torrent list << torrent end return list end
new_pages_list()
click to toggle source
@return Array<String>
List of urls that should be scanned before complete torrents search.
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 35 def new_pages_list raise NotImplementedError, "Abstract method called!" end
next_page?()
click to toggle source
@return [TrueClass, FalseClass]
Returns +true+ if some pages wasn't scanned, otherwise returns +false+.
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 113 def next_page? new_pages_list.count > 0 end
notificate_about_crash!(exception)
click to toggle source
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 129 def notificate_about_crash!(exception) Logger.with_backtrace "Parser #{self.class} crashed with error:\n #{exception.inspect}" end
page=(other_page)
click to toggle source
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 79 def page=(other_page) @page = if other_page.encoding.name == encoding.name other_page else other_page.force_encoding encoding end end
structure()
click to toggle source
Structure for current page
.
@return [Nokogiri::HTML::Document]
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 93 def structure Nokogiri::HTML page, nil, encoding.to_s end
torrents_unparsed()
click to toggle source
@return Array<Nokogiri::Node>
Each element from this array represents all available information about 1 torrent.
# File sources/ttwatcher/sites/parsers/simple_parser.rb, line 43 def torrents_unparsed raise NotImplementedError, "Abstract method called!" end