ezid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/ezid.rb +175 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDRiY2EzZmQ5ZjU5OTEwNTc2ZGUwZGY0YjNkNDI4ZGRiYjBiYTAyYg==
5
+ data.tar.gz: !binary |-
6
+ OTgxZjQ0Y2Y0MWRhMDllZWZjY2YwOTliZjAxMzQxZDEzMTcwZTMzMw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MzYxYWE2MjlmZWQwODRmZGRkNjUzNWM2NDgxYjcyMzBiZjY0YWQwMjk2YjNm
10
+ NDAxODhkMDBlNjkxODBhNjU3MGI1ZTAxNThlMzMwZWRmNDM3OTU0OTIxNDk0
11
+ M2I4NmJiM2VjZTY0YTQxOWZkMjk4ZGJjYjM3OTA0NTIyNDgyYmI=
12
+ data.tar.gz: !binary |-
13
+ MDUyMTI1YWM4MGQ3YTU2NzkxY2RmZmE0MTg2NzI5MmYxNjI2NWI3NDVkYzdi
14
+ MGY4OTAwNzgzZjcwOTIxY2EyNDFmY2Y3NDE5MzQyMWQ5YjFlNTQxMWY5ZmI5
15
+ YjExOGJjNzRmOTRhYjExZmZiNTk0NGEyZWUzYzc1MzgyMjk4YzQ=
data/lib/ezid.rb ADDED
@@ -0,0 +1,175 @@
1
+ require 'net/http'
2
+
3
+ module Ezid
4
+
5
+ class ApiSession
6
+
7
+ VERSION = '0.1.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
+ TESTSHOULDER = {SCHEMES[:ark] => '99999/fk4', SCHEMES[:doi] => '10.5072/FK2'}
19
+ TESTMETADATA = {'_target' => 'http://example.org/opensociety', 'erc.who' => 'Karl Poppe\
20
+ r', '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 = scheme
111
+ end
112
+
113
+ def set_naa(naa)
114
+ @naa = naa
115
+ end
116
+
117
+ private
118
+
119
+ def call_api(request_uri, request_method, request_data=nil)
120
+ uri = URI(SECURESERVER + request_uri)
121
+
122
+ # which HTTP method to use?
123
+ if request_method == :get
124
+ request = Net::HTTP::Get.new uri.request_uri
125
+ elsif request_method == :put
126
+ request = Net::HTTP::Put.new uri.request_uri
127
+ request.body = make_anvl(request_data)
128
+ elsif request_method == :post
129
+ request = Net::HTTP::Post.new uri.request_uri
130
+ request.body = make_anvl(request_data)
131
+ elsif request_method == :delete
132
+ request = Net::HTTP::Delete.new uri.request_uri
133
+ end
134
+
135
+ request.basic_auth @user, @pass
136
+ request.add_field("Content-Type", "text/plain; charset=UTF-8")
137
+
138
+ # Make the call
139
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
140
+ parse_record http.request(request).body
141
+ end
142
+ end
143
+
144
+ def parse_record(ezid_response)
145
+ parts = ezid_response.split("\n")
146
+ identifier = parts[0].split(": ")[1]
147
+ metadata = {}
148
+ if parts.length > 1
149
+ parts[1..-1].each do |p|
150
+ pair = p.split(": ")
151
+ metadata[pair[0]] = pair[1]
152
+ end
153
+ record = {'identifier' => identifier, 'metadata' => metadata}
154
+ else
155
+ record = identifier
156
+ end
157
+ record
158
+ end
159
+
160
+ def make_anvl(metadata)
161
+ #TODO: define escape method for anvl
162
+ def escape(s)
163
+ URI.escape(s, /[%:\n\r]/)
164
+ end
165
+ anvl = ''
166
+ metadata.each do |n, v|
167
+ anvl += escape(n) + ': ' + escape(v) + "\n"
168
+ end
169
+ #remove last newline. there is probably a really good way avoid adding it in the first place. if you know it, please fix.
170
+ anvl.strip().encode!("UTF-8")
171
+ end
172
+
173
+ end
174
+
175
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ezid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Johnson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2012-09-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: API client for California Digital Library's EZID service.
14
+ email: thomas.johnson@oregonstate.edu
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/ezid.rb
20
+ homepage: http://achelo.us/code/ezid_api
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ! '>='
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubyforge_project:
39
+ rubygems_version: 2.0.0
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: API client for California Digital Library's EZID service.
43
+ test_files: []
44
+ has_rdoc: