Class: Handsoap::Http::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/handsoap/http.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.



55
56
57
58
59
# File 'lib/handsoap/http.rb', line 55

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



54
55
56
# File 'lib/handsoap/http.rb', line 54

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



54
55
56
# File 'lib/handsoap/http.rb', line 54

def headers
  @headers
end

#partsObject (readonly)

Returns the value of attribute parts.



54
55
56
# File 'lib/handsoap/http.rb', line 54

def parts
  @parts
end

Instance Method Details

#[](key) ⇒ Object

Returns a header. Returns String | Array | nil



62
63
64
65
# File 'lib/handsoap/http.rb', line 62

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



71
72
73
74
75
76
77
78
# File 'lib/handsoap/http.rb', line 71

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/handsoap/http.rb', line 82

def inspect(&block)
  str = inspect_head
  if headers.any?
    str << headers.map { |key,values| values.map {|value| Handsoap::Http.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



67
68
69
# File 'lib/handsoap/http.rb', line 67

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

#multipart?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/handsoap/http.rb', line 79

def multipart?
  !! @parts
end