Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial commit

  • Loading branch information...
commit f4e79618fde2268ba7ccb3cf6aa278eeb466b62b 0 parents
Chris Beer authored
45 .gitignore
... ... @@ -0,0 +1,45 @@
  1 +# rcov generated
  2 +coverage
  3 +
  4 +# rdoc generated
  5 +rdoc
  6 +
  7 +# yard generated
  8 +doc
  9 +.yardoc
  10 +
  11 +# bundler
  12 +.bundle
  13 +
  14 +# jeweler generated
  15 +pkg
  16 +
  17 +Gemfile.lock
  18 +.rvmrc
  19 +
  20 +# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
  21 +#
  22 +# * Create a file at ~/.gitignore
  23 +# * Include files you want ignored
  24 +# * Run: git config --global core.excludesfile ~/.gitignore
  25 +#
  26 +# After doing this, these files will be ignored in all your git projects,
  27 +# saving you from having to 'pollute' every project you touch with them
  28 +#
  29 +# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
  30 +#
  31 +# For MacOS:
  32 +#
  33 +#.DS_Store
  34 +#
  35 +# For TextMate
  36 +#*.tmproj
  37 +#tmtags
  38 +#
  39 +# For emacs:
  40 +#*~
  41 +#\#*
  42 +#.\#*
  43 +#
  44 +# For vim:
  45 +*.swp
19 Gemfile
... ... @@ -0,0 +1,19 @@
  1 +source "http://rubygems.org"
  2 +
  3 +gem "fastercsv"
  4 +gem "rest-client"
  5 +gem "nokogiri"
  6 +
  7 +# Add dependencies required to use your gem here.
  8 +# Example:
  9 +# gem "activesupport", ">= 2.3.5"
  10 +
  11 +# Add dependencies to develop your gem here.
  12 +# Include everything needed to run rake, tests, features, etc.
  13 +group :development do
  14 + gem "shoulda", ">= 0"
  15 + gem "bundler", "~> 1.0.0"
  16 + gem "jeweler", "~> 1.5.1"
  17 + gem "rcov", ">= 0"
  18 + gem "rspec"
  19 +end
20 LICENSE.txt
... ... @@ -0,0 +1,20 @@
  1 +Copyright (c) 2010 Chris Beer
  2 +
  3 +Permission is hereby granted, free of charge, to any person obtaining
  4 +a copy of this software and associated documentation files (the
  5 +"Software"), to deal in the Software without restriction, including
  6 +without limitation the rights to use, copy, modify, merge, publish,
  7 +distribute, sublicense, and/or sell copies of the Software, and to
  8 +permit persons to whom the Software is furnished to do so, subject to
  9 +the following conditions:
  10 +
  11 +The above copyright notice and this permission notice shall be
  12 +included in all copies or substantial portions of the Software.
  13 +
  14 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 README.rdoc
Source Rendered
... ... @@ -0,0 +1,19 @@
  1 += rubydora
  2 +
  3 +Description goes here.
  4 +
  5 +== Contributing to rubydora
  6 +
  7 +* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  8 +* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  9 +* Fork the project
  10 +* Start a feature/bugfix branch
  11 +* Commit and push until you are happy with your contribution
  12 +* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  13 +* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
  14 +
  15 +== Copyright
  16 +
  17 +Copyright (c) 2011 Chris Beer. See LICENSE.txt for
  18 +further details.
  19 +
