Sha256: d2cb9124a93cc2d647fea9d851d1f40bde0a6c9b0efb7c39e04b7c2babad84a6

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

require 'faraday'
require 'faraday_middleware'

module TN
  module HTTP
    ClientError = Class.new(Faraday::ClientError)

    # TODO: remove this once this gets merged into Faraday
    Faraday::Adapter::NetHttp::NET_HTTP_EXCEPTIONS << Zlib::BufError
    Faraday::Adapter::NetHttp::NET_HTTP_EXCEPTIONS << Errno::EPIPE

    class WrapError < Faraday::Middleware
      def call(env)
        @app.call(env)
      rescue Faraday::ClientError => e
        raise ClientError, e
      end
    end

    def self.default_connection(*arguments, adapter: nil)
      Faraday.new(*arguments) do |conn|
        conn.use TN::HTTP::WrapError
        conn.use Faraday::Response::RaiseError
        yield conn if block_given?
        conn.adapter(adapter || Faraday.default_adapter)
      end
    end

    def self.form_connection(*arguments, adapter: nil)
      default_connection(*arguments, adapter: adapter) do |conn|
        conn.request  :url_encoded
        yield conn if block_given?
      end
    end

    def self.default_json_connection(*arguments, adapter: nil)
      default_connection(*arguments, adapter: adapter) do |conn|
        conn.response :mashify
        conn.response :json
        yield conn if block_given?
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dino_utils-0.1.21 lib/tn/http.rb
dino_utils-0.1.20 lib/tn/http.rb
dino_utils-0.1.19 lib/tn/http.rb