ezid 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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTk2NWU3ZjEzNzVhNzcxYzM0Y2NmNWJlZWE5Y2JmMTM3ZTc4M2NiNQ==
4
+ MWU1ZmY2ZjcyNzU5MjM5OTYzYjY0YzVhMDY1Yzg4NTQ5NDVmMTgwNQ==
5
5
  data.tar.gz: !binary |-
6
- ZGNiODNhNjZmMGM1MDQzODBmMzg0MDdiODY4ZjQ1MGQ0NDAyOTY2NQ==
6
+ ZmM4NTE0Mjk1MGQwZWQwNWExY2Q2MmFjNWFkNjYwMDIwNmUxNDYzNg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MThmNGQwMzY5NDRlYjBiMjllOWUyZDMzYWQyMzQ2N2U2ZTFlYWRiNjZiZmY3
10
- ZDY0OGJiOGZiYmU4YTdkZTY2ODk0MzBmYjhjZGZlMmZlNTBhYzZmNGYyN2M3
11
- YWI2ZTY1NDcwZjVkMjc0ODg4MGU4ZmMzMzQyY2EzODg0MmI4YjM=
9
+ NTA4ZGVhY2QzZGQ1NDFjYjY5ZjIzOGJmOWJmYWYzOTc2ZGRhZTQwOGEyYWMx
10
+ OTgzNzQ1OTkwZjI3NGUyYTkxOWQ4NzYxYTkzY2EwYjZlODZkZGU1Yzc2MDEz
11
+ ZmQwNGRhNDY3ODgzZmZhNmM5Y2EwOTA4ZDZlMDljNDc1NWJkNTY=
12
12
  data.tar.gz: !binary |-
