Class: DataGov::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, dataset) ⇒ Resource

Returns a new instance of Resource.



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

def initialize(, dataset)
  @metadata = 
  @dataset = dataset
end

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset.



8
9
10
# File 'lib/data_gov/resource.rb', line 8

def dataset
  @dataset
end

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/data_gov/resource.rb', line 8

def 
  @metadata
end

Instance Method Details

#downloadObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/data_gov/resource.rb', line 15

def download
  if dataset.pairtree.exists?(file_name)
    puts "#{file_name} already exists, skipping download"
    return
  end
  pbar = ProgressBar.create(title: file_name, total: nil)
  begin
    download = open(['url'],
                    allow_redirections: :safe,
                    content_length_proc: lambda do |content_length|
                      if content_length && 0 < content_length
                        pbar.total = content_length
                      end
                    end,
                    progress_proc: lambda do |s|
                      if pbar.total
                        pbar.progress += s
                      else
                        pbar.increment
                      end
                    end)
    dataset.pairtree.open(file_name, 'w') { |io| IO.copy_stream(download, io) }
  rescue StandardError => e
    puts e
  end
end

#extensionObject



46
47
48
# File 'lib/data_gov/resource.rb', line 46

def extension
  mimetype.preferred_extension
end

#file_nameObject



42
43
44
# File 'lib/data_gov/resource.rb', line 42

def file_name
  "#{['id']}.#{extension}"
end

#mimetypeObject



50
51
52
# File 'lib/data_gov/resource.rb', line 50

def mimetype
  MIME::Types[['mimetype']].first || MIME::Types['text/plain'].first
end