ezid-client 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 024d805940fcc9a0ccbe8ed0e6e80177d05393f6
4
- data.tar.gz: e488fafa09132f630a2047cd4008fd653a9a221a
3
+ metadata.gz: 044a6bd05e5a81f2990f4d865515af5f1679e853
4
+ data.tar.gz: 7d45fe9bca596a17562812f5a6adf4ef73649665
5
5
  SHA512:
6
- metadata.gz: 3f32330825caf59ba03f3e7765226fe5f9aaef98877a12c9ac1396ac98a2511863f57f9b2619adea40d425b9b09ae5c97b311614d1b9e21a52ec4007217a9511
7
- data.tar.gz: 4972d261b297f95f763499c92882fe1600238a3e5865bd510b41dc780ba33904f22b17f95fc1c87ff9aaeddfdb6e761b53c1159d9d011f920e7bfc7818a433e5
6
+ metadata.gz: 49710a5dd7aed59d88ae6e2463267ee429534d46597096684d16236d8d54c7eeb8bced1f12b7ed196f8e8eba8c7a2e9505244ec8a97d6c6b60019279e8a50ad8
7
+ data.tar.gz: 7084e2afebd769a138109732c01e6017eea687f6c1eb3f3ced5f84e18862dad2f4030a7830271d97cbceb88c69d9024d615b9a3b053da4d6f172615e2798149d
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Ezid::Client
1
+ # EZID Client
2
2
 
3
3
  EZID API Version 2 bindings. See http://ezid.cdlib.org/doc/apidoc.html.
4
4
 
