module Ethon::Easy::Queryable
def self.included(base)
def self.included(base) base.send(:attr_accessor, :escape) base.send(:attr_accessor, :params_encoding) end
def build_query_pairs(hash)
-
(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) return [hash] if hash.is_a?(String) pairs = [] recursively_generate_pairs(hash, nil, pairs) pairs end
def empty?
-
(Boolean)
- True if params is empty, else false.
Other tags:
- Example: Return if params is empty. -
def empty? @params.empty? end
def encode_hash_pairs(h, prefix, pairs)
def encode_hash_pairs(h, prefix, pairs) h.each_pair do |k,v| key = prefix.nil? ? k : "#{prefix}[#{k}]" pairs_for(v, key, pairs) end end
def encode_indexed_array_pairs(h, prefix, pairs)
def encode_indexed_array_pairs(h, prefix, pairs) h.each_with_index do |v, i| key = "#{prefix}[#{i}]" pairs_for(v, key, pairs) end end
def encode_multi_array_pairs(h, prefix, pairs)
def encode_multi_array_pairs(h, prefix, pairs) h.each_with_index do |v, i| key = prefix pairs_for(v, key, pairs) end end
def encode_rack_array_pairs(h, prefix, pairs)
def encode_rack_array_pairs(h, prefix, pairs) h.each do |v| key = "#{prefix}[]" pairs_for(v, key, pairs) end end
def file_info(file)
-
(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) [ filename, mime_type(filename), File.expand_path(file.path) ] end
def mime_type(filename)
def mime_type(filename) if defined?(MIME) && t = MIME::Types.type_for(filename).first t.to_s else 'application/octet-stream' end end
def pairs_for(v, key, pairs)
def pairs_for(v, key, pairs) case v when Hash, Array recursively_generate_pairs(v, key, pairs) when File, Tempfile pairs << [key, file_info(v)] else pairs << [key, v] end end
def query_pairs
-
(Array)
- The query pairs.
Other tags:
- Example: Return the query pairs. -
def query_pairs @query_pairs ||= build_query_pairs(@params) end
def recursively_generate_pairs(h, prefix, pairs)
def recursively_generate_pairs(h, prefix, pairs) case h when Hash encode_hash_pairs(h, prefix, pairs) when Array if params_encoding == :rack encode_rack_array_pairs(h, prefix, pairs) elsif params_encoding == :multi encode_multi_array_pairs(h, prefix, pairs) elsif params_encoding == :none pairs << [prefix, h] else encode_indexed_array_pairs(h, prefix, pairs) end end end
def to_s
-
(String)
- The string representation.
Other tags:
- Example: Return string representation. -
def to_s @to_s ||= query_pairs.map{ |pair| return pair if pair.is_a?(String) if escape && @easy pair.map{ |e| @easy.escape(e.to_s) }.join("=") else pair.join("=") end }.join('&') end