Module: Coolio::Http::Payload

Extended by:
Payload
Included in:
Payload
Defined in:
lib/cool.io/http/payload.rb

Defined Under Namespace

Classes: Base, Multipart, Streamed, UrlEncoded

Instance Method Summary collapse

Instance Method Details

#generate(params) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cool.io/http/payload.rb', line 12

def generate(params)
  if params.is_a?(String)
    Base.new(params)
  elsif params.respond_to?(:read)
    Streamed.new(params)
  elsif params
    if params.delete(:multipart) == true || has_file?(params)
      Multipart.new(params)
    else
      UrlEncoded.new(params)
    end
  else
    nil
  end
end

#has_file?(params) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cool.io/http/payload.rb', line 28

def has_file?(params)
  params.any? do |_, v|
    case v
      when Hash
        has_file?(v)
      when Array
        has_file_array?(v)
      else
        v.respond_to?(:path) && v.respond_to?(:read)
    end
  end
end

#has_file_array?(params) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cool.io/http/payload.rb', line 41

def has_file_array?(params)
  params.any? do |v|
    case v
      when Hash
        has_file?(v)
      when Array
        has_file_array?(v)
      else
        v.respond_to?(:path) && v.respond_to?(:read)
      end
  end
end