Class: Handsoap::Http::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/handsoap/http/part.rb

Overview

Represents a HTTP Part. For simple HTTP-requests there is only one part, which is the response.

Direct Known Subclasses

Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, body, parts = nil) ⇒ Part

Returns a new instance of Part.



11
12
13
14
15
# File 'lib/handsoap/http/part.rb', line 11

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/handsoap/http/part.rb', line 9

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/handsoap/http/part.rb', line 9

def headers
  @headers
end

#partsObject (readonly)

Returns the value of attribute parts.



9
10
11
# File 'lib/handsoap/http/part.rb', line 9

def parts
  @parts
end

Instance Method Details

#[](key) ⇒ Object

Returns a header. Returns String | Array | nil



19
20
21
22
# File 'lib/handsoap/http/part.rb', line 19

def [](key)
  key.to_s.downcase!
  (@headers[key] && @headers[key].length == 1) ? @headers[key].first : @headers[key]
end

#charsetObject

Returns the charset part of the content-type header



30
31
32
33
34
35
36
37
# File 'lib/handsoap/http/part.rb', line 30

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

#inspect(&block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/handsoap/http/part.rb', line 43

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

#mime_typeObject

Returns the mime-type part of the content-type header



25
26
27
# File 'lib/handsoap/http/part.rb', line 25

def mime_type
  @headers['content-type'].first.match(/^[^;]+/).to_s if @headers['content-type']
end

#multipart?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/handsoap/http/part.rb', line 39

def multipart?
  !! @parts
end