Sha256: e0bdfb305cd7c11c8589e3fcd4d100296574c5fec8711e961fb9338e14d154a5

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Pwush
  module Response
    class Deffered
      include Dry::Monads::Result::Mixin

      def initialize(raw_result)
        @_raw_result = raw_result
      end

      def resolve
        return http_request_failure if raw_result.status != 200
        return api_request_failure if value_from_api.status_code != 200

        api_request_succesful
      end

      private

      def http_request_failure
        Failure.new(value_from_http)
      end

      def api_request_failure
        Failure.new(value_from_api)
      end

      def api_request_succesful
        Success.new(value_from_api)
      end

      def raw_result
        @_raw_result
      end

      def parsed_result
        @_parsed_result ||= raw_result.parse
      end

      def value_from_http
        @_value_from_http ||= Value.new(
          status_code: raw_result.status,
          status_message: raw_result.reason,
          body: nil
        )
      end

      def value_from_api
        @_value_from_api ||= Value.new(
          status_code: parsed_result['status_code'],
          status_message: parsed_result['status_message'],
          body: parsed_result['response']
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pwush-0.4.0 lib/pwush/response/deffered.rb