13
- YzYyMjg5NTI5YWI3YjllYWZhNzc0NjQyYjM2NDExZGY3NGFhM2E0OGVmNmMx
14
- MDA4YTkwYzIyMjc4NDY3MjRjMjRmYjgyN2Q1OGY3YjA4ZjUxNzU3Nzg5ZDZj
15
- MjFhZGI5NDFmNWE0NGQwMTUxNzgxNDJjZmRlYTg1YWZkM2E4ZTg=
13
+ NmQ0ZjU2OGYwMmZiMmViYWYwMWQxZDMzZjFkMzgwNTQzOGRmOTBiN2FjODYy
14
+ OGYzMGEzNTg3ODdmZTY0ZTJiMThlZjI1NGY0YzFkNzI1YTYxMDUxNTI3OTMw
15
+ NzNlNzdiMWYwMmRiZTJkOTllZTFiMjNhNDM4M2VjNzc5ODFiNmM=
@@ -0,0 +1,168 @@
1
+ require 'net/http'
2
+ require 'ezid/record'
3
+ require 'ezid/server_response'
4
+ module Ezid
5
+ class ApiSession
6
+
7
+ VERSION = '0.2.0'
8
+ APIVERSION = 'EZID API, Version 2'
9
+
10
+ SECURESERVER = "https://n2t.net/ezid"
11
+ TESTUSERNAME = 'apitest'
12
+ TESTPASSWORD = 'apitest'
13
+ SCHEMES = {:ark => 'ark:/', :doi => "doi:"}
14
+
15
+ PRIVATE = "reserved"
16
+ PUBLIC = "public"
17
+ UNAVAIL = "unavailable"
18
+
19
+ TESTSHOULDER = {SCHEMES[:ark] => '99999/fk4', SCHEMES[:doi] => '10.5072/FK2'}
20
+ TESTMETADATA = {'_target' => 'http://example.org/opensociety', 'erc.who' => 'Karl Popper', 'erc.what' => 'The Open Society and Its Enemies', 'erc.when' => '1945'}
21
+
22
+ def initialize(username=TESTUSERNAME, password=TESTPASSWORD, scheme=:ark, naa='')
23
+ if username == TESTUSERNAME
24
+ password = TESTPASSWORD
25
+ @test = true
26
+ else
27
+ @test = false
28
+ end
29
+
30
+ @user = username
31
+ @pass = password
32
+ @scheme = SCHEMES[scheme]
33
+ @naa = naa
34
+
35
+ if @test == true
36
+ @naa = TESTSHOULDER[@scheme]
37
+ end
38
+ end
39
+
40
+ def mint(metadata={})
41
+ shoulder = @scheme + @naa
42
+ metadata['_status'] = PRIVATE
43
+ #TODO: send mint request to API
44
+ request_uri = "/shoulder/" + shoulder
45
+ request = call_api(request_uri, :post, metadata)
46
+ if(request.errored?)
47
+ return request
48
+ else
49
+ return get(request)
50
+ end
51
+ end
52
+
53
+ def create(identifier, metadata={})
54
+ metadata = transform_metadata(metadata)
55
+ if not (identifier.start_with?(SCHEMES[:ark]) or identifier.start_with?(SCHEMES[:doi]))
56
+ identifier = @scheme + @naa + identifier
57
+ end
58
+ request_uri = "/id/" + identifier
59
+ request = call_api(request_uri, :put, metadata)
60
+ request.errored? ? request : get(request)
61
+ end
62
+ def transform_metadata(metadata)
63
+ if !metadata['_status']
64
+ metadata['_status'] = PRIVATE
65
+ end
66
+ return metadata
67
+ end
68
+ def build_identifier(identifier)
69
+ "#{@scheme}#{@naa}#{identifier}"
70
+ end
71
+ def get(identifier)
72
+ request_uri = "/id/" + identifier
73
+ request = call_api(request_uri, :get)
74
+ if(request.errored?)
75
+ return request
76
+ else
77
+ return Ezid::Record.new(self,request.response["identifier"],request.response["metadata"],true)
78
+ end
79
+ end
80
+
81
+ def delete(identifier)
82
+ request_uri = "/id/" + identifier
83
+ call_api(request_uri, :delete)
84
+ end
85
+
86
+ # public utility methods
87
+
88
+ def record_modify(identifier, metadata, clear=false)
89
+ if clear
90
+ #TODO: clear old metadata
91
+ end
92
+ metadata.each do |name, value|
93
+ modify(identifier, name, value)
94
+ end
95
+ get(identifier)
96
+ end
97
+
98
+ def scheme=(scheme)
99
+ @scheme = SCHEMES[scheme]
100
+ if @test == true
101
+ @naa = TESTSHOULDER[@scheme]
102
+ end
103
+ end
104
+
105
+ def naa=(naa)
106
+ @naa = naa
107
+ end
108
+
109
+ private
110
+
111
+ def call_api(request_uri, request_method, request_data=nil)
112
+ uri = URI(SECURESERVER + request_uri)
113
+
114
+ # which HTTP method to use?
115
+ if request_method == :get
116
+ request = Net::HTTP::Get.new uri.request_uri
117
+ elsif request_method == :put
118
+ request = Net::HTTP::Put.new uri.request_uri
119
+ request.body = make_anvl(request_data)
120
+ elsif request_method == :post
121
+ request = Net::HTTP::Post.new uri.request_uri
122
+ request.body = make_anvl(request_data)
123
+ elsif request_method == :delete
124
+ request = Net::HTTP::Delete.new uri.request_uri
125
+ end
126
+
127
+ request.basic_auth @user, @pass
128
+ request.add_field("Content-Type", "text/plain; charset=UTF-8")
129
+
130
+ # Make the call
131
+ result = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
132
+ parse_record http.request(request).body
133
+ end
134
+ return Ezid::ServerResponse.new(result)
135
+ end
136
+
137
+ def parse_record(ezid_response)
138
+ parts = ezid_response.split("\n")
139
+ identifier = parts[0].split(": ")[1]
140
+ metadata = {}
141
+ if parts.length > 1
142
+ parts[1..-1].each do |p|
143
+ pair = p.split(": ")
144
+ metadata[pair[0]] = pair[1]
145
+ end
146
+ record = {'identifier' => identifier, 'metadata' => metadata}
147
+ else
148
+ record = identifier
149
+ end
150
+ record
151
+ end
152
+
153
+ def make_anvl(metadata)
154
+ #TODO: define escape method for anvl
155
+ def escape(s)
156
+ URI.escape(s, /[%:\n\r]/)
157
+ end
158
+ anvl = ''
159
+ metadata.each do |n, v|
160
+ anvl += escape(n.to_s) + ': ' + escape(v.to_s) + "\n"
161
+ end
162
+ #remove last newline. there is probably a really good way avoid adding it in the first place. if you know it, please fix.
163
+ anvl.strip().encode!("UTF-8")
164
+ end
165
+
166
+ end
167
+
168
+ end
@@ -0,0 +1,81 @@
1
+ module Ezid
2
+ class Record
3
+ attr_reader :identifier, :metadata
4
+ def initialize(session, identifier, metadata={},persisted=false)
5
+ @identifier = identifier
6
+ @metadata = metadata
7
+ @session = session
8
+ @changed = []
9
+ @stale = false
10
+ @persisted = persisted
11
+ self
12
+ end
13
+ # There has to be a better way to do this.
14
+ def reload
15
+ if(self.stale?)
16
+ newRecord = @session.get(@identifier)
17
+ @identifier = newRecord.identifier
18
+ @metadata = newRecord.metadata
19
+ @stale = false
20
+ end
21
+ return self
22
+ end
23
+ def persisted?
24
+ @changed.length != 0 ? false : @persisted
25
+ end
26
+ def [](attribute)
27
+ @metadata[attribute]
28
+ end
29
+ def []=(attribute,value)
30
+ if(@metadata[attribute] != value)
31
+ @changed << attribute
32
+ @changed.uniq!
33
+ end
34
+ @metadata[attribute] = value
35
+ end
36
+ def delete
37
+ @session.delete(identifier)
38
+ end
39
+ # Returns true if the record has been saved but not reloaded.
40
+ def stale?
41
+ return @stale
42
+ end
43
+ def save
44
+ return self if self.persisted?
45
+ modifyData = @changed.each_with_object({}){|key, hash| hash[key] = @metadata[key]}
46
+ request_uri = "/id/#{@identifier}"
47
+ result = @session.send(:call_api,request_uri, :post, modifyData)
48
+ if(result.errored?)
49
+ raise "Unable to save - error: #{result.response}"
50
+ end
51
+ @persisted = true
52
+ @stale = true
53
+ @changed = []
54
+ return self
55
+ end
56
+ # Utility Methods
57
+ def make_public
58
+ self["_status"] = Ezid::ApiSession::PUBLIC
59
+ end
60
+
61
+ def make_unavailable
62
+ self["_status"] = Ezid::ApiSession::UNAVAIL
63
+ end
64
+ # Shortcut methods - leave behind from old implementation.
65
+ def status
66
+ self["_status"]
67
+ end
68
+ def target
69
+ self["_target"]
70
+ end
71
+ def target=(value)
72
+ self["_target"] = value
73
+ end
74
+ def profile
75
+ self["_profile"]
76
+ end
77
+ def profile=(value)
78
+ self["_profile"] = value
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,14 @@
1
+ module Ezid
2
+ class ServerResponse
3
+ attr_reader :response
4
+ def initialize(response)
5
+ @response = response
6
+ end
7
+ def errored?
8
+ return @response.instance_of?(String) && @response.start_with?("bad request")
9
+ end
10
+ def to_str
11
+ @response.to_s
12
+ end
13
+ end
14
+ end
data/lib/ezid.rb CHANGED
@@ -1,178 +1 @@
1
- require 'net/http'
2
-
3
- module Ezid
4
-
5
- class ApiSession
6
-
7
-
8
- VERSION = '0.1.0'
9
- APIVERSION = 'EZID API, Version 2'
10
-
11
- SECURESERVER = "https://n2t.net/ezid"
12
- TESTUSERNAME = 'apitest'
13
- TESTPASSWORD = 'apitest'
14
- SCHEMES = {:ark => 'ark:/', :doi => "doi:"}
15
-
16
- PRIVATE = "reserved"
17
- PUBLIC = "public"
18
- UNAVAIL = "unavailable"
19
- TESTSHOULDER = {SCHEMES[:ark] => '99999/fk4', SCHEMES[:doi] => '10.5072/FK2'}
20
- TESTMETADATA = {'_target' => 'http://example.org/opensociety', 'erc.who' => 'Karl Popper', 'erc.what' => 'The Open Society and Its Enemies', 'erc.when' => '1945'}
21
-
22
- def initialize(username=TESTUSERNAME, password=TESTPASSWORD, scheme=:ark, naa='')
23
- if username == TESTUSERNAME
24
- password = TESTPASSWORD
25
- @test = true
26
- else
27
- @test = false
28
- end
29
-
30
- @user = username
31
- @pass = password
32
- @scheme = SCHEMES[scheme]
33
- @naa = naa
34
-
35
- if @test == true
36
- @naa = TESTSHOULDER[@scheme]
37
- end
38
- end
39
-
40
- def mint(metadata={})
41
- shoulder = @scheme + @naa
42
- metadata['_status'] = PRIVATE
43
- #TODO: send mint request to API
44
- request_uri = "/shoulder/" + shoulder
45
- call_api(request_uri, :post, metadata)
46
- end
47
-
48
- def create(identifier, metadata={})
49
- if not metadata['_status']
50
- metadata['_status'] = PRIVATE
51
- end
52
- if not (identifier.start_with?(SCHEMES[:ark]) or identifier.start_with?(SCHEMES[:doi]))
53
- identifier = @scheme + @naa + identifier
54
- end
55
- request_uri = "/id/" + identifier
56
- call_api(request_uri, :put, metadata)
57
- end
58
-
59
- def modify(identifier, name, value)
60
- request_uri = "/id/" + identifier
61
- call_api(request_uri, :post, {name => value})
62
- end
63
-
64
- def get(identifier)
65
- request_uri = "/id/" + identifier
66
- call_api(request_uri, :get)
67
- end
68
-
69
- def delete(identifier)
70
- request_uri = "/id/" + identifier
71
- call_api(request_uri, :delete)
72
- end
73
-
74
- # public utility methods
75
- def change_profile(identifier, profile)
76
- modify(identifier, '_profile', profile)
77
- end
78
-
79
- def get_status(identifier)
80
- get(identifier)['metadata']['_status']
81
- end
82
-
83
- def make_public(identifier)
84
- modify(identifier, '_status', PUBLIC)
85
- end
86
-
87
- def make_unavailable(identifier)
88
- modify(identifier, '_status', UNAVAIL)
89
- end
90
-
91
- def get_target(identifier)
92
- get(identifier)['metadata']['_target']
93
- end
94
-
95
- def modify_target(identifier, target)
96
- modify(identifier, '_target', target)
97
- end
98
-
99
- def record_modify(identifier, metadata, clear=false)
100
- if clear
101
- #TODO: clear old metadata
102
- end
103
- metadata.each do |name, value|
104
- modify(identifier, name, value)
105
- end
106
- get(identifier)
107
- end
108
-
109
- def set_scheme(scheme)
110
- @scheme = SCHEMES[scheme]
111
- if @test == true
112
- @naa = TESTSHOULDER[@scheme]
113
- end
114
- end
115
-
116
- def set_naa(naa)
117
- @naa = naa
118
- end
119
-
120
- private
121
-
122
- def call_api(request_uri, request_method, request_data=nil)
123
- uri = URI(SECURESERVER + request_uri)
124
-
125
- # which HTTP method to use?
126
- if request_method == :get
127
- request = Net::HTTP::Get.new uri.request_uri
128
- elsif request_method == :put
129
- request = Net::HTTP::Put.new uri.request_uri
130
- request.body = make_anvl(request_data)
131
- elsif request_method == :post
132
- request = Net::HTTP::Post.new uri.request_uri
133
- request.body = make_anvl(request_data)
134
- elsif request_method == :delete
135
- request = Net::HTTP::Delete.new uri.request_uri
136
- end
137
-
138
- request.basic_auth @user, @pass
139
- request.add_field("Content-Type", "text/plain; charset=UTF-8")
140
-
141
- # Make the call
142
- Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
143
- parse_record http.request(request).body
144
- end
145
- end
146
-
147
- def parse_record(ezid_response)
148
- parts = ezid_response.split("\n")
149
- identifier = parts[0].split(": ")[1]
150
- metadata = {}
151
- if parts.length > 1
152
- parts[1..-1].each do |p|
153
- pair = p.split(": ")
154
- metadata[pair[0]] = pair[1]
155
- end
156
- record = {'identifier' => identifier, 'metadata' => metadata}
157
- else
158
- record = identifier
159
- end
160
- record
161
- end
162
-
163
- def make_anvl(metadata)
164
- #TODO: define escape method for anvl
165
- def escape(s)
166
- URI.escape(s, /[%:\n\r]/)
167
- end
168
- anvl = ''
169
- metadata.each do |n, v|
170
- anvl += escape(n) + ': ' + escape(v) + "\n"
171
- end
172
- #remove last newline. there is probably a really good way avoid adding it in the first place. if you know it, please fix.
173
- anvl.strip().encode!("UTF-8")
174
- end
175
-
176
- end
177
-
178
- end
1
+ require 'ezid/apisession'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ezid
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
  - Thomas Johnson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-02-25 00:00:00.000000000 Z
11
+ date: 2013-03-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: API client for California Digital Library's EZID service.
14
14
  email: thomas.johnson@oregonstate.edu
@@ -17,6 +17,9 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/ezid.rb
20
+ - lib/ezid/apisession.rb
21
+ - lib/ezid/record.rb
22
+ - lib/ezid/server_response.rb
20
23
  homepage: http://achelo.us/code/ezid_api
21
24
  licenses: []
22
25
  metadata: {}
@@ -41,4 +44,3 @@ signing_key:
41
44
  specification_version: 4
42
45
  summary: API client for California Digital Library's EZID service.
43
46
  test_files: []
44
- has_rdoc: