ezid-client 0.1.1 → 0.2.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: 7c3a0edbc74bbd749bf0c099fa07cad69bfebcdb
4
- data.tar.gz: 4fdea290531b0f68d4d652a17b800585a4514f34
3
+ metadata.gz: 7525ff60fc6c37fafa16197480ff8612fb0d7cd0
4
+ data.tar.gz: 2d7a986b67c23cf09722b4ccfb27d70913a2ef5e
5
5
  SHA512:
6
- metadata.gz: 8eecfcf10284ab9c6ccbe0916547d704d75ee3b0e9b8d6da0ff4dcadbe35a3d698400f741483c5529f6ce1809caee462b812d76356231ac06732d3ebbac49f7f
7
- data.tar.gz: 0c137cf93d57cff8186821ace6125ca390833ed8a07327d1cffb50e708a8dae374594e374fe3f2cd603e725295b8e0c367645cb3639c568ca230e48814f3b01d
6
+ metadata.gz: a681531c12267fd6b849d59ce37a533cd2e9e00c8f8f43deb54e646fff4772f881ba7e84e1d118545bcec90e0531f87647bf35c51c0c6b1d06a978b39ef0aa66
7
+ data.tar.gz: 87b9bc72d63e31e934ec4849b79f2307ac73ad34cabbcf21faf4a10c69f3f23e0e65ec58e8e080cfb37019f270fdd0eeb6404c636b48431944ee18535c8a7288
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/lib/ezid/client.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require_relative "api"
2
- require_relative "identifier"
3
2
  require_relative "request"
4
3
  require_relative "response"
5
4
  require_relative "metadata"
data/lib/ezid/metadata.rb CHANGED
@@ -12,7 +12,7 @@ module Ezid
12
12
  # The metadata elements hash
13
13
  attr_reader :elements
14
14
 
15
- def_delegators :elements, :each, :empty?, :[], :[]=
15
+ def_delegators :elements, :each, :keys, :values, :empty?, :[], :[]=
16
16
 
17
17
  # EZID metadata profiles
18
18
  PROFILES = %w( erc dc datacite crossref )
@@ -45,8 +45,10 @@ module Ezid
45
45
  # EZID metadata field/value separator
46
46
  ANVL_SEPARATOR = ": "
47
47
 
48
- # Characters to escape on output to EZID
49
- ESCAPE_RE = /[%:\r\n]/
48
+ # Characters to escape in element values on output to EZID
49
+ ESCAPE_VALUES_RE = /[%\r\n]/
50
+
51
+ ESCAPE_KEYS_RE = /[%:\r\n]/
50
52
 
51
53
  # Character sequence to unescape from EZID
52
54
  UNESCAPE_RE = /%\h\h/
@@ -68,7 +70,7 @@ module Ezid
68
70
  # @see http://ezid.cdlib.org/doc/apidoc.html#request-response-bodies
69
71
  # @return [String] the ANVL output
70
72
  def to_anvl
71
- lines = map { |element| element.map { |e| escape(e) }.join(ANVL_SEPARATOR) }
73
+ lines = escape_keys.zip(escape_values).map { |e| e.join(ANVL_SEPARATOR) }
72
74
  lines.join("\n").force_encoding(Encoding::UTF_8)
73
75
  end
74
76
 
@@ -120,12 +122,21 @@ module Ezid
120
122
  hsh.keys.map(&:to_s).zip(hsh.values).to_h
121
123
  end
122
124
 
125
+ def escape_keys
126
+ keys.map { |k| escape(ESCAPE_KEYS_RE, k) }
127
+ end
128
+
129
+ def escape_values
130
+ values.map { |v| escape(ESCAPE_VALUES_RE, v) }
131
+ end
132
+
123
133
  # Escape value for sending to EZID host
124
134
  # @see http://ezid.cdlib.org/doc/apidoc.html#request-response-bodies
135
+ # @param re [Regexp] the regular expression to match for escaping
125
136
  # @param value [String] the value to escape
126
137
  # @return [String] the escaped value
127
- def escape(value)
128
- value.gsub(ESCAPE_RE) { |m| URI.encode(m) }
138
+ def escape(re, value)
139
+ value.gsub(re) { |m| URI.encode_www_form_component(m) }
129
140
  end
130
141
 
131
142
  # Unescape value from EZID host (or other source)
@@ -133,7 +144,7 @@ module Ezid
133
144
  # @param value [String] the value to unescape
