From b05ffba1cd2bbaf997435393829ecebeaa357703 Mon Sep 17 00:00:00 2001 From: Anton Sivakov Date: Tue, 12 Jul 2016 22:19:49 +0300 Subject: [PATCH] Added example for stream mode --- examples/README.md | 8 +++++++- examples/stream_download.rb | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 examples/stream_download.rb diff --git a/examples/README.md b/examples/README.md index 39df190..a11f66f 100644 --- a/examples/README.md +++ b/examples/README.md @@ -64,4 +64,10 @@ * Two ways to pass params to get, inline on the url or in query hash * [Rescue Json Error](rescue_json.rb) - * Rescue errors due to parsing response \ No newline at end of file + * Rescue errors due to parsing response + +* [Download file using stream mode](stream_download.rb) + * Httparty included into poro class + * Uses `get` requests + * Uses `stream_body` mode + * Download file without using the memory \ No newline at end of file diff --git a/examples/stream_download.rb b/examples/stream_download.rb new file mode 100644 index 0000000..5e64256 --- /dev/null +++ b/examples/stream_download.rb @@ -0,0 +1,24 @@ +dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) +require File.join(dir, 'httparty') +require 'pp' + +class StreamDownload + include HTTParty + + base_uri 'https://cdn.kernel.org/pub/linux/kernel/v4.x' +end + +# download file linux-4.6.4.tar.xz without using the memory +response = nil +filename = 'linux-4.6.4.tar.xz' + +File.open(filename, 'w') do |file| + response = StreamDownload.get('/linux-4.6.4.tar.xz', stream_body: true) do |fragment| + file.binmode + file.write(fragment) + end +end + +pp "Success: #{response.success?}" + +pp File.stat(filename).inspect