54 Rakefile
... ... @@ -0,0 +1,54 @@
  1 +require 'rubygems'
  2 +require 'bundler'
  3 +begin
  4 + Bundler.setup(:default, :development)
  5 +rescue Bundler::BundlerError => e
  6 + $stderr.puts e.message
  7 + $stderr.puts "Run `bundle install` to install missing gems"
  8 + exit e.status_code
  9 +end
  10 +require 'rake'
  11 +
  12 +require 'jeweler'
  13 +Jeweler::Tasks.new do |gem|
  14 + # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
  15 + gem.name = "rubydora"
  16 + gem.homepage = "http://github.com/cbeer/rubydora"
  17 + gem.license = "MIT"
  18 + gem.summary = %Q{TODO: one-line summary of your gem}
  19 + gem.description = %Q{TODO: longer description of your gem}
  20 + gem.email = "chris@cbeer.info"
  21 + gem.authors = ["Chris Beer"]
  22 +end
  23 +Jeweler::RubygemsDotOrgTasks.new
  24 +
  25 +# Get your spec rake tasks working in RSpec 2.0
  26 +
  27 +require 'rspec/core/rake_task'
  28 +
  29 +desc 'Default: run specs.'
  30 +task :default => :spec
  31 +
  32 +desc "Run specs"
  33 +RSpec::Core::RakeTask.new do |t|
  34 + t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
  35 + # Put spec opts in a file named .rspec in root
  36 +end
  37 +
  38 +desc "Generate code coverage"
  39 +require "rcov/rcovtask"
  40 +RSpec::Core::RakeTask.new(:coverage) do |t|
  41 + t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
  42 + t.rcov = true
  43 + t.rcov_opts = ['--exclude', 'spec', '--exclude', 'gems/*']
  44 +end
  45 +
  46 +require 'rake/rdoctask'
  47 +Rake::RDocTask.new do |rdoc|
  48 + version = File.exist?('VERSION') ? File.read('VERSION') : ""
  49 +
  50 + rdoc.rdoc_dir = 'rdoc'
  51 + rdoc.title = "rubydora #{version}"
  52 + rdoc.rdoc_files.include('README*')
  53 + rdoc.rdoc_files.include('lib/**/*.rb')
  54 +end
1  VERSION
... ... @@ -0,0 +1 @@
  1 +0.0.0
26 lib/rubydora.rb
... ... @@ -0,0 +1,26 @@
  1 +module Rubydora
  2 + autoload :Datastream, "rubydora/datastream"
  3 + autoload :Repository, "rubydora/repository"
  4 + autoload :ResourceIndex, "rubydora/resource_index"
  5 + autoload :RestApiClient, "rubydora/rest_api_client"
  6 + autoload :DigitalObject, "rubydora/digital_object"
  7 +
  8 + require 'fastercsv'
  9 + require 'restclient'
  10 + require 'nokogiri'
  11 +
  12 + def self.version
  13 + @version ||= File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).chomp
  14 + end
  15 +
  16 + VERSION = self.version
  17 +
  18 + def self.connect *args
  19 + Repository.new *args
  20 + end
  21 +
  22 + def self.repository
  23 + nil
  24 + end
  25 +
  26 +end
51 lib/rubydora/datastream.rb
... ... @@ -0,0 +1,51 @@
  1 +module Rubydora
  2 + module Datastream
  3 + def self.new digital_object, dsid, dsxml = nil
  4 + ds = Base.new digital_object, dsid
  5 + ds.profile = dsxml
  6 + ds
  7 + end
  8 +
  9 + class Base
  10 + attr_reader :dsid
  11 +
  12 + def initialize digital_object, dsid
  13 + @digital_object = digital_object
  14 + @dsid = dsid
  15 + end
  16 +
  17 + def new!
  18 + @new = true
  19 + end
  20 +
  21 + def new?
  22 + @new
  23 + end
  24 +
  25 +
  26 + def profile
  27 + @profile ||= begin
  28 + profile = nil
  29 + end
  30 + end
  31 +
  32 + def profile= xml
  33 + @profile = xml
  34 + end
  35 +
  36 + def dirty?
  37 + false
  38 + end
  39 +
  40 + def save
  41 + return create if new?
  42 + end
  43 +
  44 + protected
  45 +
  46 + def client
  47 + @digital_object.send(:client)["datastreams/%s"%[dsid]]
  48 + end
  49 + end
  50 + end
  51 +end
