Class: DataGov::Resource
- Inherits:
-
Object
- Object
- DataGov::Resource
- Defined in:
- lib/data_gov/resource.rb
Instance Attribute Summary collapse
-
#dataset ⇒ Object
readonly
Returns the value of attribute dataset.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
- #download ⇒ Object
- #extension ⇒ Object
- #file_name ⇒ Object
-
#initialize(metadata, dataset) ⇒ Resource
constructor
A new instance of Resource.
- #mimetype ⇒ Object
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
#dataset ⇒ Object (readonly)
Returns the value of attribute dataset.
8 9 10 |
# File 'lib/data_gov/resource.rb', line 8 def dataset @dataset end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
8 9 10 |
# File 'lib/data_gov/resource.rb', line 8 def @metadata end |
Instance Method Details
#download ⇒ Object
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 = 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 .total = content_length end end, progress_proc: lambda do |s| if .total .progress += s else .increment end end) dataset.pairtree.open(file_name, 'w') { |io| IO.copy_stream(download, io) } rescue StandardError => e puts e end end |
#extension ⇒ Object
46 47 48 |
# File 'lib/data_gov/resource.rb', line 46 def extension mimetype.preferred_extension end |
#file_name ⇒ Object
42 43 44 |
# File 'lib/data_gov/resource.rb', line 42 def file_name "#{['id']}.#{extension}" end |
#mimetype ⇒ Object
50 51 52 |
# File 'lib/data_gov/resource.rb', line 50 def mimetype MIME::Types[['mimetype']].first || MIME::Types['text/plain'].first end |