Class: RUtilAnts::URLCache::URLCache

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

Overview

Class that caches every access to a URI (local file name, http, data…). This ensures just that several files are instantiated just once. For local files, it takes into account the file modification date/time to know if the Wx::Bitmap file has to be refreshed.

Defined Under Namespace

Classes: ServerDownError

Instance Method Summary collapse

Constructor Details

#initializeURLCache

Constructor



20
21
22
23
24
25
26
27
28
# File 'lib/rUtilAnts/URLCache.rb', line 20

def initialize
  # Map of known contents, interpreted in many flavors
  # map< Integer, [ Integer, Object ] >
  # map< URL's hash, [ CRC, Content ] >
  @URLs = {}
  # Map of hosts down (no need to try again such a host)
  # map< String >
  @HostsDown = {}
end

Instance Method Details

#getURLContent(iURL, iParameters = {}) ⇒ Object

Get a content from a URL. Here are the different formats the URL can have:

  • Local file name

  • http/https/ftp/ftps:// protocols

  • data:image URI

  • file:// protocol

It also handles redirections or zipped files

Parameters:

  • iURL (String): The URL

  • iParameters (map<Symbol,Object>): Additional parameters:

** :ForceLoad (Boolean): Do we force to refresh the cache ? [optional = false] ** :FollowRedirections (Boolean): Do we follow redirections ? [optional = true] ** :NbrRedirectionsAllowed (Integer): Number of redirections allowed [optional = 10] ** :LocalFileAccess (Boolean): Do we need a local file to read the content from ? If not, the content itslef will be given the code block. [optional = false]

  • CodeBlock: The code returning the object corresponding to the content:

** iContent (String): File content, or file name if :LocalFileAccess was true ** Returns: ** Object: Object read from the content, or nil in case of error ** Exception: The error encountered, or nil in case of success Return:

  • Object: The corresponding URL content, or nil in case of failure

  • Exception: The error, or nil in case of success



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rUtilAnts/URLCache.rb', line 53

def getURLContent(iURL, iParameters = {})
  rObject = nil
  rError = nil

  # Parse parameters
  lForceLoad = iParameters[:ForceLoad]
  if (lForceLoad == nil)
    lForceLoad = false
  end
  # Get the URL handler corresponding to this URL
  lURLHandler = getURLHandler(iURL)
  lServerID = lURLHandler.getServerID
  if (@HostsDown.has_key?(lServerID))
    rError = ServerDownError.new("Server #{iURL} is currently down.")
  else
    lURLHash = iURL.hash
    # Check if it is in the cache, or if we force refresh, or if the URL was invalidated
    lCurrentCRC = lURLHandler.getCRC
    if ((@URLs[lURLHash] == nil) or
        (lForceLoad) or
        (@URLs[lURLHash][0] != lCurrentCRC))
      # Load it for real
      # Reset previous value if it was set
      @URLs[lURLHash] = nil
      # Get the object
      lObject = nil
      lAccessError = accessFile(iURL, iParameters.merge(:URLHandler => lURLHandler)) do |iContent, iBaseName|
        lObject, rError = yield(iContent)
      end
      if (lAccessError != nil)
        rError = lAccessError
      end
      # Put lObject in the cache if no error was found
      if (rError == nil)
        # OK, register it
        @URLs[lURLHash] = [ lCurrentCRC, lObject ]
      else
        if ((defined?(SocketError) != nil) and
            (rError.is_a?(SocketError)))
          # We have a server down
          @HostsDown[lServerID] = nil
        end
      end
    end
    # If no error was found (errors can only happen if it was not already in the cache), take it from the cache
    if (rError == nil)
      rObject = @URLs[lURLHash][1]
    end
  end

  return rObject, rError
end