76 lib/rubydora/digital_object.rb
... ... @@ -0,0 +1,76 @@
  1 +module Rubydora
  2 + module DigitalObject
  3 + def self.find pid, repository = nil
  4 + Base.new pid, repository
  5 + end
  6 +
  7 + def self.create pid, options = {}, repository = nil
  8 + repository ||= Rubydora.repository
  9 +
  10 + repository.ingest(options.merge(:pid => pid))
  11 +
  12 + Base.new pid, repository
  13 + end
  14 +
  15 + class Base
  16 + attr_reader :pid
  17 +
  18 + def initialize pid, repository = nil
  19 + @pid = pid
  20 + @repository = repository
  21 + end
  22 +
  23 + def delete
  24 + repository.purge_object(:pid => pid)
  25 + end
  26 +
  27 + def profile
  28 + @profile ||= begin
  29 + profile_xml = repository.object(:pid => pid)
  30 + profile_xml.gsub! '<objectProfile', '<objectProfile xmlns="http://www.fedora.info/definitions/1/0/access/"' unless profile_xml =~ /xmlns=/
  31 + doc = Nokogiri::XML(profile_xml)
  32 + h = doc.xpath('/access:objectProfile/*', {'access' => "http://www.fedora.info/definitions/1/0/access/"} ).inject({}) do |sum, node|
  33 + sum[node.name] ||= []
  34 + sum[node.name] << node.text
  35 +
  36 + if node.name == "objModels"
  37 + sum[node.name] = node.xpath('access:model', {'access' => "http://www.fedora.info/definitions/1/0/access/"}).map { |x| x.text }
  38 + end
  39 +
  40 + sum
  41 + end
  42 + h.select { |key, value| value.length == 1 }.each do |key, value|
  43 + h[key] = value.first
  44 + end
  45 +
  46 + h
  47 +
  48 + end
  49 + end
  50 +
  51 + def datastreams
  52 + @datastreams ||= begin
  53 + h = Hash.new { |h,k| h[k] = Datastream.new self, k; h[k].new!; h[k] }
  54 + datastreams_xml = repository.datastreams(:pid => pid)
  55 + datastreams_xml.gsub! '<objectDatastreams', '<objectDatastreams xmlns="http://www.fedora.info/definitions/1/0/access/"' unless datastreams_xml =~ /xmlns=/
  56 + doc = Nokogiri::XML(datastreams_xml)
  57 + doc.xpath('//access:datastream', {'access' => "http://www.fedora.info/definitions/1/0/access/"}).each { |ds| h[ds['dsid']] = Datastream.new self, ds['dsid'], ds.to_s }
  58 + h
  59 + end
  60 + end
  61 +
  62 + def save
  63 + self.datastreams.select(&:dirty?).reject(&:empty?).each(&:save)
  64 + end
  65 +
  66 +
  67 + protected
  68 +
  69 + def repository
  70 + @repository ||= Rubydora.repository
  71 + end
  72 +
  73 + end
  74 +
  75 + end
  76 +end
17 lib/rubydora/repository.rb
... ... @@ -0,0 +1,17 @@
  1 +module Rubydora
  2 + class Repository
  3 + include ResourceIndex
  4 + include RestApiClient
  5 + attr_reader :config
  6 +
  7 + def initialize options = {}
  8 + @config = options
  9 + end
  10 +
  11 + def find pid
  12 + DigitalObject.find(pid, self)
  13 + end
  14 +
  15 +
  16 + end
  17 +end
19 lib/rubydora/resource_index.rb
... ... @@ -0,0 +1,19 @@
  1 +module Rubydora
  2 + module ResourceIndex
  3 + def find_by_sparql query, options = { :binding => 'pid' }
  4 + self.sparql(query).map { |x| self.find(x[options[:binding]]) rescue nil }
  5 + end
  6 +
  7 + def sparql query
  8 + FasterCSV.parse(self.risearch(query), :headers => true)
  9 + end
  10 +
  11 + protected
  12 + def risearch query, options = {}
  13 + request_params = { :dt => 'on', :format => 'CSV', :lang => 'sparql', :limit => nil, :query => query, type => 'tuples' }.merge(options)
  14 +
  15 + self.client['risearch'].post request_params
  16 + end
  17 +
  18 + end
  19 +end
