Created
June 14, 2016 16:58
-
-
Save Madh93/d5b4d85f30454e8626a05d0917e341bc to your computer and use it in GitHub Desktop.
Http Test: Download a binary file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# NAME : Http Test | |
# DESCRIPTION : Download a binary file over HTTP | |
# AUTHOR : Madh93 (Miguel Hernandez) | |
# VERSION : 0.0.1 | |
# LICENSE : GNU General Public License v3 | |
# USAGE : ruby http_test.rb | |
require 'open-uri' | |
require 'net/http' | |
require 'rest-client' | |
URL = 'http://download.openbricks.org/sample/H264/big_buck_bunny_480p_H264_AAC_25fps_1800K.MP4' | |
# 1: OpenURI library | |
def http_test_1 | |
File.open('http_test_3.mp4', 'wb') do |f| | |
f.write open(URL).read | |
end | |
end | |
# 2: Net/HTTP library | |
def http_test_2 | |
File.open('http_test_2.mp4', 'wb') do |f| | |
f.write Net::HTTP.get(URI.parse(URL)) | |
end | |
end | |
# 3: Rest Client library | |
def http_test_3 | |
File.open('http_test_3.mp4', 'wb') do |f| | |
f.write RestClient.get(URL) | |
end | |
end | |
# 4: Net/HTTP through http segments | |
def http_test_4 | |
Net::HTTP.start('download.openbricks.org') do |http| | |
f = File.open('http_test_4.mp4', 'wb') | |
begin | |
http.request_get('/sample/H2645/big_buck_bunny_480p_H264_AAC_25fps_1800K.MP4') do |resp| | |
resp.read_body do |segment| | |
f.write segment | |
end | |
end | |
ensure | |
f.close | |
end | |
end | |
end | |
http_test_1 | |
http_test_2 | |
http_test_3 | |
http_test_4 | |
# CONCLUSION | |
# 1,2,3 -> good: easy and short, bad: reads file into **memory** before writing it to disk | |
# 4 -> good: read http segments, bad: ugly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment