Class: Ethon::Easies::Form

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#build_query_pairs, escape_zero_byte, #file_info

Constructor Details

#initialize(params) ⇒ Form

Return a new Form.

Examples:

Return a new Form.

Form.new({})

Parameters:

  • params (Hash)

    The parameter to initialize the form with.



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

#escapeObject

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.

Examples:

Free the form

Form.finalizer(form)

Parameters:

  • form (Form)

    The form to free.



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.

Examples:

Return if form is empty.

form.empty?

Returns:

  • (Boolean)

    True if form is empty, else false.



94
95
96
# File 'lib/ethon/easies/form.rb', line 94

def empty?
  @params.empty?
end

#firstFFI::Pointer

Return a pointer to the first form element in libcurl.

Examples:

Return the first form element.

form.first

Returns:

  • (FFI::Pointer)

    The first element.



42
43
44
# File 'lib/ethon/easies/form.rb', line 42

def first
  @first ||= FFI::MemoryPointer.new(:pointer)
end

#lastFFI::Pointer

Return a pointer to the last form element in libcurl.

Examples:

Return the last form element.

form.last

Returns:

  • (FFI::Pointer)

    The last element.



52
53
54
# File 'lib/ethon/easies/form.rb', line 52

def last
  @last ||= FFI::MemoryPointer.new(:pointer)
end

#materializeObject

Add form elements to libcurl.

Examples:

Add form to libcurl.

form.materialize


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.

Examples:

Return if form is multipart.

form.multipart?

Returns:

  • (Boolean)

    True if form is multipart, else false.



63
64
65
# File 'lib/ethon/easies/form.rb', line 63

def multipart?
  query_pairs.any?{|pair| pair.last.is_a?(Array)}
end

#query_pairsArray

Return the query pairs.

Examples:

Return the query pairs.

form.query_pairs

Returns:

  • (Array)

    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_sString

Return the string representation of the form. This makes only sense when the form is not multipart.

Examples:

Return string representation.

form.to_s

Returns:

  • (String)

    The string representation.



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