Class: Ezid::BatchDownload
- Inherits:
-
Hashie::Dash
- Object
- Hashie::Dash
- Ezid::BatchDownload
- Includes:
- Hashie::Extensions::Coercion
- Defined in:
- lib/ezid/batch_download.rb
Constant Summary collapse
- ANVL =
"anvl".freeze
- CSV =
"csv".freeze
- XML =
"xml".freeze
- FORMATS =
[ ANVL, CSV, XML ].freeze
- YES =
"yes".freeze
- NO =
"no".freeze
- BOOLEANS =
[ YES, NO ].freeze
- TEST =
"test".freeze
- REAL =
"real".freeze
- PERMANENCE =
[ TEST, REAL ].freeze
- ARK =
"ark".freeze
- DOI =
"doi".freeze
- URN =
"urn".freeze
- TYPES =
[ ARK, DOI, URN, ].freeze
- ID =
CSV Columns
"_id".freeze
- MAPPED_CREATOR =
"_mappedCreator".freeze
- MAPPED_TITLE =
"_mappedTitle".freeze
- MAPPED_PUBLISHER =
"_mappedPublisher".freeze
- MAPPED_DATE =
"_mappedDate".freeze
- MAPPED_TYPE =
"_mappedType".freeze
- MAX_DOWNLOAD_TRIES =
300
- DOWNLOAD_RETRY_INTERVAL =
1
Instance Method Summary collapse
- #download_file(path: nil) ⇒ Object
- #download_url ⇒ Object
- #get_response ⇒ Object
-
#initialize(format, args = {}) ⇒ BatchDownload
constructor
A new instance of BatchDownload.
- #params ⇒ Object
- #reload ⇒ Object
Constructor Details
#initialize(format, args = {}) ⇒ BatchDownload
Returns a new instance of BatchDownload.
66 67 68 |
# File 'lib/ezid/batch_download.rb', line 66 def initialize(format, args={}) super(args.merge(format: format)) end |
Instance Method Details
#download_file(path: nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ezid/batch_download.rb', line 86 def download_file(path: nil) path ||= Dir.getwd fullpath = File.directory?(path) ? File.join(path, download_filename) : path tries = 0 begin tries += 1 download = Net::HTTP.get_response(download_uri) download.value rescue Net::HTTPServerException => e if download.is_a?(Net::HTTPNotFound) if tries < MAX_DOWNLOAD_TRIES print "Download file not yet available (attempt #{tries} of #{MAX_DOWNLOAD_TRIES})." puts " Trying again in #{DOWNLOAD_RETRY_INTERVAL} second(s) ..." sleep DOWNLOAD_RETRY_INTERVAL retry else raise BatchDownloadError, "Maximum download attempts (#{MAX_DOWNLOAD_TRIES}) reached unsuccessfully." end else raise end else File.open(fullpath, "wb") do |f| f.write(download.body) end puts "File successfully download to #{fullpath}." end end |
#download_url ⇒ Object
82 83 84 |
# File 'lib/ezid/batch_download.rb', line 82 def download_url get_response.download_url end |
#get_response ⇒ Object
74 75 76 |
# File 'lib/ezid/batch_download.rb', line 74 def get_response @response ||= client.batch_download(params) end |
#params ⇒ Object
70 71 72 |
# File 'lib/ezid/batch_download.rb', line 70 def params to_h end |
#reload ⇒ Object
78 79 80 |
# File 'lib/ezid/batch_download.rb', line 78 def reload @response = nil end |