module Obtain

Constants

VERSION

Public Class Methods

create_dir(dir) click to toggle source
# File lib/obtainer.rb, line 54
def self.create_dir(dir)
  if !Dir.exists?(dir)
    FileUtils.mkdir_p(dir)
  end
end
establish_connection() click to toggle source
# File lib/obtainer.rb, line 34
def self.establish_connection
  connection = Net::HTTP.new(@uri.host, @uri.port)
  connection.https!(@uri)
end
fetch(url, path) click to toggle source
# File lib/obtainer.rb, line 15
def self.fetch(url, path)

  @uri = URI::parse(url)
  connection = self.establish_connection

  self.create_dir(File.dirname(path))

  File.open(path, 'wb') { |file|
    connection.request_get(@uri.path) { |res| 
      res.read_body { |seg|
        file << seg
        sleep 0.005
      }
    }
  }

  File.realpath(path)
end
generate_location(dir) click to toggle source
# File lib/obtainer.rb, line 39
def self.generate_location(dir)

  location = self.random_path(dir)

  while File.file?(location) do 
    location = self.random_path(dir)
  end

  location
end
random_path(dir) click to toggle source
# File lib/obtainer.rb, line 50
def self.random_path(dir)
  File.join(dir, SecureRandom.hex)
end
url_to_dir(url, dir) click to toggle source
# File lib/obtainer.rb, line 10
def self.url_to_dir(url, dir)
  path = self.generate_location(dir)
  self.fetch(url, path)
end