mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Added example for stream mode
This commit is contained in:
parent
0d808c7f3a
commit
b05ffba1cd
2 changed files with 31 additions and 1 deletions
|
@ -64,4 +64,10 @@
|
||||||
* Two ways to pass params to get, inline on the url or in query hash
|
* Two ways to pass params to get, inline on the url or in query hash
|
||||||
|
|
||||||
* [Rescue Json Error](rescue_json.rb)
|
* [Rescue Json Error](rescue_json.rb)
|
||||||
* Rescue errors due to parsing response
|
* 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
|
24
examples/stream_download.rb
Normal file
24
examples/stream_download.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue