Class: Miteru::Kit

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

Constant Summary collapse

VALID_EXTENSIONS =
Miteru.configuration.valid_extensions
VALID_MIME_TYPES =
Miteru.configuration.valid_mime_types

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, source) ⇒ Kit

Returns a new instance of Kit.



30
31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#content_lengthInteger? (readonly)

Returns:

  • (Integer, nil)


22
23
24
# File 'lib/miteru/kit.rb', line 22

def content_length
  @content_length
end

#headersHash? (readonly)

Returns:

  • (Hash, nil)


28
29
30
# File 'lib/miteru/kit.rb', line 28

def headers
  @headers
end

#mime_typeString? (readonly)

Returns:

  • (String, nil)


25
26
27
# File 'lib/miteru/kit.rb', line 25

def mime_type
  @mime_type
end

#sourceString (readonly)

Returns:

  • (String)


16
17
18
# File 'lib/miteru/kit.rb', line 16

def source
  @source
end

#statusInteger? (readonly)

Returns:

  • (Integer, nil)


19
20
21
# File 'lib/miteru/kit.rb', line 19

def status
  @status
end

#urlString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/miteru/kit.rb', line 13

def url
  @url
end

Instance Method Details

#basenameObject



53
54
55
# File 'lib/miteru/kit.rb', line 53

def basename
  @basename ||= File.basename(url)
end

#decoded_urlObject



90
91
92
# File 'lib/miteru/kit.rb', line 90

def decoded_url
  @decoded_url ||= URI.decode_www_form_component(url)
end

#downloaded?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/miteru/kit.rb', line 65

def downloaded?
  File.exist?(filepath_to_download)
end

#extnameObject



47
48
49
50
51
# File 'lib/miteru/kit.rb', line 47

def extname
  return ".tar.gz" if url.end_with?("tar.gz")

  File.extname(url)
end

#filenameObject



57
58
59
# File 'lib/miteru/kit.rb', line 57

def filename
  @filename ||= CGI.unescape(basename)
end

#filename_with_sizeObject



75
76
77
78
79
80
# 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_downloadObject



61
62
63
# File 'lib/miteru/kit.rb', line 61

def filepath_to_download
  "#{base_dir}/#{filename_to_download}"
end

#filesizeObject



69
70
71
72
73
# File 'lib/miteru/kit.rb', line 69

def filesize
  return nil unless downloaded?

  File.size filepath_to_download
end

#hostnameObject



86
87
88
# File 'lib/miteru/kit.rb', line 86

def hostname
  @hostname ||= URI(url).hostname
end

#idObject



82
83
84
# File 'lib/miteru/kit.rb', line 82

def id
  @id ||= UUIDTools::UUID.random_create.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# 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