Sha256: a0e7c02315b4010a3dd54684734581639b8b771b53eccef9473e55b7a5353e0e

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 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

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

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

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

    def self.default_json_connection(*arguments)
      default_connection(*arguments) 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.16 lib/tn/http.rb
dino_utils-0.1.15 lib/tn/http.rb
dino_utils-0.1.14 lib/tn/http.rb