Class: Ethon::Easies::Form
- Inherits:
-
Object
- Object
- Ethon::Easies::Form
- Includes:
- Util
- Defined in:
- lib/ethon/easies/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.
Instance Attribute Summary collapse
-
#escape ⇒ Object
Returns the value of attribute escape.
Class Method Summary collapse
-
.finalizer(form) ⇒ Object
Frees form in libcurl if necessary.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Return wether there are elements in the form or not.
-
#first ⇒ FFI::Pointer
Return a pointer to the first form element in libcurl.
-
#initialize(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.
-
#query_pairs ⇒ Array
Return the query pairs.
-
#to_s ⇒ String
Return the string representation of the form.
Methods included from Util
#build_query_pairs, escape_zero_byte, #file_info
Constructor Details
#initialize(params) ⇒ Form
Return a new Form.
21 22 23 24 |
# File 'lib/ethon/easies/form.rb', line 21 def initialize(params) @params = params || {} ObjectSpace.define_finalizer(self, self.class.finalizer(self)) end |
Instance Attribute Details
#escape ⇒ Object
Returns the value of attribute escape.
11 12 13 |
# File 'lib/ethon/easies/form.rb', line 11 def escape @escape end |
Class Method Details
.finalizer(form) ⇒ Object
Frees form in libcurl if necessary.
32 33 34 |
# File 'lib/ethon/easies/form.rb', line 32 def self.finalizer(form) proc { Curl.formfree(form.first) if form.multipart? } end |
Instance Method Details
#empty? ⇒ Boolean
Return wether there are elements in the form or not.
94 95 96 |
# File 'lib/ethon/easies/form.rb', line 94 def empty? @params.empty? end |
#first ⇒ FFI::Pointer
Return a pointer to the first form element in libcurl.
42 43 44 |
# File 'lib/ethon/easies/form.rb', line 42 def first @first ||= FFI::MemoryPointer.new(:pointer) end |
#last ⇒ FFI::Pointer
Return a pointer to the last form element in libcurl.
52 53 54 |
# File 'lib/ethon/easies/form.rb', line 52 def last @last ||= FFI::MemoryPointer.new(:pointer) end |
#materialize ⇒ Object
Add form elements to libcurl.
102 103 104 |
# File 'lib/ethon/easies/form.rb', line 102 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.
63 64 65 |
# File 'lib/ethon/easies/form.rb', line 63 def multipart? query_pairs.any?{|pair| pair.last.is_a?(Array)} end |
#query_pairs ⇒ Array
Return the query pairs.
73 74 75 |
# File 'lib/ethon/easies/form.rb', line 73 def query_pairs @query_pairs ||= build_query_pairs(@params) end |
#to_s ⇒ String
Return the string representation of the form. This makes only sense when the form is not multipart.
84 85 86 |
# File 'lib/ethon/easies/form.rb', line 84 def to_s query_pairs.map{|pair| pair.map{|e| escape ? CGI::escape(e.to_s) : e }.join("=")}.join('&') end |