Module: RestClient::Payload

Extended by:
Payload
Included in:
Payload
Defined in:
lib/restclient/payload.rb

Defined Under Namespace

Classes: Base, Multipart, Streamed, UrlEncoded

Instance Method Summary collapse

Instance Method Details

#_has_file?(obj) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/restclient/payload.rb', line 48

def _has_file?(obj)
  case obj
  when Hash, ParamsArray
    obj.any? {|_, v| _has_file?(v) }
  when Array
    obj.any? {|v| _has_file?(v) }
  else
    obj.respond_to?(:path) && obj.respond_to?(:read)
  end
end

#generate(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/restclient/payload.rb', line 16

def generate(params)
  if params.is_a?(RestClient::Payload::Base)
    # pass through Payload objects unchanged
    params
  elsif params.is_a?(String)
    Base.new(params)
  elsif params.is_a?(Hash)
    if params.delete(:multipart) == true || has_file?(params)
      Multipart.new(params)
    else
      UrlEncoded.new(params)
    end
  elsif params.is_a?(ParamsArray)
    if _has_file?(params)
      Multipart.new(params)
    else
      UrlEncoded.new(params)
    end
  elsif params.respond_to?(:read)
    Streamed.new(params)
  else
    nil
  end
end

#has_file?(params) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/restclient/payload.rb', line 41

def has_file?(params)
  unless params.is_a?(Hash)
    raise ArgumentError.new("Must pass Hash, not #{params.inspect}")
  end
  _has_file?(params)
end