151 lib/rubydora/rest_api_client.rb
... ... @@ -0,0 +1,151 @@
  1 +module Rubydora
  2 + module RestApiClient
  3 + def client config = {}
  4 + config = @config.merge(config)
  5 + @client ||= RestClient::Resource.new @config[:url], :user => @config[:user], :password => @config[:password]
  6 + end
  7 +
  8 + def next_pid options = {}
  9 + client[url_for(object_url() + "/nextPID", options)].post nil
  10 + end
  11 +
  12 + def find_objects options = {}
  13 + raise "" if options[:terms] and options[:query]
  14 +
  15 + client[object_url(nil, options)].get
  16 + end
  17 +
  18 + def object options = {}
  19 + pid = options.delete(:pid)
  20 + client[object_url(pid, options)].get
  21 + end
  22 +
  23 + def ingest options = {}
  24 + pid = options.delete(:pid) || 'new'
  25 + file = options.delete(:file)
  26 + client[object_url(pid, options)].post file, :content_type => 'text/xml'
  27 + end
  28 +
  29 + def modify_object options = {}
  30 + pid = options.delete(:pid)
  31 + client[object_url(pid, options)].put nil
  32 + end
  33 +
  34 + def purge_object options = {}
  35 + pid = options.delete(:pid)
  36 + client[object_url(pid, options)].delete
  37 + end
  38 +
  39 + def object_versions options = {}
  40 + pid = options.delete(:pid)
  41 + raise "" unless pid
  42 + client[url_for(object_url(pid) + "/versions", options)].get
  43 + end
  44 +
  45 + def object_xml options = {}
  46 + pid = options.delete(:pid)
  47 + raise "" unless pid
  48 + client[url_for(object_url(pid) + "/objectXML", options)].get
  49 + end
  50 +
  51 + def datastream options = {}
  52 + pid = options.delete(:pid)
  53 + dsid = options.delete(:dsid)
  54 + client[datastream_url(pid, dsid, options)].get
  55 + end
  56 +
  57 + alias_method :datastreams, :datastream
  58 +
  59 + def set_datastream_options options = {}
  60 + pid = options.delete(:pid)
  61 + dsid = options.delete(:dsid)
  62 + client[datastream_url(pid, dsid, options)].put nil
  63 + end
  64 +
  65 + def datastream_versions options = {}
  66 + pid = options.delete(:pid)
  67 + dsid = options.delete(:dsid)
  68 + raise "" unless dsid
  69 + client[url_for(datastream_url(pid, dsid) + "/versions", options)].get
  70 + end
  71 +
  72 + def datastream_dissemination options = {}
  73 + pid = options.delete(:pid)
  74 + dsid = options.delete(:dsid)
  75 + options[:format] ||= false
  76 + raise "" unless dsid
  77 + client[url_for(datastream_url(pid, dsid) + "/content", options)].get
  78 + end
  79 +
  80 + def add_datastream options = {}
  81 + pid = options.delete(:pid)
  82 + dsid = options.delete(:dsid)
  83 + file = options.delete(:file)
  84 + content_type = options.delete(:content_type) || options[:mimeType] || 'text/plain'
  85 + client[datastream_url(pid, dsid, options)].post file, :content_type => content_type
  86 + end
  87 +
  88 + def modify_datastream options = {}
  89 + pid = options.delete(:pid)
  90 + dsid = options.delete(:dsid)
  91 + file = options.delete(:file)
  92 + content_type = options.delete(:content_type) || options[:mimeType] || 'text/plain'
  93 + client[datastream_url(pid, dsid, options)].put file, :content_type => content_type
  94 + end
  95 +
  96 + def purge_datastream options = {}
  97 + pid = options.delete(:pid)
  98 + dsid = options.delete(:dsid)
  99 + client[datastream_url(pid, dsid, options)].delete
  100 + end
  101 +
  102 + def relationships options = {}
  103 + pid = options.delete(:pid)
  104 + raise "" unless pid
  105 + client[url_for(object_url(pid) + "/relationships", options)].get
  106 + end
  107 +
  108 + def add_relationship options = {}
  109 + pid = options.delete(:pid)
  110 + client[url_for(object_url(pid) + "/relationships", options)].post nil
  111 + end
  112 +
  113 + def purge_relationship options = {}
  114 + pid = options.delete(:pid)
  115 + client[url_for(object_url(pid) + "/relationships", options)].delete
  116 + end
  117 +
  118 +
  119 + def dissemination options = {}
  120 + pid = options.delete(:pid)
  121 + sdef = options.delete(:sdef)
  122 + method = options.delete(:method)
  123 + client[dissemination_url(pid,sdef,method,options)].get
  124 + end
  125 +
  126 + private
  127 +
  128 + def url_for base, options = nil
  129 + return base unless options.is_a? Hash
  130 +
  131 + options[:format] ||= 'xml' unless options[:format] == false
  132 + options.delete(:format) unless options[:format]
  133 + "#{base}" + (("?#{options.map { |key, value| "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}"}.join("&") }" if options and not options.empty?) || '')
  134 + end
  135 +
  136 + def object_url pid = nil, options = nil
  137 + url_for("objects" + (("/#{CGI::escape(pid)}" if pid) || ''), options)
  138 + end
  139 +
  140 + def dissemination_url pid, sdef = nil, method = nil, options = nil
  141 + raise "" unless pid
  142 + url_for(object_url(pid) + "/methods" + (("/#{CGI::escape(sdef)}" if sdef) || '') + (("/#{CGI::escape(method)}" if method) || ''), options)
  143 + end
  144 +
  145 + def datastream_url pid, dsid = nil, options = nil
  146 + raise "" unless pid
  147 + url_for(object_url(pid) + "/datastreams" + (("/#{CGI::escape(dsid)}" if dsid) || ''), options)
  148 + end
  149 +
  150 + end
  151 +end
