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.



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

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 (String).



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/sfdown.rb', line 136

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 if block_given?
        end
      end
    end
  end
end

#get(url) ⇒ Object

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



131
132
133
# File 'lib/sfdown.rb', line 131

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