class DataGov::Resource
Attributes
dataset[R]
metadata[R]
Public Class Methods
new(metadata, dataset)
click to toggle source
# File lib/data_gov/resource.rb, line 10 def initialize(metadata, dataset) @metadata = metadata @dataset = dataset end
Public Instance Methods
download()
click to toggle source
# 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(metadata['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
extension()
click to toggle source
# File lib/data_gov/resource.rb, line 46 def extension mimetype.preferred_extension end
file_name()
click to toggle source
# File lib/data_gov/resource.rb, line 42 def file_name "#{metadata['id']}.#{extension}" end
mimetype()
click to toggle source
# File lib/data_gov/resource.rb, line 50 def mimetype MIME::Types[metadata['mimetype']].first || MIME::Types['text/plain'].first end