107 spec/digital_object_spec.rb
... ... @@ -0,0 +1,107 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Rubydora::DigitalObject do
  4 + describe "find" do
  5 + it "should load a DigitalObject instance" do
  6 + Rubydora::DigitalObject.find("pid").should be_a_kind_of(Rubydora::DigitalObject::Base)
  7 + end
  8 + end
  9 +
  10 + describe "profile" do
  11 + before(:each) do
  12 + @mock_repository = mock(Rubydora::Repository)
  13 + @object = Rubydora::DigitalObject.find 'pid', @mock_repository
  14 + end
  15 +
  16 + it "should convert object profile to a simple hash" do
  17 + @mock_repository.should_receive(:object).with(:pid => 'pid').and_return("<objectProfile><a>1</a><b>2</b><objModels><model>3</model><model>4</model></objectProfile>")
  18 + h = @object.profile
  19 +
  20 + h.should have_key("a")
  21 + h['a'].should == '1'
  22 + h.should have_key("b")
  23 + h['b'].should == '2'
  24 + h.should have_key("objModels")
  25 + h['objModels'].should == ['3', '4']
  26 +
  27 + end
  28 + end
  29 +
  30 + describe "create" do
  31 + it "should call the Fedora REST API to create a new object" do
  32 + @mock_repository = mock(Rubydora::Repository)
  33 + @mock_repository.should_receive(:ingest).with(instance_of(Hash)).and_return(nil)
  34 + obj = Rubydora::DigitalObject.create "pid", { :a => 1, :b => 2}, @mock_repository
  35 + obj.should be_a_kind_of(Rubydora::DigitalObject::Base)
  36 + end
  37 + end
  38 +
  39 + describe "retreive" do
  40 + before(:each) do
  41 + @mock_repository = mock(Rubydora::Repository)
  42 + @object = Rubydora::DigitalObject.find 'pid', @mock_repository
  43 + end
  44 +
  45 + describe "datastreams" do
  46 + it "should provide a hash populated by the existing datastreams" do
  47 + @mock_repository.should_receive(:datastreams).with(:pid => 'pid').and_return("<objectDatastreams><datastream dsid='a'></datastream>><datastream dsid='b'></datastream>><datastream dsid='c'></datastream></objectDatastreams>")
  48 +
  49 + @object.datastreams.should have_key("a")
  50 + @object.datastreams.should have_key("b")
  51 + @object.datastreams.should have_key("c")
  52 + end
  53 +
  54 + it "should allow other datastreams to be added" do
  55 + @mock_repository.should_receive(:datastreams).with(:pid => 'pid').and_return("<objectDatastreams><datastream dsid='a'></datastream>><datastream dsid='b'></datastream>><datastream dsid='c'></datastream></objectDatastreams>")
  56 +
  57 + @object.datastreams.length.should == 3
  58 +
  59 + ds = @object.datastreams["z"]
  60 + ds.should be_a_kind_of(Rubydora::Datastream::Base)
  61 + ds.new?.should == true
  62 +
  63 + @object.datastreams.length.should == 4
  64 + end
  65 +
  66 + end
  67 +
  68 + end
  69 +
  70 + describe "save" do
  71 + before(:each) do
  72 + @mock_repository = mock(Rubydora::Repository)
  73 + @object = Rubydora::DigitalObject.find 'pid', @mock_repository
  74 + end
  75 +
  76 + it "should save all dirty datastreams" do
  77 + @ds1 = mock()
  78 + @ds1.should_receive(:dirty?).and_return(false)
  79 + @ds1.should_not_receive(:save)
  80 + @ds2 = mock()
  81 + @ds2.should_receive(:dirty?).and_return(true)
  82 + @ds2.should_receive(:empty?).and_return(true)
  83 + @ds2.should_not_receive(:save)
  84 + @ds3 = mock()
  85 + @ds3.should_receive(:dirty?).and_return(true)
  86 + @ds3.should_receive(:empty?).and_return(false)
  87 + @ds3.should_receive(:save)
  88 +
  89 + @object.should_receive(:datastreams).and_return([@ds1, @ds2, @ds3])
  90 +
  91 + @object.save
  92 + end
  93 + end
  94 +
  95 + describe "delete" do
  96 + before(:each) do
  97 + @mock_repository = mock()
  98 + @object = Rubydora::DigitalObject.find 'pid', @mock_repository
  99 + end
  100 +
  101 + it "should call the Fedora REST API" do
  102 + @mock_repository.should_receive(:purge_object).with({:pid => 'pid'})
  103 + @object.delete
  104 + end
  105 + end
  106 +
  107 +end
