Module: Mixin::Network
- Defined in:
- lib/fox/interface/thor/mixin/network.rb
Instance Method Summary collapse
-
#download(url, target_filename, progress_indicator = true) ⇒ OpenStruct
Returns an OpenStruct containing content and many other meta information.
- #initialize(*args) ⇒ Object
-
#online? ⇒ boolean
True if online, otherwise false.
Instance Method Details
#download(url, target_filename, progress_indicator = true) ⇒ OpenStruct
Returns an OpenStruct containing content and many other meta information
52 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 105 106 107 108 109 110 |
# File 'lib/fox/interface/thor/mixin/network.rb', line 52 def download url, target_filename, progress_indicator = true # Pre-condition raise ArgumentError, "The function expects a string, but got a (#{url.class.to_s})" unless( url.is_a?(String) ) # Main content = OpenStruct.new request = nil begin # wait 900s or 15min before timeout status = Timeout::timeout( 900 ) { if( progress_indicator ) # Inspired by http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI/OpenRead.html = nil request = open( url, "r", :read_timeout => nil, :content_length_proc => lambda {|t| if( t && 0 < t ) = ProgressBar.new("...", t) .file_transfer_mode end }, :progress_proc => lambda {|s| .set s if } ) else request = open( url, "r", :read_timeout => nil ) end STDOUT.puts "" } # end of Timeout::timeout rescue Timeout::Error puts 'Time out error - That took too long (>=15min) - fatal abort' exit end content.url = url content.content = request.read content.content_type = request.content_type content.charset = request.charset content.content_encoding = request.content_encoding content.last_modified = request.last_modified content.date = DateTime.now @logger. :debug, "Writing content to (file, #{target_filename.to_s})" File.open( target_filename, "w+" ) { |f| f.write( content.content ) } # Post-condition raise ArgumentError, "Result should be of type OpenStruct, but is (#{content.class.to_s})" unless( content.is_a?(OpenStruct) ) [ content, target_filename ] end |
#initialize(*args) ⇒ Object
28 29 30 |
# File 'lib/fox/interface/thor/mixin/network.rb', line 28 def initialize *args super end |
#online? ⇒ boolean
Returns True if online, otherwise false.
36 37 38 39 40 41 42 43 44 |
# File 'lib/fox/interface/thor/mixin/network.rb', line 36 def online? begin true if open( "http://www.google.com/", { read_timeout: 5 } ) rescue false end end |