lib/ezid/client.rb in ezid-client-0.7.0 vs lib/ezid/client.rb in ezid-client-0.8.0
- old
+ new
@@ -1,5 +1,7 @@
+require "uri"
+
require_relative "configuration"
require_relative "request"
require_relative "response"
require_relative "session"
require_relative "metadata"
@@ -18,33 +20,37 @@
class << self
# Configuration reader
def config
@config ||= Configuration.new
end
-
+
# Yields the configuration to a block
# @yieldparam [Ezid::Configuration] the configuration
def configure
yield config
end
- end
+ end
- attr_reader :session, :user, :password # , :host
+ attr_reader :session, :user, :password, :host, :use_ssl
def initialize(opts = {})
@session = Session.new
+ @host = opts[:host] || config.host
+ @use_ssl = opts.fetch(:use_ssl, config.use_ssl)
@user = opts[:user] || config.user
+ raise Error, "User name is required." unless user
@password = opts[:password] || config.password
+ raise Error, "Password is required." unless password
if block_given?
login
yield self
logout
end
end
def inspect
- "#<#{self.class.name} user=\"#{user}\" session=#{logged_in? ? 'OPEN' : 'CLOSED'}>"
+ "#<#{self.class.name} host=\"#{host}\" user=\"#{user}\" session=#{logged_in? ? 'OPEN' : 'CLOSED'}>"
end
# The client configuration
# @return [Ezid::Configuration] the configuration object
def config
@@ -62,11 +68,11 @@
# @return [Ezid::Client] the client
def login
if logged_in?
logger.info("Already logged in, skipping login request.")
else
- response = Request.execute(:Get, "/login") do |request|
+ response = Request.execute(:Get, build_uri("/login")) do |request|
add_authentication(request)
end
handle_response(response, "LOGIN")
session.open(response.cookie)
end
@@ -75,11 +81,11 @@
# Close the session
# @return [Ezid::Client] the client
def logout
if logged_in?
- response = Request.execute(:Get, "/logout")
+ response = Request.execute(:Get, build_uri("/logout"))
handle_response(response, "LOGOUT")
session.close
else
logger.info("Not logged in, skipping logout request.")
end
@@ -94,11 +100,11 @@
# @param identifier [String] the identifier string to create
# @param metadata [String, Hash, Ezid::Metadata] optional metadata to set
# @raise [Ezid::Error]
# @return [Ezid::Response] the response
def create_identifier(identifier, metadata=nil)
- response = Request.execute(:Put, "/id/#{identifier}") do |request|
+ response = Request.execute(:Put, build_uri("/id/#{identifier}")) do |request|
add_authentication(request)
add_metadata(request, metadata)
end
handle_response(response, "CREATE #{identifier}")
end
@@ -108,59 +114,64 @@
# @raise [Ezid::Error]
# @return [Ezid::Response] the response
def mint_identifier(shoulder=nil, metadata=nil)
shoulder ||= config.default_shoulder
raise Error, "Shoulder missing -- cannot mint identifier." unless shoulder
- response = Request.execute(:Post, "/shoulder/#{shoulder}") do |request|
+ response = Request.execute(:Post, build_uri("/shoulder/#{shoulder}")) do |request|
add_authentication(request)
add_metadata(request, metadata)
end
handle_response(response, "MINT #{shoulder}")
end
-
+
# @param identifier [String] the identifier to modify
# @param metadata [String, Hash, Ezid::Metadata] metadata to set
# @raise [Ezid::Error]
# @return [Ezid::Response] the response
def modify_identifier(identifier, metadata)
- response = Request.execute(:Post, "/id/#{identifier}") do |request|
+ response = Request.execute(:Post, build_uri("/id/#{identifier}")) do |request|
add_authentication(request)
add_metadata(request, metadata)
end
handle_response(response, "MODIFY #{identifier}")
end
# @param identifier [String] the identifier to retrieve
# @raise [Ezid::Error]
# @return [Ezid::Response] the response
def get_identifier_metadata(identifier)
- response = Request.execute(:Get, "/id/#{identifier}") do |request|
+ response = Request.execute(:Get, build_uri("/id/#{identifier}")) do |request|
add_authentication(request)
end
handle_response(response, "GET #{identifier}")
end
# @param identifier [String] the identifier to delete
# @raise [Ezid::Error]
# @return [Ezid::Response] the response
def delete_identifier(identifier)
- response = Request.execute(:Delete, "/id/#{identifier}") do |request|
+ response = Request.execute(:Delete, build_uri("/id/#{identifier}")) do |request|
add_authentication(request)
end
handle_response(response, "DELETE #{identifier}")
end
# @param subsystems [Array]
# @raise [Ezid::Error]
# @return [Ezid::Status] the status response
def server_status(*subsystems)
- response = Request.execute(:Get, "/status?subsystems=#{subsystems.join(',')}")
+ response = Request.execute(:Get, build_uri("/status?subsystems=#{subsystems.join(',')}"))
handle_response(Status.new(response), "STATUS")
end
private
+ def build_uri(path)
+ scheme = use_ssl ? "https" : "http"
+ URI([scheme, "://", host, path].join)
+ end
+
# Adds authentication data to the request
def add_authentication(request)
if session.open?
request["Cookie"] = session.cookie
else
@@ -170,10 +181,10 @@
# Adds EZID metadata (if any) to the request body
def add_metadata(request, metadata)
return if metadata.nil? || metadata.empty?
metadata = Metadata.new(metadata) unless metadata.is_a?(Metadata)
- request.body = metadata.to_anvl(false)
+ request.body = metadata.to_anvl(false)
end
def handle_response(response, request_info)
log_level = response.error? ? Logger::ERROR : Logger::INFO
message = "EZID #{request_info} -- #{response.status_line}"