Sha256: 28e40d8dc365fbec65eebe96bca1ae551ffa6caf8e8b9de9e788cec03777eee4

Contents?: true

Size: 2 KB

Versions: 4

Compression:

Stored size: 2 KB

Contents

module Cts
  module Mpx
    module Driver
      #
      # A single request of any type to the services.
      #
      # @attribute method
      #   @return [Symbol] type of rest method, GET, PUT, POST, DELETE
      # @attribute url
      #   @return [String] url to make the request against
      # @attribute query
      #   @return [Hash] query to send with the request
      # @attribute payload
      #   @return [String] payload to be sent to the services
      # @attribute response
      #   @return [Cts::Mpx::Driver::Response] response from the service
      # @attribute headers
      #   @return [Hash] headers to transmit to the services along with the request
      class Request
        include Creatable

        attribute name: 'method', kind_of: Symbol
        attribute name: 'url', kind_of: String
        attribute name: 'query', kind_of: Hash
        attribute name: 'payload', kind_of: String
        attribute name: 'response', kind_of: ::Cts::Mpx::Driver::Response
        attribute name: 'headers', kind_of: Hash

        # Call the built request.
        # @raise [RuntimeException] if the method is not a get, put, post or delete
        # @raise [RuntimeException] if the url is not a valid reference
        # @return [Cts::Mpx::Driver::Response] response from the service
        def call
          @headers ||= {}
          @query ||= {}

          call_exceptions method, url
          socket = Connections[url]
          params = {
            headers: @headers,
            path:    URI.parse(url).path,
            query:   @query
          }
          params[:body] = payload if payload

          r = socket.send method, params

          @response = Response.create original: r
          @response
        end

        private

        def call_exceptions(method, url)
          raise "#{method} is not a valid method" unless %i[get put post delete].include? method.downcase
          raise "#{url} is not a valid reference" unless Cts::Mpx::Validators.reference? url
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cts-mpx-1.2.0 lib/cts/mpx/driver/request.rb
cts-mpx-1.1.2 lib/cts/mpx/driver/request.rb
cts-mpx-1.1.1 lib/cts/mpx/driver/request.rb
cts-mpx-1.1.0 lib/cts/mpx/driver/request.rb