Sha256: 0f7647f622fb471929daf87f540b0b7f638d11e24733d771dc323d23176e641c

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'uri'
require 'net/http'
require 'open-uri'

module Sbuilder
  
  module Utils
    
    class NetIo

      class << self
        
        def uri_exist?( uri )
          if uri =~ URI::regexp
            url = URI.parse( uri )
            Net::HTTP.start(url.host, url.port) { |http|
              return http.head(url.request_uri).code == "200"
            }
          else
            return File.exist?( uri )
          end
        end

        # if  'cacheFile' return line in 'cacheFile else read from 'uri' &&  write to 'cacheFile'
        def read_lines_cached( uri, cacheFile = nil  )
          
          # cached
          return File.read( cacheFile ) if !cacheFile.nil? && File.exist?( cacheFile )

          # network access && create cache
          lines = read_lines( uri )
          cache_lines( cacheFile, lines ) unless cacheFile.nil?

          # 
          return lines
          
        end

        def read_lines( uri  )
          begin
            open( uri ).read
          rescue => e
            msg = "Error #{e}, when reading from uri '#{uri}'"
            raise msg
          end
        end

        # write lines in cache file
        def cache_lines( cacheFile, lines  )
          File.open( cacheFile, "w" ) { |f| f.write( lines ) }
        end


      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tla-sbuilder-0.1.0 lib/utils/netio.rb