Sha256: 789af992d64e42a7b4b8e0b97001696af8092778a2d020bbf55cd4e9d03824b7

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

# 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

1 entries across 1 versions & 1 rubygems

Version Path
clean-architecture-2.0.0 lib/clean_architecture/serializers/json_response_from_result.rb