Module: HTTPX::Plugins::Multipart::FormTranscoder

Defined in:
lib/httpx/plugins/multipart.rb

Class Method Summary collapse

Class Method Details

.decode(response) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/httpx/plugins/multipart.rb', line 60

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

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

.encode(form) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/httpx/plugins/multipart.rb', line 52

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

.multipart?(data) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/httpx/plugins/multipart.rb', line 73

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