Class: Merb::Test::MultipartRequestHelper::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-core/test/helpers/multipart_request_helper.rb

Constant Summary collapse

BOUNDARY =
'----------0xKhTmLbOuNdArY'
CONTENT_TYPE =
"multipart/form-data, boundary=" + BOUNDARY

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Post

Parameters

params<Hash>

Optional params for the controller.



50
51
52
53
# File 'lib/merb-core/test/helpers/multipart_request_helper.rb', line 50

def initialize(params = {})
  @multipart_params = []
  push_params(params)
end

Instance Method Details

#push_params(params, prefix = nil) ⇒ Object

Saves the params in an array of multipart params as Param and FileParam objects.

Parameters

params<Hash>

The params to add to the multipart params.

prefix<~to_s>

An optional prefix for the request string keys.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/merb-core/test/helpers/multipart_request_helper.rb', line 61

def push_params(params, prefix = nil)
  params.sort_by {|k| k.to_s}.each do |key, value|
    param_key = prefix.nil? ? key : "#{prefix}[#{key}]"
    if value.respond_to?(:read)
      @multipart_params << FileParam.new(param_key, value.path, value.read)
    else
      if value.is_a?(Hash) || value.is_a?(Mash)
        value.keys.each do |k|
          push_params(value, param_key)
        end
      else
        @multipart_params << Param.new(param_key, value)
      end
    end
  end
end

#to_multipartObject

Returns

Array[String, String]

The query and the content type.



80
81
82
83
# File 'lib/merb-core/test/helpers/multipart_request_helper.rb', line 80

def to_multipart
  query = @multipart_params.collect { |param| "--" + BOUNDARY + "\r\n" + param.to_multipart }.join("") + "--" + BOUNDARY + "--"
  return query, CONTENT_TYPE
end