lib/ezid/request.rb in ezid-client-0.1.0 vs lib/ezid/request.rb in ezid-client-0.1.1

- old
+ new

@@ -1,34 +1,55 @@ require "uri" require "net/http" -require "delegate" -require_relative "api" -require_relative "response" - module Ezid # # A request to the EZID service. # - class Request < SimpleDelegator + # @note A Request should only be created by an Ezid::Client instance. + # @api private + class Request EZID_HOST = "ezid.cdlib.org" CHARSET = "UTF-8" CONTENT_TYPE = "text/plain" - def self.build(op, *args) - http_method, path, query = Api.send(op, *args) - uri = URI::HTTPS.build(host: EZID_HOST, path: path, query: query) - http_request = Net::HTTP.const_get(http_method).new(uri) - Request.new(http_request) + attr_reader :http_request, :uri, :operation + + 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) end + # Executes the request and returns the HTTP response + # @return [Net::HTTPResponse] the response def execute - http_response = Net::HTTP.start(uri.host, use_ssl: true) do |http| - set_content_type(CONTENT_TYPE, charset: CHARSET) - http.request(__getobj__) + Net::HTTP.start(uri.host, use_ssl: true) do |http| + http.request(http_request) end - Response.build(http_response) + 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? end end end