Class: DwcaHunter::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/dwca_hunter/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Resource

Returns a new instance of Resource.



15
16
17
18
19
20
# File 'lib/dwca_hunter/resource.rb', line 15

def initialize(opts)
  @needs_download = !(opts[:download] == false)
  @needs_unpack = !(opts[:unpack] == false)
  @download_dir, @download_file = File.split(@download_path)
  prepare_path if needs_download?
end

Instance Attribute Details

#abbrObject (readonly)

Returns the value of attribute abbr.



3
4
5
# File 'lib/dwca_hunter/resource.rb', line 3

def abbr
  @abbr
end

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/dwca_hunter/resource.rb', line 3

def command
  @command
end

#download_pathObject (readonly)

Returns the value of attribute download_path.



3
4
5
# File 'lib/dwca_hunter/resource.rb', line 3

def download_path
  @download_path
end

#titleObject (readonly)

Returns the value of attribute title.



3
4
5
# File 'lib/dwca_hunter/resource.rb', line 3

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/dwca_hunter/resource.rb', line 3

def url
  @url
end

#uuidObject (readonly)

Returns the value of attribute uuid.



3
4
5
# File 'lib/dwca_hunter/resource.rb', line 3

def uuid
  @uuid
end

Class Method Details

.gunzip(file, dir = nil) ⇒ Object



10
11
12
13
# File 'lib/dwca_hunter/resource.rb', line 10

def self.gunzip(file, dir = nil)
  Dir.chdir(dir) if dir
  `gunzip #{file}`
end

.unzip(file, dir = nil) ⇒ Object



5
6
7
8
# File 'lib/dwca_hunter/resource.rb', line 5

def self.unzip(file, dir = nil)
  Dir.chdir(dir) if dir
  `unzip -qq -u #{file} > /dev/null 2>&1`
end

Instance Method Details

#downloadObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dwca_hunter/resource.rb', line 30

def download
  DwcaHunter::logger_write(self.object_id,
                           "Starting download of '%s'" % @url)
  percentage = 0
  if url.match(/^\s*http:\/\//)
    dlr = DwcaHunter::Downloader.new(url, @download_path)
    downloaded_length = dlr.download_with_percentage do |r|
      if r[:percentage].to_i != percentage
        percentage = r[:percentage].to_i
        msg = "Downloaded %.0f%% in %.0f seconds ETA is %.0f seconds" %
                      [percentage, r[:elapsed_time], r[:eta]]
        DwcaHunter::logger_write(self.object_id, msg)
      end
    end
    DwcaHunter::logger_write(self.object_id,
                             "Download finished, Size: %s" %
                              downloaded_length)
  else
    `curl -s #{url} > #{download_path}`
  end
end

#needs_download?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/dwca_hunter/resource.rb', line 22

def needs_download?
  @needs_download
end

#needs_unpack?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/dwca_hunter/resource.rb', line 26

def needs_unpack?
  @needs_unpack
end