Class: Faraday::Request::Multipart

Inherits:
UrlEncoded show all
Defined in:
lib/faraday/request/multipart.rb

Overview

Middleware for supporting multi-part requests.

Constant Summary collapse

DEFAULT_BOUNDARY_PREFIX =
'-----------RubyMultipartPost'

Constants inherited from UrlEncoded

UrlEncoded::CONTENT_TYPE

Instance Attribute Summary

Attributes inherited from Middleware

#app, #options

Attributes included from DependencyLoader

#load_error

Instance Method Summary collapse

Methods inherited from UrlEncoded

#match_content_type, #request_type

Methods inherited from Middleware

#close

Methods included from DependencyLoader

#dependency, #inherited, #loaded?, #new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware, #unregister_middleware

Constructor Details

#initialize(app = nil, options = {}) ⇒ Multipart

Returns a new instance of Multipart.


15
16
17
18
# File 'lib/faraday/request/multipart.rb', line 15

def initialize(app = nil, options = {})
  super(app)
  @options = options
end

Instance Method Details

#call(env) ⇒ Object

Checks for files in the payload, otherwise leaves everything untouched.

Parameters:


23
24
25
26
27
28
29
30
31
# File 'lib/faraday/request/multipart.rb', line 23

def call(env)
  match_content_type(env) do |params|
    env.request.boundary ||= unique_boundary
    env.request_headers[CONTENT_TYPE] +=
      "; boundary=#{env.request.boundary}"
    env.body = create_multipart(env, params)
  end
  @app.call env
end

#create_multipart(env, params) ⇒ Object

Parameters:


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/faraday/request/multipart.rb', line 57

def create_multipart(env, params)
  boundary = env.request.boundary
  parts = process_params(params) do |key, value|
    part(boundary, key, value)
  end
  parts << Faraday::Parts::EpiloguePart.new(boundary)

  body = Faraday::CompositeReadIO.new(parts)
  env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
  body
end

#has_multipart?(obj) ⇒ Boolean

Returns true if obj is an enumerable with values that are multipart.

Parameters:

  • obj (Object)

Returns:

  • (Boolean)

46
47
48
49
50
51
52
53
# File 'lib/faraday/request/multipart.rb', line 46

def has_multipart?(obj) # rubocop:disable Naming/PredicateName
  if obj.respond_to?(:each)
    (obj.respond_to?(:values) ? obj.values : obj).each do |val|
      return true if val.respond_to?(:content_type) || has_multipart?(val)
    end
  end
  false
end

#part(boundary, key, value) ⇒ Object


69
70
71
72
73
74
75
# File 'lib/faraday/request/multipart.rb', line 69

def part(boundary, key, value)
  if value.respond_to?(:to_part)
    value.to_part(boundary, key)
  else
    Faraday::Parts::Part.new(boundary, key, value)
  end
end

#process_params(params, prefix = nil, pieces = nil, &block) ⇒ Object

Parameters:

  • params (Hash)
  • prefix (String) (defaults to: nil)
  • pieces (Array) (defaults to: nil)

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/faraday/request/multipart.rb', line 85

def process_params(params, prefix = nil, pieces = nil, &block)
  params.inject(pieces || []) do |all, (key, value)|
    if prefix
      key = @options[:flat_encode] ? prefix.to_s : "#{prefix}[#{key}]"
    end

    case value
    when Array
      values = value.inject([]) { |a, v| a << [nil, v] }
      process_params(values, key, all, &block)
    when Hash
      process_params(value, key, all, &block)
    else
      # rubocop:disable Performance/RedundantBlockCall
      all << block.call(key, value)
      # rubocop:enable Performance/RedundantBlockCall
    end
  end
end

#process_request?(env) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

34
35
36
37
38
39
40
# File 'lib/faraday/request/multipart.rb', line 34

def process_request?(env)
  type = request_type(env)
  env.body.respond_to?(:each_key) && !env.body.empty? && (
    (type.empty? && has_multipart?(env.body)) ||
      (type == self.class.mime_type)
  )
end

#unique_boundaryString

Returns:

  • (String)

78
79
80
# File 'lib/faraday/request/multipart.rb', line 78

def unique_boundary
  "#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
end