module Ethon::Easies::Util

def self.escape_zero_byte(value)

Returns:
  • (String, Object) - Escaped String if

Parameters:
  • value (Object) -- The value to escape.

Other tags:
    Example: Escape zero bytes. -
def self.escape_zero_byte(value)
  return value unless value.to_s.include?(0.chr)
  value.to_s.gsub(0.chr, '\\\0')
end

def build_query_pairs(hash)

Returns:
  • (Array) - The array of query pairs.

Parameters:
  • hash (Hash) -- The hash to go through.

Other tags:
    Example: Build query pairs. -
def build_query_pairs(hash)
  pairs = []
  recursive = Proc.new do |h, prefix|
    h.each_pair do |k,v|
      key = prefix == '' ? k : "#{prefix}[#{k}]"
      case v
      when Hash
        recursive.call(v, key)
      when Array
        v.each { |x| pairs << [key, x]  }
      when File, Tempfile
        pairs << [Util.escape_zero_byte(key), file_info(v)]
      else
        pairs << [Util.escape_zero_byte(key), Util.escape_zero_byte(v)]
      end
    end
  end
  recursive.call(hash, '')
  pairs
end

def file_info(file)

Returns:
  • (Array) - Array of informations.

Parameters:
  • file (File) -- The file to handle.

Other tags:
    Example: Return file info. -
def file_info(file)
  filename = File.basename(file.path)
  types = MIME::Types.type_for(filename)
  [
    filename,
    types.empty? ? 'application/octet-stream' : types[0].to_s,
    File.expand_path(file.path)
  ]
end