class Miteru::Kit

Constants

VALID_EXTENSIONS
VALID_MIME_TYPES

Attributes

content_length[R]

@return [Integer, nil]

headers[R]

@return [Hash, nil]

mime_type[R]

@return [String, nil]

source[R]

@return [String]

status[R]

@return [Integer, nil]

url[R]

@return [String]

Public Class Methods

new(url, source) click to toggle source
# File lib/miteru/kit.rb, line 30
def initialize(url, source)
  @url = url
  @source = source

  @content_length = nil
  @mime_type = nil
  @status = nil
  @headers = nil
end

Public Instance Methods

basename() click to toggle source
# File lib/miteru/kit.rb, line 53
def basename
  @basename ||= File.basename(url)
end
decoded_url() click to toggle source
# File lib/miteru/kit.rb, line 90
def decoded_url
  @decoded_url ||= URI.decode_www_form_component(url)
end
downloaded?() click to toggle source
# File lib/miteru/kit.rb, line 65
def downloaded?
  File.exist?(filepath_to_download)
end
extname() click to toggle source
# File lib/miteru/kit.rb, line 47
def extname
  return ".tar.gz" if url.end_with?("tar.gz")

  File.extname(url)
end
filename() click to toggle source
# File lib/miteru/kit.rb, line 57
def filename
  @filename ||= CGI.unescape(basename)
end
filename_with_size() click to toggle source
# File lib/miteru/kit.rb, line 75
def filename_with_size
  return filename unless filesize

  kb = (filesize.to_f / 1024.0).ceil
  "#{filename}(#{kb}KB)"
end
filepath_to_download() click to toggle source
# File lib/miteru/kit.rb, line 61
def filepath_to_download
  "#{base_dir}/#{filename_to_download}"
end
filesize() click to toggle source
# File lib/miteru/kit.rb, line 69
def filesize
  return nil unless downloaded?

  File.size filepath_to_download
end
hostname() click to toggle source
# File lib/miteru/kit.rb, line 86
def hostname
  @hostname ||= URI(url).hostname
end
id() click to toggle source
# File lib/miteru/kit.rb, line 82
def id
  @id ||= UUIDTools::UUID.random_create.to_s
end
valid?() click to toggle source
# File lib/miteru/kit.rb, line 40
def valid?
  # make a HEAD request for the validation
  before_validation

  valid_ext? && reachable? && valid_mime_type? && valid_content_length?
end

Private Instance Methods

base_dir() click to toggle source
# File lib/miteru/kit.rb, line 100
def base_dir
  @base_dir ||= Miteru.configuration.download_to
end
before_validation() click to toggle source
# File lib/miteru/kit.rb, line 108
def before_validation
  res = HTTPClient.head(url)
  @content_length = res.content_length
  @mime_type = res.content_type.mime_type.to_s
  @status = res.status
  @headers = res.headers.to_h
rescue StandardError
  # do nothing
end
filename_to_download() click to toggle source
# File lib/miteru/kit.rb, line 96
def filename_to_download
  "#{id}#{extname}"
end
reachable?() click to toggle source
# File lib/miteru/kit.rb, line 118
def reachable?
  status&.success?
end
valid_content_length?() click to toggle source
# File lib/miteru/kit.rb, line 126
def valid_content_length?
  content_length.to_i > 0
end
valid_ext?() click to toggle source
# File lib/miteru/kit.rb, line 104
def valid_ext?
  VALID_EXTENSIONS.include? extname
end
valid_mime_type?() click to toggle source
# File lib/miteru/kit.rb, line 122
def valid_mime_type?
  VALID_MIME_TYPES.include? mime_type
end