Class: Ezid::BatchDownload

Inherits:
Hashie::Dash
  • Object
show all
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

Constructor Details

#initialize(format, args = {}) ⇒ BatchDownload

Returns a new instance of BatchDownload.



67
68
69
# File 'lib/ezid/batch_download.rb', line 67

def initialize(format, args={})
  super(args.merge(format: format))
end

Instance Method Details

#download_file(path: nil) ⇒ Object Also known as: file



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
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ezid/batch_download.rb', line 89

def download_file(path: nil)
  path ||= Dir.getwd
  fullpath = File.directory?(path) ? File.join(path, download_filename) : path
  tries = 0
  ready = false

  print "Checking for download "
  Net::HTTP.start(download_uri.host, download_uri.port) do |http|
    while tries < MAX_DOWNLOAD_TRIES
      tries += 1
      sleep DOWNLOAD_RETRY_INTERVAL
      print "."
      response = http.head(download_uri.path)
      if response.code == '200'
        ready = true
        break
      end
    end
  end
  puts

  unless ready
    raise BatchDownloadError,
          "Download not ready after checking #{MAX_DOWNLOAD_TRIES} times."
  end

  File.open(fullpath, "wb") do |f|
    Net::HTTP.start(download_uri.host, download_uri.port) do |http|
      http.request_get(download_uri.path) do |response|
        response.read_body do |chunk|
          f.write(chunk)
        end
      end
    end
  end

  fullpath
end

#download_urlObject Also known as: url



83
84
85
# File 'lib/ezid/batch_download.rb', line 83

def download_url
  get_response.download_url
end

#get_responseObject



75
76
77
# File 'lib/ezid/batch_download.rb', line 75

def get_response
  @response ||= client.batch_download(params)
end

#paramsObject



71
72
73
# File 'lib/ezid/batch_download.rb', line 71

def params
  to_h
end

#reloadObject



79
80
81
# File 'lib/ezid/batch_download.rb', line 79

def reload
  @response = nil
end