lib/ezid/request.rb in ezid-client-0.3.0 vs lib/ezid/request.rb in ezid-client-0.4.0

- old
+ new

@@ -1,55 +1,41 @@ +require "delegate" require "uri" require "net/http" module Ezid # # A request to the EZID service. # - # @note A Request should only be created by an Ezid::Client instance. # @api private - class Request + class Request < SimpleDelegator - EZID_HOST = "ezid.cdlib.org" + HOST = "https://ezid.cdlib.org" CHARSET = "UTF-8" CONTENT_TYPE = "text/plain" - attr_reader :http_request, :uri, :operation + def self.execute(*args) + request = new(*args) + yield request if block_given? + request.execute + end - def initialize(*args) - @operation = args - http_method, path, query = Api.send(*args) - @uri = URI::HTTPS.build(host: EZID_HOST, path: path, query: query) - @http_request = Net::HTTP.const_get(http_method).new(uri) - @http_request.set_content_type(CONTENT_TYPE, charset: CHARSET) + # @param method [Symbol] the Net::HTTP constant for the request method + # @param path [String] the uri path (including query string, if any) + def initialize(method, path) + http_method = Net::HTTP.const_get(method) + uri = URI.parse([HOST, path].join) + super(http_method.new(uri)) + set_content_type(CONTENT_TYPE, charset: CHARSET) end - # Executes the request and returns the HTTP response - # @return [Net::HTTPResponse] the response + # Executes the request and returns the response + # @return [Ezid::Response] the response def execute - Net::HTTP.start(uri.host, use_ssl: true) do |http| - http.request(http_request) + http_response = Net::HTTP.start(uri.host, use_ssl: true) do |http| + http.request(__getobj__) end - end - - # Adds authentication data to the request - # @param opts [Hash] the options. - # Must include either: `:cookie`, or: `:user` and `:password`. - # @option opts [String] :cookie a session cookie - # @option opts [String] :user user name for basic auth - # @option opts [String] :password password for basic auth - def add_authentication(opts={}) - if opts[:cookie] - http_request["Cookie"] = opts[:cookie] - else - http_request.basic_auth(opts[:user], opts[:password]) - end - end - - # Adds EZID metadata (if any) to the request body - # @param metadata [Ezid::Metadata] the metadata to add - def add_metadata(metadata) - http_request.body = metadata.to_anvl unless metadata.empty? + Response.new(http_response) end end end