Class: Container::Shared
- Inherits:
-
Object
- Object
- Container::Shared
- Includes:
- Trackers
- Defined in:
- lib/torrents/container.rb
Instance Method Summary collapse
-
#default_values(method) ⇒ Object
Returns default value if any of the below methods (:details for example) return an exception.
-
#download(url) ⇒ Object
Downloads the URL, returns an empty string if an error occurred Here we try to convert the downloaded content to UTF8, if we“re at least 60% sure that the content that was downloaded actally is was we think The timeout is set to 10 seconds, after that time, an empty string will be returned url (String) The URL to download.
-
#error(messages, error = "") ⇒ Object
Prints a nice(er) error to the console if something went wrong This is only being called when trying to download or when trying to parse a page messages (String) The custom error to the user #error (Exception) The actual error that was thrown TODO: Implement a real logger => www.ruby-doc.org/stdlib/libdoc/logger/rdoc/classes/Logger.html.
-
#inner_call(method, option = nil) ⇒ Object
A middle caller that can handle errors for external trackers If the tracker that is being loaded in #load crashes, then this method makes sure that the entire application won“t crash method (Hash) The method that is being called inside the trackers module tr (Nokogiri | Symbol) The object that contains the HTML content of the current row.
-
#load ⇒ Object
Creating a singleton of the tracker class.
-
#url_cleaner(url) ⇒ Object
Cleans up the URL The ingoing param to the | RestClient method can handle the special characters below.
-
#valid_option?(method, option) ⇒ Boolean
Check to see if the ingoing arguments to the tracker if valid.
Instance Method Details
#default_values(method) ⇒ Object
Returns default value if any of the below methods (:details for example) return an exception. If the method for some reason isn’t implemented (is not in the hash below), then it will return an empty string method (Hash) The method that raised an exception
74 75 76 77 |
# File 'lib/torrents/container.rb', line 74 def default_values(method) # warn "Something went wrong, we can't find the #{method} tag, using default values" {torrent: "", torrents: [], seeders: 1, title: "", details: "", id: 0}[method] || "" end |
#download(url) ⇒ Object
Downloads the URL, returns an empty string if an error occurred Here we try to convert the downloaded content to UTF8, if we“re at least 60% sure that the content that was downloaded actally is was we think The timeout is set to 10 seconds, after that time, an empty string will be returned url (String) The URL to download
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/torrents/container.rb', line 21 def download(url) begin data = RestClient.get self.url_cleaner(url), {:timeout => 10, :cookies => @cookies} cd = CharDet.detect(data) return (cd["confidence"] > 0.6) ? (Iconv.conv(cd["encoding"] + "//IGNORE", "UTF-8", data) rescue data) : data rescue self.error("Something when wrong when trying to fetch #{url}", $!) end # The default value, if {RestClient} for some reason craches (like wrong encoding or a timeout) return "" end |
#error(messages, error = "") ⇒ Object
Prints a nice(er) error to the console if something went wrong This is only being called when trying to download or when trying to parse a page messages (String) The custom error to the user #error (Exception) The actual error that was thrown TODO: Implement a real logger => www.ruby-doc.org/stdlib/libdoc/logger/rdoc/classes/Logger.html
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/torrents/container.rb', line 39 def error(, error = "") = .class == Array ? : [] error = error.inspect[0..60] @errors = [] unless @errors @errors << "#{.join(", ")}\n#{error}" return unless @debug warn "An error in the Torrents gem occurred" warn "==> " + .join("\n\t") warn "==> " + error + " ..." warn "\n\n" end |
#inner_call(method, option = nil) ⇒ Object
A middle caller that can handle errors for external trackers If the tracker that is being loaded in #load crashes, then this method makes sure that the entire application won“t crash method (Hash) The method that is being called inside the trackers module tr (Nokogiri | Symbol) The object that contains the HTML content of the current row
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/torrents/container.rb', line 58 def inner_call(method, option = nil) begin results = option.nil? ? self.load.send(method) : self.load.send(method, option) if self.valid_option?(method, option) rescue self.error("An error occurred in the #{@tracker} class at the #{method} method.", $!) ensure raise NotImplementedError.new("#{option} is not implemented yet") if results.nil? and method == :category_url value = results.nil? ? self.default_values(method) : results end return value end |
#load ⇒ Object
Creating a singleton of the tracker class
80 81 82 |
# File 'lib/torrents/container.rb', line 80 def load @load ||= eval("#{Classify.new.camelize(@tracker)}.new") end |
#url_cleaner(url) ⇒ Object
Cleans up the URL The ingoing param to the | RestClient method can handle the special characters below. The only way to download the content that the URL points to is to escape those characters. Read more about it here => stackoverflow.com/questions/4999322/escape-and-download-url-using-ruby url (String) The url to escape Returns an escaped string
110 111 112 |
# File 'lib/torrents/container.rb', line 110 def url_cleaner(url) url.gsub(/\{|\}|\||\\|\^|\[|\]|\`|\s+/) { |m| CGI::escape(m) } end |
#valid_option?(method, option) ⇒ Boolean
Check to see if the ingoing arguments to the tracker if valid. If something goes wrong after the parser has been implemented, then it (the tracker) wont crash. Insted we write to a log file, so that the user can figure out the problem afterwards. method (Symbol) That method that is being called option (Object) That params to the method, can be anything, including nil Returns a boolean, true if the method can handle the option params, false otherwise.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/torrents/container.rb', line 90 def valid_option?(method, option) case method when :details, :title, :torrent option.instance_of?(Nokogiri::XML::Element) when :category_url option.instance_of?(Symbol) when :torrents, :seeders option.instance_of?(Nokogiri::HTML::Document) when :id option.instance_of?(String) else true end end |