Sha256: b781fbe0a2ea259c3aece97cb37084f2a7bca950850eb4f381a2c3c3dfb66f21

Contents?: true

Size: 1.21 KB

Versions: 21

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

require "forwardable"

module HTTPX::Transcoder
  module Body
    class Error < HTTPX::Error; end

    module_function

    class Encoder
      using HTTPX::ArrayExtensions
      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.sum(&:bytesize)
        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

21 entries across 21 versions & 1 rubygems

Version Path
httpx-0.20.3 lib/httpx/transcoder/body.rb
httpx-0.20.2 lib/httpx/transcoder/body.rb
httpx-0.20.1 lib/httpx/transcoder/body.rb
httpx-0.20.0 lib/httpx/transcoder/body.rb
httpx-0.19.8 lib/httpx/transcoder/body.rb
httpx-0.19.7 lib/httpx/transcoder/body.rb
httpx-0.19.6 lib/httpx/transcoder/body.rb
httpx-0.19.5 lib/httpx/transcoder/body.rb
httpx-0.19.4 lib/httpx/transcoder/body.rb
httpx-0.19.3 lib/httpx/transcoder/body.rb
httpx-0.19.2 lib/httpx/transcoder/body.rb
httpx-0.19.1 lib/httpx/transcoder/body.rb
httpx-0.19.0 lib/httpx/transcoder/body.rb
httpx-0.18.7 lib/httpx/transcoder/body.rb
httpx-0.18.6 lib/httpx/transcoder/body.rb
httpx-0.18.5 lib/httpx/transcoder/body.rb
httpx-0.18.4 lib/httpx/transcoder/body.rb
httpx-0.18.3 lib/httpx/transcoder/body.rb
httpx-0.18.2 lib/httpx/transcoder/body.rb
httpx-0.18.1 lib/httpx/transcoder/body.rb