Class: Miteru::Kit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Service

#call, call, #result, result

Constructor Details

#initialize(url, source:) ⇒ Kit

Returns a new instance of Kit.

Parameters:

  • url (String)
  • source (String)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/miteru/kit.rb', line 27

def initialize(url, source:)
  super()

  @url = url
  @source = source

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

Instance Attribute Details

#content_lengthInteger? (readonly)

Returns:

  • (Integer, nil)


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

def content_length
  @content_length
end

#headersHash? (readonly)

Returns:

  • (Hash, nil)


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

def headers
  @headers
end

#mime_typeString? (readonly)

Returns:

  • (String, nil)


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

def mime_type
  @mime_type
end

#sourceString (readonly)

Returns:

  • (String)


9
10
11
# File 'lib/miteru/kit.rb', line 9

def source
  @source
end

#statusInteger? (readonly)

Returns:

  • (Integer, nil)


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

def status
  @status
end

#urlString (readonly)

Returns:

  • (String)


6
7
8
# File 'lib/miteru/kit.rb', line 6

def url
  @url
end

Instance Method Details

#basenameObject



51
52
53
# File 'lib/miteru/kit.rb', line 51

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

#decoded_urlObject



88
89
90
# File 'lib/miteru/kit.rb', line 88

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

#downloaded?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/miteru/kit.rb', line 63

def downloaded?
  File.exist?(filepath_to_download)
end

#extnameObject



45
46
47
48
49
# File 'lib/miteru/kit.rb', line 45

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

  File.extname(url)
end

#filenameObject



55
56
57
# File 'lib/miteru/kit.rb', line 55

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

#filename_with_sizeObject



73
74
75
76
77
78
# File 'lib/miteru/kit.rb', line 73

def filename_with_size
  return filename unless filesize

  kb = (filesize.to_f / 1024.0).ceil
  "#{filename}(#{kb}KB)"
end

#filepath_to_downloadObject



59
60
61
# File 'lib/miteru/kit.rb', line 59

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

#filesizeObject



67
68
69
70
71
# File 'lib/miteru/kit.rb', line 67

def filesize
  return nil unless downloaded?

  File.size filepath_to_download
end

#hostnameObject



84
85
86
# File 'lib/miteru/kit.rb', line 84

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

#idObject



80
81
82
# File 'lib/miteru/kit.rb', line 80

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

#truncated_urlString

Returns:

  • (String)


95
96
97
# File 'lib/miteru/kit.rb', line 95

def truncated_url
  url.truncate(64)
end

#valid?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/miteru/kit.rb', line 39

def valid?
  # make a HEAD request for the validation
  before_validation
  valid_ext? && reachable? && valid_mime_type? && valid_content_length?
end