Sha256: d385ace8fd3c42ee32da95c45e0a4a560d123e410bfbef9bc718e2501b611c87

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Plugins
    #
    # This plugin adds support for passing `http-form_data` objects (like file objects) as "multipart/form-data";
    #
    #   HTTPX.post(URL, form: form: { image: HTTP::FormData::File.new("path/to/file")})
    #
    module Multipart
      module FormTranscoder
        module_function

        class Encoder
          extend Forwardable

          def_delegator :@raw, :content_type

          def_delegator :@raw, :to_s

          def_delegator :@raw, :read

          def initialize(form)
            @raw = HTTP::FormData.create(form)
          end

          def bytesize
            @raw.content_length
          end

          def force_encoding(*args)
            @raw.to_s.force_encoding(*args)
          end

          def to_str
            @raw.to_s
          end
        end

        def encode(form)
          Encoder.new(form)
        end
      end

      def self.load_dependencies(*)
        require "http/form_data"
      end

      def self.configure(*)
        Transcoder.register("form", FormTranscoder)
      end
    end
    register_plugin :multipart, Multipart
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
httpx-0.4.0 lib/httpx/plugins/multipart.rb