Skip to content

Commit

Permalink
Merge branch 'release-0.13.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dchandekstark committed Feb 20, 2015
2 parents 6f87471 + 60b927a commit 2087d8b
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 30 deletions.
70 changes: 56 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Or install it yourself as:

```
>> identifier = Ezid::Identifier.create(shoulder: "ark:/99999/fk4")
I, [2014-12-04T15:06:02.428445 #86655] INFO -- : EZID MINT ark:/99999/fk4 -- success: ark:/99999/fk4rx9d523
I, [2014-12-04T15:06:03.249793 #86655] INFO -- : EZID GET ark:/99999/fk4rx9d523 -- success: ark:/99999/fk4rx9d523
I, [2014-12-04T15:06:02.428445 #86655] INFO -- : EZID MintIdentifier -- success: ark:/99999/fk4rx9d523
I, [2014-12-04T15:06:03.249793 #86655] INFO -- : EZID GetIdentifierMetadata -- success: ark:/99999/fk4rx9d523
=> #<Ezid::Identifier id="ark:/99999/fk4rx9d523" status="public" target="http://ezid.cdlib.org/id/ark:/99999/fk4rx9d523" created="2014-12-04 20:06:02 UTC">
>> identifier.id
=> "ark:/99999/fk4rx9d523"
Expand Down Expand Up @@ -60,25 +60,25 @@ New identifiers will then be minted on the default shoulder when a shoulder is n

```
>> identifier = Ezid::Identifier.create
I, [2014-12-09T11:22:34.499860 #32279] INFO -- : EZID MINT ark:/99999/fk4 -- success: ark:/99999/fk43f4wd4v
I, [2014-12-09T11:22:35.317181 #32279] INFO -- : EZID GET ark:/99999/fk43f4wd4v -- success: ark:/99999/fk43f4wd4v
I, [2014-12-09T11:22:34.499860 #32279] INFO -- : EZID MintIdentifier -- success: ark:/99999/fk43f4wd4v
I, [2014-12-09T11:22:35.317181 #32279] INFO -- : EZID GetIdentifierMetadata -- success: ark:/99999/fk43f4wd4v
=> #<Ezid::Identifier id="ark:/99999/fk43f4wd4v" status="public" target="http://ezid.cdlib.org/id/ark:/99999/fk43f4wd4v" created="2014-12-09 16:22:35 UTC">
```

[Create a specific identifier](http://ezid.cdlib.org/doc/apidoc.html#operation-create-identifier)

```
>> identifier = Ezid::Identifier.create(id: "ark:/99999/fk4rx9d523/12345")
I, [2014-12-09T11:21:42.077297 #32279] INFO -- : EZID CREATE ark:/99999/fk4rx9d523/12345 -- success: ark:/99999/fk4rx9d523/12345
I, [2014-12-09T11:21:42.808534 #32279] INFO -- : EZID GET ark:/99999/fk4rx9d523/12345 -- success: ark:/99999/fk4rx9d523/12345
I, [2014-12-09T11:21:42.077297 #32279] INFO -- : EZID CreateIdentifier -- success: ark:/99999/fk4rx9d523/12345
I, [2014-12-09T11:21:42.808534 #32279] INFO -- : EZID GetIdentifierMetadata -- success: ark:/99999/fk4rx9d523/12345
=> #<Ezid::Identifier id="ark:/99999/fk4rx9d523/12345" status="public" target="http://ezid.cdlib.org/id/ark:/99999/fk4rx9d523/12345" created="2014-12-09 16:21:42 UTC">
```

**Retrieve** (Get Metadata)

```
>> identifier = Ezid::Identifier.find("ark:/99999/fk4rx9d523")
I, [2014-12-04T15:07:00.648676 #86655] INFO -- : EZID GET ark:/99999/fk4rx9d523 -- success: ark:/99999/fk4rx9d523
I, [2014-12-04T15:07:00.648676 #86655] INFO -- : EZID GetIdentifierMetadata -- success: ark:/99999/fk4rx9d523
=> #<Ezid::Identifier id="ark:/99999/fk4rx9d523" status="public" target="http://ezid.cdlib.org/id/ark:/99999/fk4rx9d523" created="2014-12-04 20:06:02 UTC">
```

Expand All @@ -90,8 +90,8 @@ I, [2014-12-04T15:07:00.648676 #86655] INFO -- : EZID GET ark:/99999/fk4rx9d523
>> identifier.target = "http://example.com"
=> "http://example.com"
>> identifier.save
I, [2014-12-09T11:24:26.321801 #32279] INFO -- : EZID MODIFY ark:/99999/fk43f4wd4v -- success: ark:/99999/fk43f4wd4v
I, [2014-12-09T11:24:27.039288 #32279] INFO -- : EZID GET ark:/99999/fk43f4wd4v -- success: ark:/99999/fk43f4wd4v
I, [2014-12-09T11:24:26.321801 #32279] INFO -- : EZID ModifyIdentifier -- success: ark:/99999/fk43f4wd4v
I, [2014-12-09T11:24:27.039288 #32279] INFO -- : EZID GetIdentifierMetadata -- success: ark:/99999/fk43f4wd4v
=> #<Ezid::Identifier id="ark:/99999/fk43f4wd4v" status="public" target="http://example.com" created="2014-12-09 16:22:35 UTC">
>> identifier.target
=> "http://example.com"
Expand All @@ -103,14 +103,32 @@ I, [2014-12-09T11:24:27.039288 #32279] INFO -- : EZID GET ark:/99999/fk43f4wd4v

```
>> identifier = Ezid::Identifier.create(shoulder: "ark:/99999/fk4", status: "reserved")
I, [2014-12-04T15:12:39.976930 #86734] INFO -- : EZID MINT ark:/99999/fk4 -- success: ark:/99999/fk4n58pc0r
I, [2014-12-04T15:12:40.693256 #86734] INFO -- : EZID GET ark:/99999/fk4n58pc0r -- success: ark:/99999/fk4n58pc0r
I, [2014-12-04T15:12:39.976930 #86734] INFO -- : EZID MintIdentifier -- success: ark:/99999/fk4n58pc0r
I, [2014-12-04T15:12:40.693256 #86734] INFO -- : EZID GetIdentifierMetadata -- success: ark:/99999/fk4n58pc0r
=> #<Ezid::Identifier id="ark:/99999/fk4n58pc0r" status="reserved" target="http://ezid.cdlib.org/id/ark:/99999/fk4n58pc0r" created="2014-12-04 20:12:39 UTC">
>> identifier.delete
I, [2014-12-04T15:12:48.853964 #86734] INFO -- : EZID DELETE ark:/99999/fk4n58pc0r -- success: ark:/99999/fk4n58pc0r
I, [2014-12-04T15:12:48.853964 #86734] INFO -- : EZID DeleteIdentifier -- success: ark:/99999/fk4n58pc0r
=> #<Ezid::Identifier id="ark:/99999/fk4n58pc0r" DELETED>
```

## 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"
```

## Metadata handling

Accessors are provided to ease the use of EZID [reserved metadata elements](http://ezid.cdlib.org/doc/apidoc.html#internal-metadata) and [metadata profiles](http://ezid.cdlib.org/doc/apidoc.html#metadata-profiles):
Expand Down Expand Up @@ -169,8 +187,8 @@ Then new identifiers will receive the defaults:

```
>> identifier = Ezid::Identifier.create(shoulder: "ark:/99999/fk4")
I, [2014-12-09T11:38:37.335136 #32279] INFO -- : EZID MINT ark:/99999/fk4 -- success: ark:/99999/fk4zs2w500
I, [2014-12-09T11:38:38.153546 #32279] INFO -- : EZID GET ark:/99999/fk4zs2w500 -- success: ark:/99999/fk4zs2w500
I, [2014-12-09T11:38:37.335136 #32279] INFO -- : EZID MintIdentifier -- success: ark:/99999/fk4zs2w500
I, [2014-12-09T11:38:38.153546 #32279] INFO -- : EZID GetIdentifierMetadata -- success: ark:/99999/fk4zs2w500
=> #<Ezid::Identifier id="ark:/99999/fk4zs2w500" status="reserved" target="http://ezid.cdlib.org/id/ark:/99999/fk4zs2w500" created="2014-12-09 16:38:38 UTC">
>> identifier.profile
=> "dc"
Expand Down Expand Up @@ -232,6 +250,30 @@ end
client = Ezid::Client.new(host: "localhost", port: 80)
```

## HTTP Timeout

The default HTTP timeout is set to 300 seconds (5 minutes). The setting can be customized:

- By environment variable:

```sh
export EZID_TIMEOUT=600
```

- Client configuration:

```ruby
Ezid::Client.configure do |config|
config.timeout = 600
end
```

- At client initialization

```ruby
client = Ezid::Client.new(timeout: 600)
```

## Test Helper

If you have tests that (directly or indirectly) use `ezid-client` you may want to require the test helper module:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.0
0.13.0
12 changes: 11 additions & 1 deletion lib/ezid/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ def version
end
end

attr_reader :user, :password, :host, :port, :use_ssl
attr_reader :user, :password, :host, :port, :use_ssl, :timeout

def initialize(opts = {})
@host = opts[:host] || config.host
@port = (opts[:port] || config.port).to_i
@use_ssl = opts[:use_ssl] || config.use_ssl
@timeout = (opts[:timeout] || config.timeout).to_i
@user = opts[:user] || config.user
@password = opts[:password] || config.password
if block_given?
Expand Down Expand Up @@ -169,6 +170,14 @@ def server_status(*subsystems)
execute ServerStatusRequest, *subsystems
end

# Submit a batch download request
# @see http://ezid.cdlib.org/doc/apidoc.html#batch-download
# @param format [String] format of results - one of "anvl", "csv", "xml"
# @param params [Hash] optional request parameters
def batch_download(params={})
execute BatchDownloadRequest, params
end

# The Net::HTTP object used to connect to EZID
# @return [Net::HTTP] the connection
def connection
Expand All @@ -184,6 +193,7 @@ def use_ssl?
def build_connection
conn = Net::HTTP.new(host, port)
conn.use_ssl = use_ssl?
conn.read_timeout = timeout
conn
end

Expand Down
15 changes: 6 additions & 9 deletions lib/ezid/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,27 @@ class Configuration

HOST = "ezid.cdlib.org"
PORT = 443
TIMEOUT = 300

# EZID host name
# Default: value of `EZID_HOST` environment variable, if present, or
# the EZID service host "ezid.cdlib.org".
attr_accessor :host

# EZID TCP/IP port
# Default: value of `EZID_PORT` variable
attr_accessor :port

# Use HTTPS?
# Default: `nil`; `true` if `EZID_USE_SSL` environment variable is set
# to the string "true".
attr_accessor :use_ssl

# HTTP read timeout (seconds)
attr_accessor :timeout

# EZID user name
# Default: value of `EZID_USER` environment variable
attr_accessor :user

# EZID password
# Default: value of `EZID_PASSWORD` environment variable
attr_accessor :password

# Ruby logger instance
# Default device: STDERR
attr_writer :logger

# Default shoulder for minting (scheme + NAAN + shoulder)
Expand All @@ -48,7 +44,8 @@ def initialize
@password = ENV["EZID_PASSWORD"]
@host = ENV["EZID_HOST"] || HOST
@port = ENV["EZID_PORT"] || PORT
@use_ssl = true if ENV["EZID_USE_SSL"] == true.to_s
@use_ssl = vvciujcrdjhcbtrue if ENV["EZID_USE_SSL"] == true.to_s
@timeout = ENV["EZID_TIMEOUT"] || TIMEOUT
@default_shoulder = ENV["EZID_DEFAULT_SHOULDER"]
end

Expand Down
22 changes: 22 additions & 0 deletions lib/ezid/requests/batch_download_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require_relative "request"

module Ezid
class BatchDownloadRequest < Request

self.http_method = POST
self.path = "/download_request"
self.response_class = BatchDownloadResponse

attr_reader :params

def initialize(client, params={})
@params = params
super
end

def customize_request
set_form_data(params)
end

end
end
9 changes: 5 additions & 4 deletions lib/ezid/requests/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ module Ezid
class Request < SimpleDelegator
extend Forwardable

CHARSET = "UTF-8"
CONTENT_TYPE = "text/plain"

# HTTP methods
GET = Net::HTTP::Get
PUT = Net::HTTP::Put
Expand Down Expand Up @@ -45,6 +42,7 @@ def short_name
def initialize(client, *args)
@client = client
super build_request
customize_request
end

# Executes the request and returns the response
Expand Down Expand Up @@ -93,13 +91,16 @@ def handle_response(http_response)

def get_response_for_request
connection.start do |conn|
set_content_type(CONTENT_TYPE, charset: CHARSET)
add_authentication if authentication_required?
add_metadata if has_metadata?
conn.request(__getobj__)
end
end

def customize_request
set_content_type("text/plain", charset: "UTF-8")
end

def build_request
self.class.http_method.new(uri)
end
Expand Down
11 changes: 11 additions & 0 deletions lib/ezid/responses/batch_download_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require_relative "response"

module Ezid
class BatchDownloadResponse < Response

def download_url
message
end

end
end
17 changes: 16 additions & 1 deletion spec/integration/client_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "time"

module Ezid
RSpec.describe Client do

Expand All @@ -9,7 +11,6 @@ module Ezid
modified = client.modify_identifier(@id, "dc.title" => "Test")
expect(modified).to be_success
retrieved = client.get_identifier_metadata(@id)
puts retrieved.class.to_s
expect(retrieved).to be_success
expect(retrieved.metadata).to match(/dc.title: Test/)
deleted = client.delete_identifier(@id)
Expand Down Expand Up @@ -63,5 +64,19 @@ module Ezid
end
end

describe "batch download" do
before do
r = subject.mint_identifier
r = subject.get_identifier_metadata(r.id)
metadata = Metadata.new(r.metadata)
@created = metadata.created.iso8601
end
it "should return a valid URL" do
response = subject.batch_download(format: "anvl", createdAfter: @created, permanence: "test")
expect(response).to be_success
expect(response.download_url).to match(/^http:\/\/ezid.cdlib.org\/download\/[^\/]+.gz$/)
end
end

end
end
26 changes: 26 additions & 0 deletions spec/unit/batch_download_request_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Ezid
RSpec.describe BatchDownloadRequest do

let(:client) { Client.new }
let(:params) do
{ format: "xml",
notify: "[email protected]",
convertTimestamps: "yes",
exported: "no",
owner: ["you", "me"],
status: ["reserved", "unavailable"],
type: "ark"
}
end
subject { described_class.new(client, params) }

it "should add the request params to the request body" do
expect(subject.body).to eq("format=xml&notify=noreply%40example.com&convertTimestamps=yes&exported=no&owner=you&owner=me&status=reserved&status=unavailable&type=ark")
end

it "should have the correct content type" do
expect(subject.content_type).to eq("application/x-www-form-urlencoded")
end

end
end
13 changes: 13 additions & 0 deletions spec/unit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ module Ezid
end
end

describe "batch download" do
let(:stub_response) { BatchDownloadResponse.new(http_response) }
let(:http_response) { double(body: "success: http://ezid.cdlib.org/download/da543b91a0.xml.gz") }
before do
allow(BatchDownloadRequest).to receive(:execute).with(subject, format: "xml") { stub_response }
end
it "should return the URL to download the batch" do
response = subject.batch_download(format: "xml")
expect(response).to be_success
expect(response.download_url).to eq("http://ezid.cdlib.org/download/da543b91a0.xml.gz")
end
end

describe "error handling" do
let(:http_response) { double(body: "error: bad request - no such identifier") }
it "should raise an exception" do
Expand Down

0 comments on commit 2087d8b

Please sign in to comment.