Sha256: 7871f675c82c8fc9a68feb29eeaa014ba898febfede4ed712c3011940db4845c

Contents?: true

Size: 1023 Bytes

Versions: 1

Compression:

Stored size: 1023 Bytes

Contents

# frozen_string_literal: true

module QiwiObserver
  class WebhookResponse < Response
    attr_reader :error, :value
    def initialize(success:, body:)
      @success = success
      if @success
        @value = parse_body(body)
      else
        @error = error_description(body)
      end
    end

    def success?
      @success
    end

    private

    def parse_body(body)
      return parse_hash(body) if body.is_a?(Hash)
      return parse_json(body) if body.is_a?(String)
    end

    def error_description(body)
      return "This ##{body} transaction is not authenticated" if body.is_a?(String)
      return "Error #{body.first} #{body.last}" if body.is_a?(Array)
    end

    def parse_hash(body)
      if body[:test] != true
        output = body[:payment]
        output.reject { |key, _val| key == :signFields }.to_h
      else
        body
      end
    end

    def parse_json(body)
      output = JSON.parse(body).reduce({}) {|result, (key, value)| result.merge({key.to_sym => value})}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qiwi_observer-0.2.1 lib/qiwi_observer/webhook/webhook_response.rb