Module: HTTPX::Transcoder::Form

Defined in:
lib/httpx/transcoder/form.rb

Defined Under Namespace

Modules: Decoder Classes: Encoder

Constant Summary collapse

PARAM_DEPTH_LIMIT =
32

Class Method Summary collapse

Class Method Details

.decode(response) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/httpx/transcoder/form.rb', line 56

def decode(response)
  content_type = response.content_type.mime_type

  case content_type
  when "application/x-www-form-urlencoded"
    Decoder
  when "multipart/form-data"
    Multipart::Decoder.new(response)
  else
    raise Error, "invalid form mime type (#{content_type})"
  end
end

.encode(form) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/httpx/transcoder/form.rb', line 48

def encode(form)
  if multipart?(form)
    Multipart::Encoder.new(form)
  else
    Encoder.new(form)
  end
end

.multipart?(data) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/httpx/transcoder/form.rb', line 69

def multipart?(data)
  data.any? do |_, v|
    Multipart::MULTIPART_VALUE_COND.call(v) ||
      (v.respond_to?(:to_ary) && v.to_ary.any?(&Multipart::MULTIPART_VALUE_COND)) ||
      (v.respond_to?(:to_hash) && v.to_hash.any? { |_, e| Multipart::MULTIPART_VALUE_COND.call(e) })
  end
end