Class: Sfdown::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/sfdown.rb

Overview

HTTP GET/download with timeout, redirect-following and per-request sleep.

Constant Summary collapse

Error =
Class.new(StandardError)
MAX_REDIRECTS =
10

Instance Method Summary collapse

Constructor Details

#initialize(timeout: 5, sleep: 0) ⇒ Http

Returns a new instance of Http.



118
119
120
121
# File 'lib/sfdown.rb', line 118

def initialize(timeout: 5, sleep: 0)
  @timeout = timeout
  @sleep = sleep
end

Instance Method Details

#download(url, dest) ⇒ Object

Stream url to dest in chunks (never buffered whole); yields each chunk's bytesize.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sfdown.rb', line 129

def download(url, dest)
  with_sleep do
    with_response(url) do |res|
      File.open(dest, "wb") do |f|
        res.read_body do |chunk|
          f.write(chunk)
          yield chunk.bytesize if block_given?
        end
      end
    end
  end
end

#get(url) ⇒ Object

GET url (following redirects), returning the response body.



124
125
126
# File 'lib/sfdown.rb', line 124

def get(url)
  with_sleep { with_response(url) { |res| res.body } }
end