/usr/lib/ruby/vendor_ruby/ethon/easy/queryable.rb is in ruby-ethon 0.7.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | module Ethon
class Easy
# This module contains logic about building
# query parameters for url or form.
module Queryable
# :nodoc:
def self.included(base)
base.send(:attr_accessor, :escape)
end
# Return wether there are elements in params or not.
#
# @example Return if params is empty.
# form.empty?
#
# @return [ Boolean ] True if params is empty, else false.
def empty?
@params.empty?
end
# Return the string representation of params.
#
# @example Return string representation.
# params.to_s
#
# @return [ String ] The 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
# Return the query pairs.
#
# @example Return the query pairs.
# params.query_pairs
#
# @return [ Array ] The query pairs.
def query_pairs
@query_pairs ||= build_query_pairs(@params)
end
# Return query pairs build from a hash.
#
# @example Build query pairs.
# action.build_query_pairs({a: 1, b: 2})
# #=> [[:a, 1], [:b, 2]]
#
# @param [ Hash ] hash The hash to go through.
#
# @return [ Array ] The array of query pairs.
def build_query_pairs(hash)
return [hash] if hash.is_a?(String)
pairs = []
recursively_generate_pairs(hash, nil, pairs)
pairs
end
# Return file info for a file.
#
# @example Return file info.
# action.file_info(File.open('fubar', 'r'))
#
# @param [ File ] file The file to handle.
#
# @return [ Array ] Array of informations.
def file_info(file)
filename = File.basename(file.path)
[
filename,
mime_type(filename),
File.expand_path(file.path)
]
end
private
def mime_type(filename)
if defined?(MIME) && t = MIME::Types.type_for(filename).first
t.to_s
else
'application/octet-stream'
end
end
def recursively_generate_pairs(h, prefix, pairs)
case h
when Hash
h.each_pair do |k,v|
key = prefix.nil? ? k : "#{prefix}[#{k}]"
pairs_for(v, key, pairs)
end
when Array
h.each_with_index do |v, i|
key = "#{prefix}[#{i}]"
pairs_for(v, key, pairs)
end
end
end
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
end
end
end
|