Sha256: 0c037e93abd75a32ec383db499eefe57819bb9f6db1765171bce539133ea8d6a

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

# typed: false
# frozen_string_literal: true

module Vigiles
  module Archive
    class Response < T::Struct
      const :rack_response, Rack::Response
      const :content_type, String
      const :headers,      Types::Headers
      const :payload,      Types::Payload
      const :status,       Integer

      sig { params(rack_response: Rack::Response).returns(Types::Payload) }
      private_class_method def self.extract_payload(rack_response)
        case (body = rack_response.body)
        when Array
          return { body: :empty_no_content } if body.empty?

          { __false_body: :not_empty_handle_later }
        when Rack::BodyProxy
          body_proxy = body
          body_proxy = body_proxy.instance_variable_get(:@body) until body_proxy.is_a?(Array)
          begin
            JSON.parse(body_proxy[0])
          rescue StandardError
            { __false_body: body_proxy[0] }
          end
        else
          { __false_body: :unknown_response_payload_type }
        end
      end

      sig { params(res: Rack::Response).returns(Response) }
      def self.from(res)
        Response.new(
          rack_response: res,
          content_type: res.headers["Content-Type"] || "unknown_content_type",
          headers: res.headers.as_json,
          payload: extract_payload(res),
          status: res.status
        )
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vigiles-0.1.3 lib/vigiles/archive/response.rb
vigiles-0.1.2 lib/vigiles/archive/response.rb