Class: Ethon::Easy::Form
- Inherits:
-
Object
- Object
- Ethon::Easy::Form
- Defined in:
- lib/ethon/easy/form.rb
Overview
This class represents a form and is used to send a payload in the request body via POST/PUT. It handles multipart forms, too.
Class Method Summary collapse
-
.finalizer(form) ⇒ Object
Frees form in libcurl if necessary.
Instance Method Summary collapse
-
#first ⇒ FFI::Pointer
Return a pointer to the first form element in libcurl.
-
#initialize(easy, params) ⇒ Form
constructor
Return a new Form.
-
#last ⇒ FFI::Pointer
Return a pointer to the last form element in libcurl.
-
#materialize ⇒ Object
Add form elements to libcurl.
-
#multipart? ⇒ Boolean
Return if form is multipart.
Methods included from Queryable
#build_query_pairs, #empty?, #file_info, included, #query_pairs, #to_s
Methods included from Util
Constructor Details
#initialize(easy, params) ⇒ Form
Return a new Form.
22 23 24 25 26 |
# File 'lib/ethon/easy/form.rb', line 22 def initialize(easy, params) @easy = easy @params = params || {} ObjectSpace.define_finalizer(self, self.class.finalizer(self)) end |
Class Method Details
.finalizer(form) ⇒ Object
Frees form in libcurl if necessary.
34 35 36 |
# File 'lib/ethon/easy/form.rb', line 34 def self.finalizer(form) proc { Curl.formfree(form.first) if form.multipart? } end |
Instance Method Details
#first ⇒ FFI::Pointer
Return a pointer to the first form element in libcurl.
44 45 46 |
# File 'lib/ethon/easy/form.rb', line 44 def first @first ||= FFI::MemoryPointer.new(:pointer) end |
#last ⇒ FFI::Pointer
Return a pointer to the last form element in libcurl.
54 55 56 |
# File 'lib/ethon/easy/form.rb', line 54 def last @last ||= FFI::MemoryPointer.new(:pointer) end |
#materialize ⇒ Object
Add form elements to libcurl.
73 74 75 |
# File 'lib/ethon/easy/form.rb', line 73 def materialize query_pairs.each { |pair| form_add(pair.first.to_s, pair.last) } end |
#multipart? ⇒ Boolean
Return if form is multipart. The form is multipart, when it contains a file.
65 66 67 |
# File 'lib/ethon/easy/form.rb', line 65 def multipart? query_pairs.any?{|pair| pair.respond_to?(:last) && pair.last.is_a?(Array)} end |