Class: Rig::HTTPBody

Inherits:
Array
  • Object
show all
Defined in:
lib/rig/http_body.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ HTTPBody

Returns a new instance of HTTPBody.



4
5
6
7
8
9
10
11
12
13
# File 'lib/rig/http_body.rb', line 4

def initialize options
  @options      = options[:body] || {}
  @http_method  = options[:http_method]

  if multipart?
    create_multipart_body
  else
    create_simple_body
  end
end

Instance Method Details

#boundary ⇒ Object



31
32
33
# File 'lib/rig/http_body.rb', line 31

def boundary
  @boundary ||= "----rigHTTPmultipart#{rand(2**32)}XZWCFOOBAR"
end

#content_length ⇒ Object



15
16
17
# File 'lib/rig/http_body.rb', line 15

def content_length
  join.bytes.to_a.length
end

#content_type ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rig/http_body.rb', line 19

def content_type
  if multipart?
    "multipart/form-data; boundary=#{boundary}"
  else
    if %w(POST PUT).include?( @http_method )
      "application/x-www-form-urlencoded; charset=UTF-8"
    else
      "text/plain"
    end
  end
end

#create_multipart_body ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rig/http_body.rb', line 51

def create_multipart_body
  @options.each do |key, value|
    if value.respond_to?( :read )
      push new_file_multipart( key, value )
    elsif value.is_a?( String ) || value.respond_to?( :to_s )
      push new_text_multipart( key, value )
    else
      raise ArgumentError, "Invalid Parameter Value"
    end
  end

  push "--#{boundary}--\r\n"
end

#create_simple_body ⇒ Object



47
48
49
# File 'lib/rig/http_body.rb', line 47

def create_simple_body
  push @options.map {|key, value| "#{key}=#{value}"}.join("&")
end

#multipart? ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rig/http_body.rb', line 35

def multipart?
  if defined? @multipart
    @multipart
  elsif @options
    @multipart = @options.values.any? do |element|
      element.respond_to?( :read )
    end
  else
    @multipart = false
  end
end

#new_file_multipart(field_name, file) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rig/http_body.rb', line 76

def new_file_multipart field_name, file
  content_type = %x[file --mime-type -b #{file.path}].chomp

  part = ""
  part += "--#{boundary}"
  part += CRLF
  part += "Content-Disposition: form-data; name=\"#{field_name}\"; "
  part += "filename=\"#{File.basename( file )}\""
  part += CRLF
  part += "Content-Type: #{content_type}"
  part += CRLF
  part += CRLF
  part += file.read
  file.close
  part += CRLF
end

#new_text_multipart(field_name, text) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/rig/http_body.rb', line 65

def new_text_multipart field_name, text
  part = ""
  part += "--#{boundary}"
  part += CRLF
  part += "Content-Disposition: form-data; name=\"#{field_name}\""
  part += CRLF
  part += CRLF
  part += text
  part += CRLF
end

#to_s ⇒ Object



93
94
95
# File 'lib/rig/http_body.rb', line 93

def to_s
  join
end