53 spec/repository_spec.rb
... ... @@ -0,0 +1,53 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Rubydora::Repository do
  4 + before(:each) do
  5 + @repository = Rubydora::Repository.new
  6 + end
  7 +
  8 + describe "client" do
  9 + it "should return a RestClient resource" do
  10 + client = @repository.client :url => 'http://example.org', :user => 'fedoraAdmin', :password => 'fedoraAdmin'
  11 +
  12 + client.should be_a_kind_of(RestClient::Resource)
  13 + end
  14 + end
  15 +
  16 + describe "find" do
  17 +
  18 + it "should load objects by pid" do
  19 + @mock_object = mock(Rubydora::DigitalObject)
  20 + Rubydora::DigitalObject.should_receive(:find).with("pid", instance_of(Rubydora::Repository)).and_return @mock_object
  21 +
  22 + @repository.find('pid')
  23 + end
  24 +
  25 + end
  26 +
  27 + describe "sparql" do
  28 + it "should return csv results for sparql queries" do
  29 + resource_index_query = ""
  30 + @repository.should_receive(:risearch).with(resource_index_query).and_return("pid\na\nb\nc\n")
  31 +
  32 + csv = @repository.sparql(resource_index_query)
  33 + end
  34 +
  35 +end
  36 +
  37 +describe "find_by_sparql" do
  38 + it "should attempt to load objects from the results of a sparql query" do
  39 +
  40 + resource_index_query = ""
  41 + @repository.should_receive(:risearch).with(resource_index_query).and_return("pid\na\nb\nc\n")
  42 +
  43 + @repository.should_receive(:find).with('a').and_return(1)
  44 + @repository.should_receive(:find).with('b').and_return(1)
  45 + @repository.should_receive(:find).with('c').and_return(1)
  46 +
  47 + objects = @repository.find_by_sparql(resource_index_query)
  48 +
  49 + objects.length.should == 3
  50 + end
  51 + end
  52 +
  53 +end
