require'ethon/easies/util'require'ethon/easies/queryable'moduleEthonmoduleEasies# This class represents a form and is used to send a payload in the# request body via POST/PUT.# It handles multipart forms, too.classFormincludeEthon::Easies::UtilincludeEthon::Easies::Queryable# 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.definitialize(params)@params=params||{}ObjectSpace.define_finalizer(self,self.class.finalizer(self))end# Frees form in libcurl if necessary.## @example Free the form# Form.finalizer(form)## @param [ Form ] form The form to free.defself.finalizer(form)proc{Curl.formfree(form.first)ifform.multipart?}end# Return a pointer to the first form element in libcurl.## @example Return the first form element.# form.first## @return [ FFI::Pointer ] The first element.deffirst@first||=FFI::MemoryPointer.new(:pointer)end# Return a pointer to the last form element in libcurl.## @example Return the last form element.# form.last## @return [ FFI::Pointer ] The last element.deflast@last||=FFI::MemoryPointer.new(:pointer)end# 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.defmultipart?query_pairs.any?{|pair|pair.respond_to?(:last)&&pair.last.is_a?(Array)}end# Add form elements to libcurl.## @example Add form to libcurl.# form.materializedefmaterializequery_pairs.each{|pair|form_add(pair.first.to_s,pair.last)}endprivatedefform_add(name,content)casecontentwhenArrayCurl.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)elseCurl.formadd(first,last,:form_option,:copyname,:pointer,name,:form_option,:namelength,:long,name.bytesize,:form_option,:copycontents,:pointer,content,:form_option,:contentslength,:long,content.bytesize,:form_option,:end)endendendendend