ruby-fedora 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -2,3 +2,10 @@
2
2
 
3
3
  * very alpha enhancement:
4
4
  * Initial release
5
+
6
+ == 0.1.1 2008-04-16
7
+ * Remove the gem dependency on active_resource
8
+
9
+ == 0.1.2 2008-04-30
10
+ * Rename FedoraRepository.fetch_conent to FedoraRepository.fetch_content
11
+ * Support custom mime-type for Datastream with parameter :mimeType
data/README.txt CHANGED
@@ -10,7 +10,7 @@ test_object = Fedora::FedoraObject.new(:label => 'test', :contentModel => 'Image
10
10
  repository.save(test_object)
11
11
 
12
12
  objects = repository.find_objects('label~Image*')
13
- object = repository.fetch_conent('demo:1')
13
+ object = repository.fetch_content('demo:1')
14
14
 
15
15
 
16
16
  Check out spec/repository_spec.rb for more examples.
data/lib/fedora/base.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'xmlsimple'
2
2
 
3
3
  class Hash
4
+ # {:q => 'test', :num => 5}.to_query # => 'q=test&num=5'
4
5
  def to_query
5
6
  self.collect { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.sort * '&'
6
7
  end
@@ -25,6 +26,10 @@ class Fedora::BaseObject
25
26
  @blob = attributes.delete(:blob)
26
27
  end
27
28
 
29
+ def [](key)
30
+ @attributes[key]
31
+ end
32
+
28
33
  def new_object?
29
34
  @new_object
30
35
  end
@@ -22,23 +22,24 @@ require 'net/https'
22
22
  # end
23
23
  module Fedora::Multipart
24
24
  def set_multipart_data(param_hash={})
25
+ mime_type = param_hash.delete(:content_type)
25
26
  boundary_token = [Array.new(8) {rand(256)}].join
26
27
  self.content_type = "multipart/form-data; boundary=#{boundary_token}"
27
28
  boundary_marker = "--#{boundary_token}\r\n"
28
29
  self.body = param_hash.map { |param_name, param_value|
29
30
  boundary_marker + case param_value
30
- when File then file_to_multipart(param_name, param_value)
31
- when String then text_to_multipart(param_name, param_value)
31
+ when File then file_to_multipart(param_name, param_value, mime_type)
32
+ when String then text_to_multipart(param_name, param_value, mime_type)
32
33
  else ""
33
34
  end
34
35
  }.join('') + "--#{boundary_token}--\r\n"
35
36
  end
36
37
 
37
38
  private
38
- def file_to_multipart(key,file)
39
+ def file_to_multipart(key,file, mime_type = nil)
39
40
  filename = File.basename(file.path)
40
41
  mime_types = MIME::Types.of(filename)
41
- mime_type = mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type
42
+ mime_type ||= mime_types.empty? ? "application/octet-stream" : mime_types.first.content_type
42
43
  part = %Q{Content-Disposition: form-data; name="#{key}"; filename="#{filename}"\r\n}
43
44
  part += "Content-Transfer-Encoding: binary\r\n"
44
45
  part += "Content-Type: #{mime_type}\r\n\r\n#{file.read}"
@@ -147,7 +148,7 @@ module RubyFedora
147
148
  # Used to update resources.
148
149
  def put(path, body = '', headers = {})
149
150
  if body.is_a?(File)
150
- multipart_request(Net::HTTP::Put.new(path, build_request_headers(headers)), body)
151
+ multipart_request(Net::HTTP::Put.new(path, build_request_headers(headers)), body, headers)
151
152
  else
152
153
  request(:put, path, body.to_s, build_request_headers(headers))
153
154
  end
@@ -157,7 +158,7 @@ module RubyFedora
157
158
  # Used to create new resources.
158
159
  def post(path, body = '', headers = {})
159
160
  if body.is_a?(File)
160
- multipart_request(Net::HTTP::Post.new(path, build_request_headers(headers)), body)
161
+ multipart_request(Net::HTTP::Post.new(path, build_request_headers(headers)), body, headers)
161
162
  else
162
163
  request(:post, path, body.to_s, build_request_headers(headers))
163
164
  end
@@ -168,10 +169,10 @@ module RubyFedora
168
169
  end
169
170
 
170
171
  private
171
- def multipart_request(req, file)
172
+ def multipart_request(req, file, headers = {})
172
173
  result = nil
173
174
  http.start do |conn|
174
- req.set_multipart_data(:file => file)
175
+ req.set_multipart_data(:file => file, :content_type => headers['Content-Type'])
175
176
  result = conn.request(req)
176
177
  end
177
178
  handle_response(result)
@@ -3,6 +3,7 @@ require 'fedora/base'
3
3
  class Fedora::Datastream < Fedora::BaseObject
4
4
  def initialize(attrs = nil)
5
5
  super
6
+ @attributes[:controlGroup] = 'M' if @attributes[:mimeType]
6
7
  # TODO: check for required attributes
7
8
  end
8
9
 
@@ -9,12 +9,21 @@ require 'fedora/datastream'
9
9
  module Fedora
10
10
  NAMESPACE = "fedora:info/"
11
11
  ALL_FIELDS = [
12
- :pid, :label, :fType, :cModel, :state, :ownerId, :cDate, :dcmDate,
12
+ :pid, :label, :fType, :cModel, :state, :ownerId, :cDate, :mDate, :dcmDate,
13
13
  :bMech, :title, :creator, :subject, :description, :contributor,
14
14
  :date, :type, :format, :identifier, :source, :language, :relation, :coverage, :rights
15
15
  ]
16
16
 
17
17
  class Repository
18
+ class StringResponse < String
19
+ attr_reader :content_type
20
+
21
+ def initialize(s, content_type)
22
+ super(s)
23
+ @content_type = content_type
24
+ end
25
+ end
26
+
18
27
  attr_accessor :fedora_url
19
28
 
20
29
  def initialize(fedora_url = "http://localhost:8080/fedora")
@@ -23,8 +32,9 @@ module Fedora
23
32
  end
24
33
 
25
34
  # Fetch the raw content of either a fedora object or datastream
26
- def fetch_conent(object_uri)
27
- connection.raw_get("#{url_for(object_uri)}?format=xml").body
35
+ def fetch_content(object_uri)
36
+ response = connection.raw_get("#{url_for(object_uri)}?format=xml")
37
+ StringResponse.new(response.body, response.content_type)
28
38
  end
29
39
 
30
40
  # Find fedora objects with http://www.fedora.info/wiki/index.php/API-A-Lite_findObjects
@@ -57,7 +67,7 @@ module Fedora
57
67
  params[:sessionToken] = options[:sessionToken] if options[:sessionToken]
58
68
  includes = fields.inject("") { |s, f| s += "&#{f}=true"; s }
59
69
 
60
- convert_xml(connection.get("#{fedora_url.path}/objects?#{to_query(params)}#{includes}"))
70
+ convert_xml(connection.get("#{fedora_url.path}/objects?#{params.to_query}#{includes}"))
61
71
  end
62
72
 
63
73
  # Create the given object if it's new (not obtained from a find method). Otherwise update the object.
@@ -83,8 +93,10 @@ module Fedora
83
93
  end
84
94
  when Fedora::Datastream
85
95
  raise ArgumentError, "Missing dsID attribute" if object.dsid.nil?
96
+ extra_headers = {}
97
+ extra_headers['Content-Type'] = object.attributes[:mimeType] if object.attributes[:mimeType]
86
98
  response = connection.post("#{url_for(object)}?" + object.attributes.to_query,
87
- object.blob)
99
+ object.blob, extra_headers)
88
100
  if response.code == '201'
89
101
  object.new_object = false
90
102
  true
@@ -147,7 +159,7 @@ module Fedora
147
159
  end
148
160
 
149
161
  extra_params.delete(:format) if method == :export
150
- connection.raw_get("#{url_for(object)}#{path}?#{to_query(extra_params)}").body
162
+ connection.raw_get("#{url_for(object)}#{path}?#{extra_params.to_query}").body
151
163
  end
152
164
 
153
165
  private
@@ -185,11 +197,6 @@ module Fedora
185
197
  def extract_pid(response)
186
198
  CGI.unescape(response['Location'].split('/').last)
187
199
  end
188
-
189
- # {:q => 'test', :num => 5}.to_query # => 'q=test&num=5'
190
- def to_query(hash)
191
- hash.collect { |key, value| "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}" }.sort * '&'
192
- end
193
200
  end
194
201
  end
195
202
 
data/lib/ruby-fedora.rb CHANGED
@@ -6,7 +6,7 @@ module RubyFedora #:nodoc:
6
6
  module VERSION #:nodoc:
7
7
  MAJOR = 0
8
8
  MINOR = 1
9
- TINY = 1
9
+ TINY = 2
10
10
 
11
11
  STRING = [MAJOR, MINOR, TINY].join('.')
12
12
  end
data/script/destroy CHANGED
File without changes
data/script/generate CHANGED
File without changes
data/script/txt2html CHANGED
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-fedora
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Zumwalt
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-16 00:00:00 -05:00
12
+ date: 2008-06-19 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  requirements: []
111
111
 
112
112
  rubyforge_project: rubyfedora
113
- rubygems_version: 1.0.1
113
+ rubygems_version: 1.1.1
114
114
  signing_key:
115
115
  specification_version: 2
116
116
  summary: Ruby API for Fedora