Sha256: ea69e0ecfffa9269ccd576abfea93e246d2e1080cb47acbf5c2998e056db4ab8

Contents?: true

Size: 964 Bytes

Versions: 4

Compression:

Stored size: 964 Bytes

Contents

require "delegate"
require "uri"
require "net/http"

module Ezid
  #
  # A request to the EZID service.
  #
  # @api private
  #
  class Request < SimpleDelegator

    CHARSET = "UTF-8"
    CONTENT_TYPE = "text/plain"

    def self.execute(*args)
      request = new(*args)
      yield request if block_given?
      request.execute
    end

    # @param method [Symbol] the Net::HTTP constant for the request method
    # @param uri [URI] the uri
    def initialize(method, uri)
      http_method = Net::HTTP.const_get(method)
      super(http_method.new(uri))
      set_content_type(CONTENT_TYPE, charset: CHARSET)
    end

    # Executes the request and returns the response
    # @return [Ezid::Response] the response
    def execute
      http_response = Net::HTTP.start(uri.host, use_ssl: use_ssl?) do |http|
        http.request(__getobj__)
      end
      Response.new(http_response)
    end

    def use_ssl?
      uri.is_a?(URI::HTTPS)
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ezid-client-0.11.0 lib/ezid/request.rb
ezid-client-0.10.0 lib/ezid/request.rb
ezid-client-0.9.1 lib/ezid/request.rb
ezid-client-0.9.0 lib/ezid/request.rb