class Ethon::Easy::Form

This class represents a form and is used to send a payload in the request body via POST/PUT. It handles multipart forms, too.

@api private

Public Class Methods

finalizer(form) click to toggle source

Frees form in libcurl if necessary.

@example Free the form

Form.finalizer(form)

@param [ Form ] form The form to free.

# File lib/ethon/easy/form.rb, line 36
def self.finalizer(form)
  proc { Curl.formfree(form.first) if form.multipart? }
end
new(easy, params) click to toggle source

Return a new Form.

@example Return a new Form.

Form.new({})

@param [ Hash ] params The parameter to initialize the form with.

@return [ Form ] A new Form.

# File lib/ethon/easy/form.rb, line 24
def initialize(easy, params)
  @easy = easy
  @params = params || {}
  ObjectSpace.define_finalizer(self, self.class.finalizer(self))
end

Public Instance Methods

first() click to toggle source

Return a pointer to the first form element in libcurl.

@example Return the first form element.

form.first

@return [ FFI::Pointer ] The first element.

# File lib/ethon/easy/form.rb, line 46
def first
  @first ||= FFI::MemoryPointer.new(:pointer)
end
last() click to toggle source

Return a pointer to the last form element in libcurl.

@example Return the last form element.

form.last

@return [ FFI::Pointer ] The last element.

# File lib/ethon/easy/form.rb, line 56
def last
  @last ||= FFI::MemoryPointer.new(:pointer)
end
materialize() click to toggle source

Add form elements to libcurl.

@example Add form to libcurl.

form.materialize
# File lib/ethon/easy/form.rb, line 75
def materialize
  query_pairs.each { |pair| form_add(pair.first.to_s, pair.last) }
end
multipart?() click to toggle source

Return if form is multipart. The form is multipart, when it contains a file.

@example Return if form is multipart.

form.multipart?

@return [ Boolean ] True if form is multipart, else false.

# File lib/ethon/easy/form.rb, line 67
def multipart?
  query_pairs.any?{|pair| pair.respond_to?(:last) && pair.last.is_a?(Array)}
end

Private Instance Methods

form_add(name, content) click to toggle source
# File lib/ethon/easy/form.rb, line 81
def form_add(name, content)
  case content
  when Array
    Curl.formadd(first, last,
                 :form_option, :copyname, :pointer, name,
                 :form_option, :namelength, :long, name.bytesize,
                 :form_option, :file, :string, content[2],
                 :form_option, :filename, :string, content[0],
                 :form_option, :contenttype, :string, content[1],
                 :form_option, :end
                )
  else
    Curl.formadd(first, last,
                 :form_option, :copyname, :pointer, name,
                 :form_option, :namelength, :long, name.bytesize,
                 :form_option, :copycontents, :pointer, content,
                 :form_option, :contentslength, :long, content ? content.bytesize : 0,
                 :form_option, :end
                )
  end
end