Sha256: 6cd58f536bf9e0a87d5b4b7b6f920f56589a5caf1ae51877d6576b8662d0b235

Contents?: true

Size: 1.17 KB

Versions: 17

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require "http"

module Miteru
  module HTTP
    #
    # Better error handling feature
    #
    class BetterError < ::HTTP::Feature
      def wrap_response(response)
        return response if response.status.success?

        raise StatusError.new(
          "Unsuccessful response code returned: #{response.code}",
          response.code,
          response.body.to_s
        )
      end

      ::HTTP::Options.register_feature(:better_error, self)
    end

    #
    # HTTP client factory
    #
    class Factory
      class << self
        USER_AGENT = "miteru/#{Miteru::VERSION}".freeze

        #
        # @param [Integer, nil] timeout
        # @param [Hash] headers
        # @param [Boolean] raise_exception
        #
        # @return [::HTTP::Client]
        #
        # @param [Object] raise_exception
        def build(headers: {}, timeout: nil, raise_exception: true)
          client = raise_exception ? ::HTTP.use(:better_error) : ::HTTP

          headers["User-Agent"] ||= USER_AGENT

          client = client.headers(headers)
          client = client.timeout(timeout) unless timeout.nil?
          client
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
miteru-3.0.1 lib/miteru/http.rb
miteru-3.0.0 lib/miteru/http.rb
miteru-2.4.0 lib/miteru/http.rb
miteru-2.3.2 lib/miteru/http.rb
miteru-2.3.1 lib/miteru/http.rb
miteru-2.3.0 lib/miteru/http.rb
miteru-2.2.4 lib/miteru/http.rb
miteru-2.2.3 lib/miteru/http.rb
miteru-2.2.2 lib/miteru/http.rb
miteru-2.2.1 lib/miteru/http.rb
miteru-2.2.0 lib/miteru/http.rb
miteru-2.1.1 lib/miteru/http.rb
miteru-2.1.0 lib/miteru/http.rb
miteru-2.0.3 lib/miteru/http.rb
miteru-2.0.2 lib/miteru/http.rb
miteru-2.0.1 lib/miteru/http.rb
miteru-2.0.0 lib/miteru/http.rb