Sha256: 1b719fc0b39ec5d62a79c7d724c668002844a62cb7c49f07053e71158c0840e1

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require 'excon'
require 'forwardable'
require 'peddler/errors/class_generator'
require 'peddler/errors/parser'

module Peddler
  module Errors
    # @!visibility private
    class Builder
      extend Forwardable

      DIGIT_AS_FIRST_CHAR = /^\d/.freeze
      private_constant :DIGIT_AS_FIRST_CHAR

      def_delegator :error, :response

      def self.call(error)
        new(error).build
      end

      attr_reader :error

      def initialize(error)
        @error = error
      end

      def build
        parse_original_response
        return if no_error_response?
        return if bad_class_name?

        error_class.new(error_message, error)
      end

      private

      def bad_class_name?
        error_name.match?(DIGIT_AS_FIRST_CHAR)
      end

      def no_error_response?
        response.parse.nil?
      end

      def error_class
        ClassGenerator.call(error_name)
      end

      def error_name
        response.code
      end

      def error_message
        response.message
      end

      def parse_original_response
        error.instance_variable_set :@response, Parser.new(error.response)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
peddler-2.4.5 lib/peddler/errors/builder.rb
peddler-2.4.4 lib/peddler/errors/builder.rb
peddler-2.4.3 lib/peddler/errors/builder.rb
peddler-2.4.1 lib/peddler/errors/builder.rb
peddler-2.4.0 lib/peddler/errors/builder.rb
peddler-2.3.0 lib/peddler/errors/builder.rb