Module: Rager::Utils::Http

Extended by:
T::Sig
Defined in:
lib/rager/utils/http.rb

Class Method Summary collapse

Class Method Details

.download_binary(url, http_adapter: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rager/utils/http.rb', line 26

def self.download_binary(url, http_adapter: nil)
  adapter = http_adapter || Rager.config.http_adapter
  response = adapter.make_request(
    Rager::Http::Request.new(url: url, headers: {})
  )

  return nil unless response.success?
  body = response.body
  return nil if body.nil?

  if body.is_a?(String)
    body
  elsif body.respond_to?(:each)
    body.to_a.join
  else
    body.to_s
  end
end

.download_file(url, path, http_adapter: nil) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/rager/utils/http.rb', line 46

def self.download_file(url, path, http_adapter: nil)
  content = download_binary(url, http_adapter: http_adapter)
  return nil if content.nil?

  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, content)
  path
end

.sleep(duration) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/rager/utils/http.rb', line 17

def self.sleep(duration)
  if defined?(Async::Task) && (task = Async::Task.current?)
    task.sleep(duration)
  else
    Kernel.sleep(duration)
  end
end