131 spec/rest_api_client_spec.rb
... ... @@ -0,0 +1,131 @@
  1 +require 'spec_helper'
  2 +
  3 +describe Rubydora::RestApiClient do
  4 + class MockRepository
  5 + include Rubydora::RestApiClient
  6 +
  7 + attr_accessor :config
  8 + end
  9 +
  10 + before(:each) do
  11 + @mock_repository = MockRepository.new
  12 + @mock_repository.config = { :url => 'http://example.org',:user => 'fedoraAdmin', :password => 'fedoraAdmin'}
  13 + end
  14 +
  15 + it "should call nextPID" do
  16 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/nextPID?format=xml"))
  17 + @mock_repository.next_pid
  18 + end
  19 +
  20 + it "should find objects" do
  21 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects?query=a&format=xml"))
  22 + @mock_repository.find_objects :query => 'a'
  23 + end
  24 +
  25 + it "should show object properties" do
  26 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/z?format=xml"))
  27 + @mock_repository.object :pid => 'z'
  28 + end
  29 +
  30 + it "ingest" do
  31 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/new?format=xml"))
  32 + @mock_repository.ingest
  33 + end
  34 +
  35 + it "ingest with pid" do
  36 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid?format=xml"))
  37 + @mock_repository.ingest :pid => 'mypid'
  38 + end
  39 +
  40 + it "modify_object" do
  41 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid?format=xml&state=Z"))
  42 + @mock_repository.modify_object :pid => 'mypid', :state => 'Z'
  43 + end
  44 +
  45 + it "purge_object" do
  46 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid?format=xml"))
  47 + @mock_repository.purge_object :pid => 'mypid'
  48 + end
  49 +
  50 + it "object_versions" do
  51 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/versions?format=xml"))
  52 + @mock_repository.object_versions :pid => 'mypid'
  53 + end
  54 +
  55 + it "object_xml" do
  56 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/objectXML?format=xml"))
  57 + @mock_repository.object_xml :pid => 'mypid'
  58 + end
  59 +
  60 + it "datastream" do
  61 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/datastreams?format=xml"))
  62 + @mock_repository.datastream :pid => 'mypid'
  63 + end
  64 +
  65 + it "datastream" do
  66 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/datastreams/aaa?format=xml"))
  67 + @mock_repository.datastream :pid => 'mypid', :dsid => 'aaa'
  68 + end
  69 +
  70 + it "datastream_dissemination" do
  71 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/datastreams/aaa/content"))
  72 + @mock_repository.datastream_dissemination :pid => 'mypid', :dsid => 'aaa'
  73 + end
  74 +
  75 + it "add_datastream" do
  76 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/datastreams/aaa?format=xml"))
  77 + @mock_repository.add_datastream :pid => 'mypid', :dsid => 'aaa'
  78 + end
  79 +
  80 + it "modify_datastream" do
  81 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/datastreams/aaa?format=xml"))
  82 + @mock_repository.modify_datastream :pid => 'mypid', :dsid => 'aaa'
  83 + end
  84 +
  85 + it "purge_datastream" do
  86 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/datastreams/aaa?format=xml"))
  87 + @mock_repository.purge_datastream :pid => 'mypid', :dsid => 'aaa'
  88 + end
  89 +
  90 + it "set_datastream_options" do
  91 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/datastreams/aaa?format=xml&aparam=true"))
  92 + @mock_repository.set_datastream_options :pid => 'mypid', :dsid => 'aaa', :aparam => true
  93 + end
  94 +
  95 + it "datastream_versions" do
  96 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/datastreams/aaa/versions?format=xml"))
  97 + @mock_repository.datastream_versions :pid => 'mypid', :dsid => 'aaa'
  98 +
  99 + end
  100 +
  101 + it "relationships" do
  102 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/relationships?format=xml"))
  103 + @mock_repository.relationships :pid => 'mypid'
  104 + end
  105 +
  106 + it "add_relationship" do
  107 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/relationships?subject=z&format=xml"))
  108 + @mock_repository.add_relationship :pid => 'mypid', :subject => 'z'
  109 + end
  110 +
  111 + it "purge_relationships" do
  112 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/relationships?subject=z&format=xml"))
  113 + @mock_repository.purge_relationship :pid => 'mypid', :subject => 'z'
  114 + end
  115 +
  116 + it "dissemination" do
  117 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/methods?format=xml"))
  118 + @mock_repository.dissemination :pid => 'mypid'
  119 + end
  120 +
  121 + it "dissemination" do
  122 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/methods/sdef?format=xml"))
  123 + @mock_repository.dissemination :pid => 'mypid', :sdef => 'sdef'
  124 + end
  125 +
  126 + it "dissemination" do
  127 + RestClient::Request.should_receive(:execute).with(hash_including(:url => "http://example.org/objects/mypid/methods/sdef/method?format=xml"))
  128 + @mock_repository.dissemination :pid => 'mypid', :sdef => 'sdef', :method => 'method'
  129 + end
  130 +
  131 +end
4 spec/spec_helper.rb
... ... @@ -0,0 +1,4 @@
  1 +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
  2 +$LOAD_PATH.unshift(File.dirname(__FILE__))
  3 +require 'rubydora'
  4 +require 'rspec'

0 comments on commit f4e7961

Please sign in to comment.
Something went wrong with that request. Please try again.