ezid 0.2.0 → 0.2.1

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