Sha256: db78606a84cf901c9e776626418be57fd8335a3496d5fc3b53b7d884edf7cc7b

Contents?: true

Size: 1.94 KB

Versions: 17

Compression:

Stored size: 1.94 KB

Contents

# -*- coding: utf-8 -*-

module Handsoap
  module Http

    # Represents a HTTP Part.
    # For simple HTTP-requests there is only one part, which is the response.
    class Part
      attr_reader :headers, :body, :parts

      def initialize(headers, body, parts = nil)
        @headers = headers
        @body = body
        @parts = parts
      end

      # Returns a header.
      # Returns String | Array | nil
      def [](key)
        key.to_s.downcase!
        (@headers[key] && @headers[key].length == 1) ? @headers[key].first : @headers[key]
      end

      # Returns the mime-type part of the content-type header
      def mime_type
        @headers['content-type'].first.match(/^[^;]+/).to_s if @headers['content-type']
      end

      # Returns the charset part of the content-type header
      def charset
        if @headers['content-type']
          match_data = @headers['content-type'].first.match(/^[^;]+; charset=([^;]+)/)
          if match_data
            match_data[1].to_s
          end
        end
      end

      def multipart?
        !! @parts
      end

      def inspect(&block)
        str = inspect_head
        if headers.any?
          str << headers.map { |key,values| values.map {|value| normalize_header_key(key) + ": " + value + "\n" }.join("")  }.join("")
        end
        if body
          if multipart?
            if block_given?
              str << parts.map{|part| part.inspect(&block) }.join("")
            else
              str << parts.map{|part| part.inspect }.join("")
            end
          elsif body
            str <<  "---\n"
            if block_given?
              str << yield(body)
            else
              str << body
            end
            str << "\n---"
          end
        end
      end
      
    private
      
      def inspect_head
        "--- Part ---\n"
      end

      def normalize_header_key(key)
        key.split("-").map{|s| s.downcase.capitalize }.join("-")
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
handsoap-1.4.0 lib/handsoap/http/part.rb
handsoap-1.3.0 lib/handsoap/http/part.rb
handsoap-1.2.2 lib/handsoap/http/part.rb
handsoap-1.1.8 lib/handsoap/http/part.rb
handsoap-1.1.7 lib/handsoap/http/part.rb
pgericson-handsoap-1.1.10 lib/handsoap/http/part.rb
pgericson-handsoap-1.1.9 lib/handsoap/http/part.rb
pgericson-handsoap-1.1.8 lib/handsoap/http/part.rb
pgericson-handsoap-1.1.7 lib/handsoap/http/part.rb
handsoap-1.1.6 lib/handsoap/http/part.rb
handsoap-1.1.5 lib/handsoap/http/part.rb
whenyouneedme-handsoap-1.1.4 lib/handsoap/http/part.rb
handsoap-1.1.4 lib/handsoap/http/part.rb
handsoap-1.1.3 lib/handsoap/http/part.rb
handsoap-1.1.2 lib/handsoap/http/part.rb
handsoap-1.1.1 lib/handsoap/http/part.rb
handsoap-1.1.0 lib/handsoap/http/part.rb