134
145
  # @return [String] the unescaped value
135
146
  def unescape(value)
136
- value.gsub(UNESCAPE_RE) { |m| URI.decode(m) }
147
+ value.gsub(UNESCAPE_RE) { |m| URI.decode_www_form_component(m) }
137
148
  end
138
149
 
139
150
  # Coerce a string of metadata (e.g., from EZID host) into a Hash
@@ -1,19 +1,26 @@
1
1
  module Ezid
2
2
  RSpec.describe Metadata do
3
+
3
4
  describe "method missing" do
4
- context "for an internal metadata element name w/o leading underscore" do
5
+ context "for an internal element name w/o leading underscore" do
5
6
  it "should call the reader method" do
6
7
  expect(subject).to receive(:reader).with("_status")
7
8
  subject.status
8
9
  end
9
10
  end
10
- context "for an internal writable metadata element name + '=' and w/o leading underscore" do
11
+ context "for an internal writable element name + '=' and w/o leading underscore" do
11
12
  it "should call the writer method" do
12
13
  expect(subject).to receive(:writer).with("_status", "public")
13
14
  subject.status = "public"
14
15
  end
15
16
  end
17
+ context "for an internal readonly element name + '=' and w/o leading underscore" do
18
+ it "should not call the writer method" do
19
+ expect { subject.created = "1416507086" }.to raise_error
20
+ end
21
+ end
16
22
  end
23
+
17
24
  describe "internal element reader" do
18
25
  context "for a datetime element" do
19
26
  before { subject["_created"] = "1416507086" }
@@ -28,12 +35,14 @@ module Ezid
28
35
  end
29
36
  end
30
37
  end
38
+
31
39
  describe "internal element writer" do
32
40
  before { subject["_status"] = "reserved" }
33
41
  it "should set the element" do
34
42
  expect { subject.status = "public" }.to change { subject["_status"] }.from("reserved").to("public")
35
43
  end
36
44
  end
45
+
37
46
  describe "ANVL output" do
