Sha256: b4e75494e560af3c27392dadf3437456481a39c6079d2a4b38029382bdcbbf5a

Contents?: true

Size: 1.19 KB

Versions: 15

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require "forwardable"

module HTTPX::Transcoder
  module Body
    Error = Class.new(HTTPX::Error)

    module_function

    class Encoder
      extend Forwardable

      def_delegator :@raw, :to_s

      def initialize(body)
        @raw = body
      end

      def bytesize
        if @raw.respond_to?(:bytesize)
          @raw.bytesize
        elsif @raw.respond_to?(:to_ary)
          @raw.map(&:bytesize).reduce(0, :+)
        elsif @raw.respond_to?(:size)
          @raw.size || Float::INFINITY
        elsif @raw.respond_to?(:length)
          @raw.length || Float::INFINITY
        elsif @raw.respond_to?(:each)
          Float::INFINITY
        else
          raise Error, "cannot determine size of body: #{@raw.inspect}"
        end
      end

      def content_type
        "application/octet-stream"
      end

      private

      def respond_to_missing?(meth, *args)
        @raw.respond_to?(meth, *args) || super
      end

      def method_missing(meth, *args, &block)
        return super unless @raw.respond_to?(meth)

        @raw.__send__(meth, *args, &block)
      end
    end

    def encode(body)
      Encoder.new(body)
    end
  end
  register "body", Body
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
httpx-0.15.4 lib/httpx/transcoder/body.rb
httpx-0.15.3 lib/httpx/transcoder/body.rb
httpx-0.15.2 lib/httpx/transcoder/body.rb
httpx-0.15.1 lib/httpx/transcoder/body.rb
httpx-0.15.0 lib/httpx/transcoder/body.rb
httpx-0.14.5 lib/httpx/transcoder/body.rb
httpx-0.14.4 lib/httpx/transcoder/body.rb
httpx-0.14.3 lib/httpx/transcoder/body.rb
httpx-0.14.2 lib/httpx/transcoder/body.rb
httpx-0.14.1 lib/httpx/transcoder/body.rb
httpx-0.14.0 lib/httpx/transcoder/body.rb
httpx-0.13.2 lib/httpx/transcoder/body.rb
httpx-0.13.1 lib/httpx/transcoder/body.rb
httpx-0.13.0 lib/httpx/transcoder/body.rb
httpx-0.12.0 lib/httpx/transcoder/body.rb