Sha256: 68acf5ec7c6ae847b9c63552575aa74828bc23c087bb22c2b90a4b45b797d947

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

# typed: true
# frozen_string_literal: true

require 'clean_architecture/entities/failure_details'
require 'clean_architecture/matchers/use_case_result'
require 'clean_architecture/queries/http_failure_code'
require 'clean_architecture/queries/http_success_code'

module CleanArchitecture
  module Serializers
    class JsonResponseFromResult
      def initialize(result, http_method, success_payload)
        @result = result
        @http_method = http_method
        @success_payload = success_payload
      end

      def to_h
        {
          status: http_status_code,
          json: json
        }
      end

      private

      def http_status_code
        Matchers::UseCaseResult.call(@result) do |matcher|
          matcher.success { Queries::HttpSuccessCode.new(@http_method).to_sym }
          matcher.failure do |failure_value|
            Queries::HttpFailureCode.new(failure_value.type).to_sym
          end
        end
      end

      def json
        Matchers::UseCaseResult.call(@result) do |matcher|
          matcher.success { success_payload }
          matcher.failure do |failure_details|
            { jsonapi: json_api_version_hash, errors: [failure_details.message] }
          end
        end
      end

      def success_payload
        {
          jsonapi: json_api_version_hash,
          data: @success_payload.data
        }
      end

      def json_api_version_hash
        { version: @success_payload.version }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
clean-architecture-3.0.2 lib/clean_architecture/serializers/json_response_from_result.rb
clean-architecture-3.0.1 lib/clean_architecture/serializers/json_response_from_result.rb
clean-architecture-3.0.0 lib/clean_architecture/serializers/json_response_from_result.rb