Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net::HTTPResponse body as IO

Tags:

http

io

ruby

Net::HTTPResponse's body is a stream-like object, and you get can read its input in lazy chucks using read_body. In the rest of ruby, steams are represented as class IO. Is there a wrapper or something that lets me use a Net::HTTPResponse as if it was an IO object?

like image 852
jes5199 Avatar asked Oct 27 '25 08:10

jes5199


1 Answers

Use the OpenURI library that comes with standard Ruby. It uses Net::Http under the hood, and provides a convenient File-like object.

require 'open-uri'
open('http://example.com/some_file') do |f|
  f.each_line do |line|
    puts "http line: #{line}"
  end
do
like image 189
Kevin Avatar answered Oct 28 '25 22:10

Kevin