38
47
  let(:metadata) { described_class.new(_updated: "1416507086",
39
48
  _target: "http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87",
@@ -54,9 +63,51 @@ _export: yes
54
63
  _created: 1416507086
55
64
  _status: public")
56
65
  end
66
+ describe "escaping" do
67
+ context "of element names" do
68
+ it "should escape a colon"
69
+ it "should escape a line feed"
70
+ it "should escape a carriage return"
71
+ it "should escape a percent sign"
72
+ end
73
+ context "of element values" do
74
+ it "should escape a line feed"
75
+ it "should escape a carriage return"
76
+ it "should escape a percent sign"
77
+ end
78
+ end
57
79
  end
58
- describe "coercion" do
59
80
 
81
+ describe "coercion" do
82
+ context "of a string" do
83
+ let(:data) { "\
84
+ _updated: 1416507086
85
+ _target: http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87
86
+ _profile: erc
87
+ _ownergroup: apitest
88
+ _owner: apitest
89
+ _export: yes
90
+ _created: 1416507086
91
+ _status: public" }
92
+ subject { described_class.new(data) }
93
+ it "should coerce the data into a hash" do
94
+ expect(subject.elements).to eq({"_updated" => "1416507086",
95
+ "_target" => "http://ezid.cdlib.org/id/ark:/99999/fk4fn19h87",
96
+ "_profile" => "erc",
97
+ "_ownergroup" => "apitest",
98
+ "_owner" => "apitest",
99
+ "_export" => "yes",
100
+ "_created" => "1416507086",
101
+ "_status" => "public"})
102
+ end
103
+ end
104
+ context "of a hash" do
105
+ it "should stringify the keys"
106
+ end
107
+ context "of an Ezid::Metadata instance" do
108
+ it "should return the elements hash"
109
+ end
60
110
  end
111
+
61
112
  end
62
113
  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.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dchandekstark
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2014-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,7 +100,6 @@ files:
100
100
  - lib/ezid/client.rb
101
101
  - lib/ezid/configuration.rb
102
102
  - lib/ezid/error.rb
103
- - lib/ezid/identifier.rb
104
103
  - lib/ezid/logger.rb
105
104
  - lib/ezid/metadata.rb
106
105
  - lib/ezid/request.rb
@@ -108,7 +107,6 @@ files:
108
107
  - lib/ezid/session.rb
109
108
  - lib/ezid/test_helper.rb
110
109
  - spec/lib/ezid/client_spec.rb
111
- - spec/lib/ezid/identifier_spec.rb
112
110
  - spec/lib/ezid/metadata_spec.rb
113
111
  - spec/spec_helper.rb
114
112
  homepage: https://github.com/duke-libraries/ezid-client
@@ -137,6 +135,5 @@ specification_version: 4
137
135
  summary: Ruby client for EZID API Version 2
138
136
  test_files:
139
137
  - spec/lib/ezid/client_spec.rb
140
- - spec/lib/ezid/identifier_spec.rb
141
138
  - spec/lib/ezid/metadata_spec.rb
142
139
  - spec/spec_helper.rb
@@ -1,129 +0,0 @@
1
- require "forwardable"
2
-
3
- require_relative "metadata"
4
-
5
- module Ezid
6
- #
7
- # An EZID identifier resource.
8
- #
9
- # Identifier objects are instantiated by the class methods `create', `mint' and `find'.
10
- #
11
- # @api public
12
- class Identifier
13
- extend Forwardable
14
-
15
- attr_reader :id
16
-
17
- def_delegators :metadata, :status
18
-
19
- private_class_method :new
20
-
21
- class << self
22
- # Creates and EZID identifier
23
- def create(id, metadata=nil)
24
- response = Client.create_identifier(id, metadata)
25
- new(response.identifier)
26
- end
27
-
28
- # Mints an EZID identifier
29
- def mint(shoulder=nil, metadata=nil)
30
- response = Client.mint_identifier(metadata)
31
- identifier = new(response.identifier)
32
- end
33
-
34
- # Find an EZID indentifier
35
- def find(id)
36
- response = Client.get_identifier_metadata(id)
37
- new(response.identifier, response.metadata)
38
- end
39
- end
40
-
41
- def initialize(id, metadata=nil)
42
- @id = id
43
- @metadata = Metadata.new(metadata)
44
- end
45
-
46
- # The identifier metadata, cached locally
47
- # @return [Ezid::Metadata] the metadata
48
- def metadata
49
- reload if @metadata.empty?
50
- @metadata
51
- end
52
-
53
- # The identifier which this identifier is shadowed by, or nil.
54
- # @return [Ezid::Identifier] the shadowing identifier
55
- def shadowed_by
56
- Identifer.new(metadata.shadowedby) if metadata.shadowedby
57
- end
58
-
59
- # The identifer which this identifier shadows, or nil.
60
- # @return [Ezid::Identifier] the shadowed identifier
61
- def shadows
62
- Identifier.new(metadata.shadows) if metadata.shadows
63
- end
64
-
65
- # Retrieve the current metadata for the identifier from EZID
66
- # @return [Ezid::Identifier] the identifier
67
- def reload
68
- response = client.get_identifier_metadata(id)
69
- @metadata = response.metadata
70
- self
71
- end
72
-
73
- # Clears the metadata on the identifier object
74
- # @return [Ezid::Identifier] the identifier
75
- def reset
76
- @metadata = Metadata.new
77
- self
78
- end
79
-
80
- # Returns an EZID client
81
- # @return [Ezid::Client] the client
82
- def client
83
- @client ||= Client.new
84
- end
85
-
86
- # Deletes the identifier - caution!
87
- def delete
88
- response = client.delete_identifier(id)
89
- reset
90
- freeze
91
- response.message
92
- end
93
-
94
- # Sends the metadata to EZID - caution!
95
- def update(metadata)
96
- response = client.modify_identifier_metadata(metadata)
97
- reset
98
- response.message
99
- end
100
-
101
- def make_public!
102
- update(_status: Metadata::PUBLIC)
103
- end
104
-
105
- def make_unavailable!
106
- update(_status: Metadata::UNAVAILABLE)
107
- end
108
-
109
- def public?
110
- status == Metadata::PUBLIC
111
- end
112
-
113
- def reserved?
114
- status == Metadata::RESERVED
115
- end
116
-
117
- def unavailable?
118
- status == Metadata::UNAVAILABLE
119
- end
120
-
121
- private
122
-
123
- def remove(*elements)
124
- metadata = elements.map { |el| [el, ""] }.to_h
125
- update(metadata)
126
- end
127
-
128
- end
129
- end
@@ -1,5 +0,0 @@
1
- module Ezid
2
- RSpec.describe Identifier do
3
-
4
- end
5
- end