Module: RUtilAnts::URLAccess::URLAccessInterface

Included in:
Manager
Defined in:
lib/rUtilAnts/URLAccess.rb

Instance Method Summary collapse

Instance Method Details

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

Access the content of a URL. No cache. It calls a code block with the binary content of the URL (or a local file name if required).

Parameters
  • iURL (String): The URL (used to detect cyclic redirections)

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

    • :follow_redirections (Boolean): Do we follow redirections ? [optional = true]

    • :nbr_redirections_allowed (Integer): Number of redirections allowed [optional = 10]

    • :local_file_access (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]

    • :url_handler (Object): The URL handler, if it has already been instantiated, or nil otherwise [optional = nil]

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

    • iContent (String): File content, or file name if :local_file_access was true

    • iFileBaseName (String): The base name the file could have. Useful to get file name extensions.

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

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



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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rUtilAnts/URLAccess.rb', line 56

def access_file(iURL, iParameters = {})
  rError = nil

  lFollowRedirections = iParameters[:lFollowRedirections]
  lNbrRedirectionsAllowed = iParameters[:nbr_redirections_allowed]
  lLocalFileAccess = iParameters[:local_file_access]
  lURLHandler = iParameters[:url_handler]
  if (lFollowRedirections == nil)
    lFollowRedirections = true
  end
  if (lNbrRedirectionsAllowed == nil)
    lNbrRedirectionsAllowed = 10
  end
  if (lLocalFileAccess == nil)
    lLocalFileAccess = false
  end
  if (lURLHandler == nil)
    lURLHandler = get_url_handler(iURL)
  end
  # Get the content from the handler
  lContentFormat, lContent = lURLHandler.get_content(lFollowRedirections)
  case (lContentFormat)
  when CONTENT_ERROR
    rError = lContent
  when CONTENT_REDIRECT
    # Handle too much redirections (cycles)
    if (lContent.upcase == iURL.upcase)
      rError = RedirectionError.new("Redirecting to the same URL: #{iURL}")
    elsif (lNbrRedirectionsAllowed < 0)
      rError = RedirectionError.new("Too much URL redirections for URL: #{iURL} redirecting to #{lContent}")
    elsif (lFollowRedirections)
      # Follow the redirection if we want it
      lNewParameters = iParameters.clone
      lNewParameters[:nbr_redirections_allowed] = lNbrRedirectionsAllowed - 1
      # Reset the URL handler for the new parameters.
      lNewParameters[:url_handler] = nil
      rError = access_file(lContent, lNewParameters) do |iContent, iBaseName|
        yield(iContent, iBaseName)
      end
    else
      rError = RedirectionError.new("Received invalid redirection for URL: #{iURL}")
    end
  when CONTENT_STRING
    # The content is directly accessible.
    if (lLocalFileAccess)
      # Write the content in a local temporary file
      require 'tmpdir'
      lBaseName = lURLHandler.get_corresponding_file_base_name
      lLocalFileName = "#{Dir.tmpdir}/URLCache/#{lBaseName}"
      begin
        require 'fileutils'
        FileUtils::mkdir_p(File.dirname(lLocalFileName))
        File.open(lLocalFileName, 'wb') do |oFile|
          oFile.write(lContent)
        end
      rescue Exception
        rError = $!
        lContent = nil
      end
      if (rError == nil)
        yield(lLocalFileName, lBaseName)
        # Delete the temporary file
        File.unlink(lLocalFileName)
      end
    else
      # Give it to the code block directly
      yield(lContent, lURLHandler.get_corresponding_file_base_name)
    end
  when CONTENT_LOCALFILENAME, CONTENT_LOCALFILENAME_TEMPORARY
    lLocalFileName = lContent
    # The content is a local file name already accessible
    if (!lLocalFileAccess)
      # First, read the local file name
      begin
        File.open(lLocalFileName, 'rb') do |iFile|
          # Replace the file name with the real content
          lContent = iFile.read
        end
      rescue Exception
        rError = $!
      end
    end
    if (rError == nil)
      yield(lContent, lURLHandler.get_corresponding_file_base_name)
    end
    # If the file was temporary, delete it
    if (lContentFormat == CONTENT_LOCALFILENAME_TEMPORARY)
      File.unlink(lLocalFileName)
    end
  end

  return rError
end

#get_url_handler(iURL) ⇒ Object

Get the URL handler corresponding to this URL

Parameters
  • iURL (String): The URL

Return
  • Object: The URL handler



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rUtilAnts/URLAccess.rb', line 156

def get_url_handler(iURL)
  rURLHandler = nil

  # Try out every regexp unless it matches.
  # If none matches, assume a local file.
  @Plugins.each do |iPluginName, iPluginInfo|
    iRegexps, iPluginClassName = iPluginInfo
    iRegexps.each do |iRegexp|
      if (iRegexp.match(iURL) != nil)
        # Found a matching handler
        rURLHandler = eval("#{iPluginClassName}.new(iURL)")
        break
      end
    end
    if (rURLHandler != nil)
      break
    end
  end
  if (rURLHandler == nil)
    # Assume a local file
    rURLHandler = eval("#{@Plugins['LocalFile'][1]}.new(iURL)")
  end

  return rURLHandler
end

#init_url_accessObject

Constructor



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rUtilAnts/URLAccess.rb', line 19

def init_url_access
  # Get the map of plugins to read URLs
  # map< String, [ list<Regexp>, String ] >
  # map< PluginName, [ List of matching regexps, Plugin class name ] >
  @Plugins = {}
  Dir.glob(File.expand_path("#{File.dirname(__FILE__)}/URLHandlers/*.rb")).each do |iFileName|
    begin
      lPluginName = File.basename(iFileName)[0..-4]
      require "rUtilAnts/URLHandlers/#{lPluginName}"
      @Plugins[lPluginName] = [
        eval("RUtilAnts::URLAccess::URLHandlers::#{lPluginName}::get_matching_regexps"),
        "RUtilAnts::URLAccess::URLHandlers::#{lPluginName}"
      ]
    rescue Exception
      log_exc$!, "Error while requiring URLHandler plugin #{iFileName}"
    end
  end
end