@@ -71,7 +71,7 @@ I, [2014-12-04T15:12:48.853964 #86734] INFO -- : EZID DELETE ark:/99999/fk4n58p
71
71
 
72
72
  ## Metadata handling
73
73
 
74
- See `Ezid::Metadata` class and examples in `spec/unit/metadata_spec.rb`.
74
+ Although "EZID imposes no requirements on the presence or form of citation metadata"[*](http://ezid.cdlib.org/doc/apidoc.html#metadata-requirements-mapping), `ezid-client` is intended to support the 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). While it is possible to use the client to send and receive any metadata, the object methods are geared towards the defined elements. Therefore it was seen fit, for example, to map the method `Ezid::Identifier#status` to the "_status" element. Likewise, all the reserved elements, except for "_crossref", have readers and -- for user-writable elements -- writers without the leading underscores. Since there are both "_crossref" and "crossref" elements, their accessors match the elements names. Similarly, accessors for metadata profile elements use underscores in place of dots -- for example, `Ezid::Identifer#dc_title` and `#dc_title=` for the "dc.title" element.
75
75
 
76
76
  ## Authentication
77
77
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
@@ -107,7 +107,8 @@ module Ezid
107
107
  # @param metadata [String, Hash, Ezid::Metadata] metadata to set
108
108
  # @raise [Ezid::Error]
109
109
  # @return [Ezid::Response] the response
110
- def mint_identifier(shoulder, metadata=nil)
110
+ def mint_identifier(shoulder=nil, metadata=nil)
111
+ shoulder ||= config.default_shoulder
111
112
  raise Error, "Shoulder missing -- cannot mint identifier." unless shoulder
112
113
  response = Request.execute(:Post, "/shoulder/#{shoulder}") do |request|
113
114
  add_authentication(request)
@@ -10,42 +10,43 @@ module Ezid
10
10
  class Configuration
11
11
 
12
12
  # EZID user name
13
- # Default: value of EZID_USER environment variable
13
+ # Default: value of `EZID_USER` environment variable
14
14
  attr_accessor :user
15
15
 
16
16
  # EZID password
17
- # Default: value of EZID_PASSWORD environment variable
17
+ # Default: value of `EZID_PASSWORD` environment variable
18
18
  attr_accessor :password
19
19
 
20
20
  # Ruby logger instance
21
21
  # Default device: STDERR
22
22
  attr_writer :logger
23
23
 
24
- # Default metadata profile
24
+ # Default metadata profile - "erc" (EZID default), "dc", "datacite", or "crossref"
25
+ # If set, new identifiers (created or minted) will set the "_profile" element to
26
+ # this value.
25
27
  # attr_accessor :default_metadata_profile
26
28
 
27
- # Default status - set only if default should not "public" (EZID default)
29
+ # Default status - "public" (EZID default), "reserved", or "unavailable"
30
+ # If set, new identifiers (created or minted) will set the "_status" element to
31
+ # this value.
28
32
  # attr_accessor :default_status
29
33
 
30
- # Default shoulder for minting
31
- # attr_accessor :default_shoulder
32
-
33
- # Hash of options to pass to Net::HTTP.start
34
- # attr_accessor :http_request_options
34
+ # Default shoulder for minting (scheme + NAAN + shoulder)
35
+ # @example "ark:/99999/fk4"
36
+ attr_accessor :default_shoulder
35
37
 
36
38
  def initialize
37
39
  @user = ENV["EZID_USER"]
38
40
  @password = ENV["EZID_PASSWORD"]
39
- # @http_request_options = default_http_request_options
40
41
  end
41
42
 
42
43
  def logger
43
44
  @logger ||= Logger.new(STDERR)
44
45
  end
45
46
 
46
- # def default_http_request_options
47
- # { use_ssl: true }
48
- # end
47
+ def identifier
48
+ Identifier
49
+ end
49
50
 
50
51
  end
51
52
  end
@@ -23,7 +23,9 @@ module Ezid
23
23
  RESERVED = "reserved"
24
24
  UNAVAILABLE = "unavailable"
25
25
 
26
- class << self
26
+ class << self
27
+ attr_accessor :defaults
28
+
27
29
  # Creates or mints an identifier (depending on arguments)
28
30
  # @see #save
29
31
  # @return [Ezid::Identifier] the new identifier
@@ -42,13 +44,14 @@ module Ezid
42
44
  end
43
45
  end
44
46
 
47
+ self.defaults = {}
48
+
45
49
  def initialize(args={})
46
50
  @client = args.delete(:client) || Client.new
47
51
  @id = args.delete(:id)
48
52
  @shoulder = args.delete(:shoulder)
49
- @metadata = Metadata.new(args.delete(:metadata))
50
- update_metadata(args)
51
53
  @deleted = false
54
+ init_metadata(args)
52
55
  end
53
56
 
54
57
  def inspect
@@ -73,11 +76,7 @@ module Ezid
73
76
  # with an error status.
74
77
  def save
75
78
  raise Error, "Cannot save a deleted identifier." if deleted?
76
- if persisted?
77
- modify
78
- else
79
- create_or_mint
80
- end
79
+ persisted? ? modify : create_or_mint
81
80
  reload
82
81
  end
83
82
 
@@ -154,13 +153,6 @@ module Ezid
154
153
  status == UNAVAILABLE
155
154
  end
156
155
 
157
- protected
158
-
159
- def method_missing(name, *args)
160
- return metadata.send(name, *args) if metadata.respond_to?(name)
161
- super
162
- end
163
-
164
156
  private
165
157
 
166
158
  def refresh_metadata
@@ -177,13 +169,7 @@ module Ezid
177
169
  end
178
170
 
179
171
  def create_or_mint
180
- if id
181
- create
182
- elsif shoulder
183
- mint
184
- else
185
- raise Error, "Unable to create or mint identifier when neither `id' nor `shoulder' present."
186
- end
172
+ id ? create : mint
187
173
  end
188
174
 
189
175
  def mint
@@ -195,8 +181,9 @@ module Ezid
195
181
  client.create_identifier(id, metadata)
196
182
  end
197
183
 
198
- def init_metadata(args={})
184
+ def init_metadata(args)
185
+ @metadata = Metadata.new(args.delete(:metadata))
186
+ update_metadata(self.class.defaults.merge(args))
199
187
  end
200
-
201
188
  end
202
189
  end
@@ -1,21 +1,55 @@
1
+
2
+
1
3
  module Ezid
2
4
  RSpec.describe Identifier do
3
5
 
4
- it "should handle CRUD operations" do
5
- # create (mint)
6
- identifier = described_class.create(shoulder: ARK_SHOULDER)
7
- expect(identifier.status).to eq("public")
8
- # update
9
- identifier.target = "http://example.com"
10
- identifier.save
11
- # retrieve
12
- identifier = described_class.find(identifier.id)
13
- expect(identifier.target).to eq("http://example.com")
14
- # delete
15
- identifier = described_class.create(shoulder: ARK_SHOULDER, status: "reserved")
16
- identifier.delete
17
- expect { described_class.find(identifier.id) }.to raise_error
18
- end
6
+ describe "CRUD operations" do
7
+ describe "create" do
8
+ describe "with a shoulder" do
9
+ subject { described_class.create(shoulder: ARK_SHOULDER) }
10
+ it "should mint an identifier" do
11
+ expect(subject).to be_a(described_class)
12
+ expect(subject.id).to match(/#{ARK_SHOULDER}/)
13
+ end
14
+ end
15
+ describe "with an id" do
16
+ let(:minted) { described_class.create(shoulder: ARK_SHOULDER) }
17
+ subject { described_class.create(id: "#{minted}/123") }
18
+ it "should create the identifier" do
19
+ expect(subject).to be_a(described_class)
20
+ expect(subject.id).to eq("#{minted}/123")
21
+ end
22
+ end
23
+ end
19
24
 
25
+ describe "retrieve" do
26
+ let(:minted) { described_class.create(shoulder: ARK_SHOULDER, target: "http://example.com") }
27
+ subject { described_class.find(minted.id) }
28
+ it "should instantiate the identifier" do
29
+ expect(subject).to be_a(described_class)
30
+ expect(subject.id).to eq(minted.id)
31
+ expect(subject.target).to eq("http://example.com")
32
+ end
33
+ end
34
+
35
+ describe "update" do
36
+ subject { described_class.create(shoulder: ARK_SHOULDER, target: "http://google.com") }
37
+ before do
38
+ subject.target = "http://example.com"
39
+ subject.save
40
+ end
41
+ it "should update the metadata" do
42
+ expect(subject.target).to eq("http://example.com")
43
+ end
44
+ end
45
+
46
+ describe "delete" do
47
+ subject { described_class.create(shoulder: ARK_SHOULDER, status: "reserved") }
48
+ before { subject.delete }
49
+ it "should delete the identifier" do
50
+ expect { described_class.find(subject.id) }.to raise_error
51
+ end
52
+ end
53
+ end
20
54
  end
21
55
  end
@@ -86,7 +86,7 @@ RSpec.configure do |config|
86
86
  # Print the 10 slowest examples and example groups at the
87
87
  # end of the spec run, to help surface which specs are running
88
88
  # particularly slow.
89
- # config.profile_examples = 10
89
+ config.profile_examples = 5
90
90
 
91
91
  # Run specs in random order to surface order dependencies. If you find an
92
92
  # order dependency and want to debug it, you can fix the order by providing
@@ -23,9 +23,9 @@ module Ezid
23
23
  end
24
24
 
25
25
  describe "#mint_identifier" do
26
- before { allow(Request).to receive(:execute) { stub_response } }
27
26
  describe "which is an ARK" do
28
27
  let(:stub_response) { Response.new(double(body: "success: ark:/99999/fk4fn19h88")) }
28
+ before { allow(Request).to receive(:execute).with(:Post, "/shoulder/#{ARK_SHOULDER}") { stub_response } }
29
29
  subject { described_class.new.mint_identifier(ARK_SHOULDER) }
30
30
  it "should be a succes" do
31
31
  expect(subject).to be_success
@@ -44,6 +44,7 @@ datacite.publicationyear: 2014
44
44
  datacite.resourcetype: Other
45
45
  EOS
46
46
  end
47
+ before { allow(Request).to receive(:execute).with(:Post, "/shoulder/#{DOI_SHOULDER}") { stub_response } }
47
48
  subject { described_class.new.mint_identifier(DOI_SHOULDER, metadata) }
48
49
  it "should be a sucess" do
49
50
  expect(subject).to be_success
@@ -51,6 +52,24 @@ EOS
51
52
  expect(subject.shadow_ark).to eq("ark:/99999/fk4fn19h88")
52
53
  end
53
54
  end
55
+ describe "when a shoulder is not given" do
56
+ let(:stub_response) { Response.new(double(body: "success: ark:/99999/fk4fn19h88")) }
57
+ context "and the :default_shoulder config option is set" do
58
+ subject { described_class.new.mint_identifier }
59
+ before do
60
+ allow(Request).to receive(:execute).with(:Post, "/shoulder/#{ARK_SHOULDER}") { stub_response }
61
+ allow(Client.config).to receive(:default_shoulder) { ARK_SHOULDER }
62
+ end
63
+ it "should use the default shoulder" do
64
+ expect(subject).to be_success
65
+ end
66
+ end
67
+ context "and the :default_shoulder config option is not set" do
68
+ it "should raise an exception" do
69
+ expect { described_class.new.mint_identifier }.to raise_error
70
+ end
71
+ end
72
+ end
54
73
  end
55
74
 
56
75
  describe "#get_identifier_metadata" do
@@ -2,91 +2,87 @@ module Ezid
2
2
  RSpec.describe Identifier do
3
3
 
4
4
  describe ".create" do
5
- describe "when given an id" do
6
- let(:id) { "ark:/99999/fk4zzzzzzz" }
7
- subject { described_class.create(id: id) }
8
- before do
9
- allow_any_instance_of(Client).to receive(:create_identifier).with(id, {}) { double(id: id) }
10
- allow_any_instance_of(Client).to receive(:get_identifier_metadata).with(id) { double(metadata: {}) }
11
- end
12
- it "should create an identifier" do
13
- expect(subject).to be_a(described_class)
14
- expect(subject.id).to eq(id)
15
- end
16
- end
17
- describe "when given a shoulder (and no id)" do
18
- let(:id) { "ark:/99999/fk4fn19h88" }
19
- subject { described_class.create(shoulder: ARK_SHOULDER) }
20
- before do
21
- allow_any_instance_of(Client).to receive(:mint_identifier).with(ARK_SHOULDER, {}) { double(id: id) }
22
- allow_any_instance_of(Client).to receive(:get_identifier_metadata).with(id) { double(metadata: {}) }
23
- end
24
- it "should mint an identifier" do
25
- expect(subject).to be_a(described_class)
26
- expect(subject.id).to eq(id)
27
- end
5
+ let(:attrs) { {shoulder: ARK_SHOULDER, profile: "dc", target: "http://example.com"} }
6
+ it "should instantiate a new Identifier and save it" do
7
+ expect(described_class).to receive(:new).with(attrs).and_call_original
8
+ expect_any_instance_of(described_class).to receive(:save) { double }
9
+ described_class.create(attrs)
28
10
  end
29
11
  describe "when given neither an id nor a shoulder" do
12
+ before { allow(described_class).to receive(:defaults) {} }
30
13
  it "should raise an exception" do
31
14
  expect { described_class.create }.to raise_error
32
15
  end
33
16
  end
34
- describe "with metadata" do
35
- it "should send the metadata"
36
- end
37
17
  end
38
18
 
39
19
  describe ".find" do
40
- describe "when the id exists" do
41
- let(:id) { "ark:/99999/fk4fn19h88" }
42
- subject { described_class.find(id) }
43
- before do
44
- allow_any_instance_of(Client).to receive(:get_identifier_metadata).with(id) { double(id: id, metadata: {}) }
20
+ it "should instantiate a new identifier and reload" do
21
+ expect(described_class).to receive(:new).with(id: "id").and_call_original
22
+ expect_any_instance_of(described_class).to receive(:reload) { double }
23
+ described_class.find("id")
24
+ end
25
+ end
26
+
27
+ describe ".defaults" do
28
+ before { @original_defaults = described_class.defaults }
29
+ after { described_class.defaults = @original_defaults }
30
+ it "should be settable via client config" do
31
+ Client.config.identifier.defaults = {status: "reserved"}
32
+ expect(described_class.defaults).to eq({status: "reserved"})
33
+ end
34
+ end
35
+
36
+ describe "#initialize" do
37
+ describe "with metadata" do
38
+ describe "via the :metadata argument" do
39
+ subject { described_class.new(metadata: "_profile: dc\n_target: http://example.com") }
40
+ it "should set the metadata" do
41
+ expect(subject.profile).to eq("dc")
42
+ expect(subject.target).to eq("http://example.com")
43
+ end
45
44
  end
46
- it "should get the identifier" do
47
- expect(subject).to be_a(Identifier)
48
- expect(subject.id).to eq(id)
45
+ describe "via keyword arguments" do
46
+ subject { described_class.new(profile: "dc", target: "http://example.com") }
47
+ it "should set the metadata" do
48
+ expect(subject.profile).to eq("dc")
49
+ expect(subject.target).to eq("http://example.com")
50
+ end
49
51
  end
50
52
  end
51
- describe "when the id does not exist" do
52
- let(:id) { "ark:/99999/fk4zzzzzzz" }
53
+ describe "default metadata" do
53
54
  before do
54
- allow_any_instance_of(Client).to receive(:get_identifier_metadata).with(id).and_raise(Error)
55
+ allow(described_class).to receive(:defaults) { {profile: "dc", status: "reserved"} }
55
56
  end
56
- it "should raise an exception" do
57
- expect { described_class.find(id) }.to raise_error
57
+ it "should set the default metadata" do
58
+ expect(subject.profile).to eq("dc")
59
+ expect(subject.status).to eq("reserved")
60
+ end
61
+ context "when explicit arguments override the defaults" do
62
+ subject { described_class.new(shoulder: ARK_SHOULDER, status: "public") }
63
+ it "should override the defaults" do
64
+ expect(subject.profile).to eq("dc")
65
+ expect(subject.status).to eq("public")
66
+ end
58
67
  end
59
68
  end
60
69
  end
61
70
 
62
71
  describe "#update" do
63
- let(:id) { "ark:/99999/fk4fn19h88" }
64
72
  let(:metadata) { {"status" => "unavailable"} }
65
- subject { described_class.new(id: id) }
66
- before do
67
- allow(subject).to receive(:persisted?) { true }
68
- allow(subject.client).to receive(:modify_identifier).with(id, subject.metadata) do
69
- double(id: id, metadata: {})
70
- end
71
- end
72
- it "should update the metadata" do
73
+ subject { described_class.new(id: "id") }
74
+ it "should update the metadata and save" do
73
75
  expect(subject).to receive(:update_metadata).with(metadata)
74
- subject.update(metadata)
75
- end
76
- it "should save the identifier" do
77
- expect(subject).to receive(:save)
76
+ expect(subject).to receive(:save) { double }
78
77
  subject.update(metadata)
79
78
  end
80
79
  end
81
80
 
82
81
  describe "#reload" do
83
- let(:id) { "ark:/99999/fk4fn19h88" }
84
- let(:metadata) { "_created: 1416507086" }
85
- subject { described_class.new(id: id) }
86
- before do
87
- allow(subject.client).to receive(:get_identifier_metadata).with(id) { double(metadata: metadata) }
88
- end
82
+ let(:metadata) { "_profile: erc" }
83
+ before { allow(subject).to receive(:id) { "id" } }
89
84
  it "should reinitialize the metadata from EZID" do
85
+ expect(subject.client).to receive(:get_identifier_metadata).with("id") { double(id: "id", metadata: metadata) }
90
86
  expect(Metadata).to receive(:new).with(metadata)
91
87
  subject.reload
92
88
  end
@@ -121,31 +117,23 @@ module Ezid
121
117
  end
122
118
 
123
119
  describe "#delete" do
124
- let(:id) { "ark:/99999/fk4zzzzzzz" }
125
- subject { described_class.new(id: id, status: "reserved") }
126
- before do
127
- allow_any_instance_of(Client).to receive(:delete_identifier).with(id) { double(id: id) }
128
- end
120
+ subject { described_class.new(id: "id", status: "reserved") }
129
121
  it "should delete the identifier" do
130
- expect(subject.client).to receive(:delete_identifier).with(id)
122
+ expect(subject.client).to receive(:delete_identifier).with("id") { double(id: "id") }
131
123
  subject.delete
132
124
  expect(subject).to be_deleted
133
125
  end
134
126
  end
135
127
 
136
128
  describe "#save" do
137
- let(:id) { "ark:/99999/fk4zzzzzzz" }
138
- before do
139
- allow(subject.client).to receive(:get_identifier_metadata).with(id) { double(metadata: {}) }
140
- end
129
+ before { allow(subject).to receive(:reload) { double } }
141
130
  context "when the identifier is persisted" do
142
131
  before do
143
- allow_any_instance_of(Client).to receive(:modify_identifier).with(id, {}) { double(id: id) }
144
- allow(subject).to receive(:id) { id }
132
+ allow(subject).to receive(:id) { "id" }
145
133
  allow(subject).to receive(:persisted?) { true }
146
134
  end
147
135
  it "should modify the identifier" do
148
- expect(subject.client).to receive(:modify_identifier).with(id, {})
136
+ expect(subject.client).to receive(:modify_identifier).with("id", {}) { double(id: "id") }
149
137
  subject.save
150
138
  end
151
139
  end
@@ -154,23 +142,17 @@ module Ezid
154
142
  allow(subject).to receive(:persisted?) { false }
155
143
  end
156
144
  context "and `id' is present" do
157
- before do
158
- allow(subject).to receive(:id) { id }
159
- allow_any_instance_of(Client).to receive(:create_identifier).with(id, {}) { double(id: id) }
160
- end
145
+ before { allow(subject).to receive(:id) { "id" } }
161
146
  it "should create the identifier" do
162
- expect(subject.client).to receive(:create_identifier).with(id, {})
147
+ expect(subject.client).to receive(:create_identifier).with("id", {}) { double(id: "id") }
163
148
  subject.save
164
149
  end
165
150
  end
166
151
  context "and `id' is not present" do
167
152
  context "and `shoulder' is present" do
168
- before do
169
- allow(subject).to receive(:shoulder) { ARK_SHOULDER }
170
- allow_any_instance_of(Client).to receive(:mint_identifier).with(ARK_SHOULDER, {}) { double(id: id) }
171
- end
153
+ before { allow(subject).to receive(:shoulder) { ARK_SHOULDER } }
172
154
  it "should mint the identifier" do
173
- expect(subject.client).to receive(:mint_identifier).with(ARK_SHOULDER, {})
155
+ expect(subject.client).to receive(:mint_identifier).with(ARK_SHOULDER, {}) { double(id: "id") }
174
156
  subject.save
175
157
  end
176
158
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ezid-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Chandek-Stark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-06 00:00:00.000000000 Z
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler