Module: SK::Downloads

Extended by:
Downloads
Included in:
Downloads
Defined in:
lib/downloads.rb

Instance Method Summary collapse

Instance Method Details

#countObject



45
46
47
# File 'lib/downloads.rb', line 45

def count
  files.length
end

#delete_allObject



77
78
79
# File 'lib/downloads.rb', line 77

def delete_all
  FileUtils.rm_f(files)
end

#delete_any(name) ⇒ Object



72
73
74
75
# File 'lib/downloads.rb', line 72

def delete_any(name)
  list = files.select { |f|  File.basename(f).include?(name)  } 
  FileUtils.rm_f(list)  
end

#determine_mac_pathObject



15
16
17
18
19
# File 'lib/downloads.rb', line 15

def determine_mac_path
  parts = File.dirname(__FILE__).split("/")
  # will work for AR and PC on a Mac (for now)
  "/#{parts[1]}/#{parts[2]}/downloads"
end

#downloaded?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/downloads.rb', line 64

def downloaded?
  !downloading? && files.any?
end

#downloading?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/downloads.rb', line 68

def downloading?
  files.grep(/\.part$/).any?
end

#filesObject



21
22
23
24
25
26
# File 'lib/downloads.rb', line 21

def files
  # NOTE: every time this is called it goes out to the OS for
  # a fresh list of the files... we may want to change to a 
  # "refresh" or "readfiles" model for performance
  Dir["#{self.path}/*"]
end

#include?(filename) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/downloads.rb', line 32

def include?(filename)
  filepath = "#{path}/#{filename}"
  files.include? filepath
end

#lastObject



37
38
39
# File 'lib/downloads.rb', line 37

def last
  files.max_by {|f| File.mtime(f)}   
end

#last_nameObject



41
42
43
# File 'lib/downloads.rb', line 41

def last_name
  File.basename last # just the file name
end

#namesObject



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

def names    
  files.collect {|f| File.basename(f) }
end

#pathObject



11
12
13
# File 'lib/downloads.rb', line 11

def path
  @path = @path || determine_mac_path
end

#path=(p) ⇒ Object



7
8
9
# File 'lib/downloads.rb', line 7

def path=(p)
  @path = p
end

#wait_for_downloadObject

def first_content

wait_for_download
File.read(first)

end



58
59
60
61
62
# File 'lib/downloads.rb', line 58

def wait_for_download
  Timeout.timeout(1) do
    sleep 0.1 until downloaded?
  end
end