-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
222 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
sudo: false | ||
language: ruby | ||
rvm: | ||
- 2.2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,21 +113,24 @@ I, [2014-12-04T15:12:48.853964 #86734] INFO -- : EZID DeleteIdentifier -- succe | |
|
||
## Batch Download | ||
|
||
Instantiate an `Ezid::Client` and call `batch_download` with hash options -- see http://ezid.cdlib.org/doc/apidoc.html#parameters. Repeated values should be given as an array value for the parameter key. | ||
|
||
Note that, due to the asynchronous nature of this request, the response only returns the URL at which the batch will be available to download (as described in the EZID documentation). Use the `notify` option to specify one or more email addresses to receive notification when the download file is actually available. | ||
|
||
**Example** | ||
|
||
``` | ||
>> c = Ezid::Client.new | ||
=> #<Ezid::Client connection=#<Net::HTTP ezid.cdlib.org:443 open=false> user="eziduser" session=CLOSED> | ||
>> response = c.batch_download(format: "csv", notify: "[email protected]", column: ["_id", "_target", "_status", "_profile", "_export", "_created", "_updated"], convertTimestamps: "yes", permanence: "real", owner: "eziduser") | ||
I, [2015-02-20T15:16:53.462660 #55850] INFO -- : EZID BatchDownload -- success: http://ezid.cdlib.org/download/473deecb96.csv.gz | ||
=> #<Net::HTTPOK 200 OK readbody=true> | ||
>> response.download_url | ||
=> "http://ezid.cdlib.org/download/da543b91a0.csv.gz" | ||
``` | ||
See http://ezid.cdlib.org/doc/apidoc.html#parameters. Repeated values should be given as an array value for the parameter key. | ||
|
||
``` | ||
>> batch = Ezid::BatchDownload.new(:csv) | ||
=> #<Ezid::BatchDownload format=:csv> | ||
>> batch.column = ["_id", "_target"] | ||
=> ["_id", "_target"] | ||
>> batch.createdAfter = Date.today.to_time | ||
=> 2016-02-24 00:00:00 -0500 | ||
>> batch | ||
=> #<Ezid::BatchDownload column=["_id", "_target"] createdAfter=1456290000 format=:csv> | ||
>> batch.download_url | ||
I, [2016-02-24T18:03:40.828005 #1084] INFO -- : EZID BatchDownload -- success: http://ezid.cdlib.org/download/4a63401e17.csv.gz | ||
=> "http://ezid.cdlib.org/download/4a63401e17.csv.gz" | ||
>> batch.download_file | ||
File successfully download to /current/working/directory/4a63401e17.csv.gz. | ||
=> nil | ||
``` | ||
|
||
## Metadata handling | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.2.0 | ||
1.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
require "hashie" | ||
require "net/http" | ||
require "uri" | ||
require_relative "reserved_metadata" | ||
|
||
module Ezid | ||
class BatchDownloadError < Error; end | ||
|
||
class BatchDownload < Hashie::Dash | ||
include Hashie::Extensions::Coercion | ||
|
||
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 | ||
|
||
# CSV Columns | ||
ID = "_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 | ||
|
||
# Parameters | ||
property :format, required: true # {anvl|csv|xml} | ||
property :column # repeatable | ||
property :notify # repeatable | ||
property :convertTimestamps # {yes|no} | ||
|
||
# Search constraints | ||
property :createdAfter | ||
property :createdBefore | ||
property :crossref # {yes|no} | ||
property :exported # {yes|no} | ||
property :owner # repeatable | ||
property :ownergroup # repeatable | ||
property :permanence # {test|real} | ||
property :profile # (repeatable) | ||
property :status # {reserved|public|unavailable} (repeatable) | ||
property :type # {ark|doi|urn} (repeatable) | ||
property :updatedAfter | ||
property :updatedBefore | ||
|
||
coerce_value FalseClass, ->(v) { NO } | ||
coerce_value TrueClass, ->(v) { YES } | ||
coerce_value DateTime, ->(v) { v.to_time.utc.iso8601 } | ||
coerce_value Time, Integer | ||
|
||
def initialize(format, args={}) | ||
super(args.merge(format: format)) | ||
end | ||
|
||
def params | ||
to_h | ||
end | ||
|
||
def get_response | ||
@response ||= client.batch_download(params) | ||
end | ||
|
||
def reload | ||
@response = nil | ||
end | ||
|
||
def download_url | ||
get_response.download_url | ||
end | ||
|
||
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 | ||
|
||
private | ||
|
||
def download_uri | ||
URI(download_url) | ||
end | ||
|
||
def download_filename | ||
File.basename(download_uri.path) | ||
end | ||
|
||
def client | ||
Client.new | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Ezid | ||
# | ||
# EZID reserved metadata elements | ||
# | ||
# @see http://ezid.cdlib.org/doc/apidoc.html#internal-metadata | ||
# | ||
module ReservedMetadata | ||
COOWNERS = "_coowners".freeze | ||
CREATED = "_created".freeze | ||
DATACENTER = "_datacenter".freeze | ||
EXPORT = "_export".freeze | ||
OWNER = "_owner".freeze | ||
OWNERGROUP = "_ownergroup".freeze | ||
PROFILE = "_profile".freeze | ||
SHADOWEDBY = "_shadowedby".freeze | ||
SHADOWS = "_shadows".freeze | ||
STATUS = "_status".freeze | ||
TARGET = "_target".freeze | ||
UPDATED = "_updated".freeze | ||
|
||
# Read-only elements | ||
READONLY = [ | ||
CREATED, DATACENTER, OWNER, OWNERGROUP, SHADOWEDBY, SHADOWS, UPDATED | ||
].freeze | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Ezid | ||
# | ||
# EZID status terms | ||
# | ||
module Status | ||
PUBLIC = "public".freeze | ||
RESERVED = "reserved".freeze | ||
UNAVAILABLE = "unavailable".freeze | ||
end | ||
end |
Oops, something went wrong.