Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dchandekstark committed Feb 19, 2015
1 parent 7a6f76e commit e60276f
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 50 deletions.
4 changes: 1 addition & 3 deletions lib/ezid/client.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
require "net/http"

require_relative "configuration"
require_relative "response"
require_relative "session"
require_relative "metadata"
require_relative "identifier"
require_relative "error"
require_relative "status_response"

Dir[File.expand_path("../requests/*.rb", __FILE__)].each { |m| require m }

Expand Down Expand Up @@ -151,7 +149,7 @@ def delete_identifier(identifier)

# @param subsystems [Array]
# @raise [Ezid::Error]
# @return [Ezid::Status] the status response
# @return [Ezid::StatusResponse] the status response
def server_status(*subsystems)
execute ServerStatusRequest, *subsystems
end
Expand Down
5 changes: 3 additions & 2 deletions lib/ezid/requests/identifier_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ def path
"/id/#{identifier}"
end

def handle_args(*args)
@identifier = args.first
def initialize(client, identifier)
@identifier = identifier
super
end
end
end
4 changes: 2 additions & 2 deletions lib/ezid/requests/identifier_with_metadata_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Ezid
class IdentifierWithMetadataRequest < IdentifierRequest
attr_reader :metadata

def handle_args(*args)
def initialize(client, identifier, metadata)
@metadata = Metadata.new(metadata)
super
@metadata = Metadata.new(args[1])
end
end
end
7 changes: 2 additions & 5 deletions lib/ezid/requests/login_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
module Ezid
class LoginRequest < Request
self.http_method = GET
self.path = "/login"

def path
"/login"
end

def handle_response(http_response)
def handle_response(*)
super do |response|
session.open(response.cookie)
end
Expand Down
7 changes: 2 additions & 5 deletions lib/ezid/requests/logout_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
module Ezid
class LogoutRequest < Request
self.http_method = GET

def path
"/logout"
end
self.path = "/logout"

def authentication_required?
false
end

def handle_response(http_response)
def handle_response(*)
super do |response|
session.close
end
Expand Down
7 changes: 4 additions & 3 deletions lib/ezid/requests/mint_identifier_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class MintIdentifierRequest < Request
self.http_method = POST
attr_reader :shoulder, :metadata

def handle_args(*args)
@shoulder = args.first
@metadata = Metadata.new(args[1])
def initialize(client, shoulder, metadata)
@shoulder = shoulder
@metadata = Metadata.new(metadata)
super
end

def path
Expand Down
46 changes: 26 additions & 20 deletions lib/ezid/requests/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "net/http"
require "forwardable"

require_relative "../responses/response"

module Ezid
#
# A request to the EZID service.
Expand All @@ -16,13 +18,14 @@ class Request < SimpleDelegator
CHARSET = "UTF-8"
CONTENT_TYPE = "text/plain"

# HTTP methods
GET = Net::HTTP::Get
PUT = Net::HTTP::Put
POST = Net::HTTP::Post
DELETE = Net::HTTP::Delete

class << self
attr_accessor :http_method
attr_accessor :http_method, :path

def execute(client, *args)
request = new(client, *args)
Expand All @@ -32,7 +35,7 @@ def execute(client, *args)

def short_name
name.split("::").last.sub("Request", "")
end
end
end

attr_reader :client
Expand All @@ -41,20 +44,13 @@ def short_name
# @param client [Ezid::Client] the client
def initialize(client, *args)
@client = client
handle_args(*args)
super build_request
end

# Executes the request and returns the response
# @return [Ezid::Response] the response
def execute
http_response = connection.start do |conn|
set_content_type(CONTENT_TYPE, charset: CHARSET)
add_authentication if authentication_required?
add_metadata if accepts_metadata?
conn.request(__getobj__)
end
handle_response(http_response)
handle_response(get_response_for_request)
end

# The request URI
Expand All @@ -66,7 +62,13 @@ def uri
# HTTP request path
# @return [String] the path
def path
raise NotImplementedError, "Subclasses must implement `#path'."
self.class.path
end

# Class to wrap Net::HTTPResponse
# @return [Class]
def response_class
Response
end

# HTTP request query string
Expand All @@ -77,23 +79,27 @@ def authentication_required?
true
end

def accepts_metadata?
respond_to?(:metadata)
def has_metadata?
!metadata.empty? rescue false
end

protected

def handle_response(http_response)
Response.new(http_response).tap do |response|
response_class.new(http_response).tap do |response|
yield response if block_given?
end
end

# Subclass hook
def handle_args(*args); end

private

def get_response_for_request
connection.start do |conn|
set_content_type(CONTENT_TYPE, charset: CHARSET)
add_authentication if authentication_required?
add_metadata if has_metadata?
conn.request(__getobj__)
end
end

def build_request
self.class.http_method.new(uri)
end
Expand All @@ -117,7 +123,7 @@ def add_authentication
end

def add_metadata
self.body = metadata.to_anvl(false) unless metadata.empty?
self.body = metadata.to_anvl(false)
end

end
Expand Down
20 changes: 10 additions & 10 deletions lib/ezid/requests/server_status_request.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
require_relative "request"
require_relative "../responses/status_response"

module Ezid
class ServerStatusRequest < Request
self.http_method = GET
self.path = "/status"

attr_reader :subsystems

def path
"/status"
def initialize(client, *args)
@subsystems = args
super
end

def query
"subsystems=#{subsystems.join(',')}"
end

def handle_args(*args)
@subsystems = args
end

def handle_response(http_response)
Status.new(http_response)
end

def authentication_required?
false
end

def response_class
StatusResponse
end
end
end
File renamed without changes.
File renamed without changes.

0 comments on commit e60276f

Please sign in to comment.