Module: Ethon::Easy::Queryable
Overview
This module contains logic about building query parameters for url or form.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#build_query_pairs(hash) ⇒ Array
Return query pairs build from a hash.
-
#empty? ⇒ Boolean
Return wether there are elements in the form or not.
-
#file_info(file) ⇒ Array
Return file info for a file.
-
#query_pairs ⇒ Array
Return the query pairs.
-
#to_s ⇒ String
Return the string representation of params.
Class Method Details
.included(base) ⇒ Object
:nodoc:
9 10 11 |
# File 'lib/ethon/easy/queryable.rb', line 9 def self.included(base) base.send(:attr_accessor, :escape) end |
Instance Method Details
#build_query_pairs(hash) ⇒ Array
Return query pairs build from a hash.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ethon/easy/queryable.rb', line 58 def build_query_pairs(hash) return [hash] if hash.is_a?(String) pairs = [] recursive = Proc.new do |h, prefix| case h when Hash h.each_pair do |k,v| key = prefix == '' ? k : "#{prefix}[#{k}]" pairs_for(v, key, pairs, recursive) end when Array h.each_with_index do |v, i| key = "#{prefix}[#{i}]" pairs_for(v, key, pairs, recursive) end end end recursive.call(hash, '') pairs end |
#empty? ⇒ Boolean
Return wether there are elements in the form or not.
19 20 21 |
# File 'lib/ethon/easy/queryable.rb', line 19 def empty? @params.empty? end |
#file_info(file) ⇒ Array
Return file info for a file.
88 89 90 91 92 93 94 95 96 |
# File 'lib/ethon/easy/queryable.rb', line 88 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.(file.path) ] end |
#query_pairs ⇒ Array
Return the query pairs.
45 46 47 |
# File 'lib/ethon/easy/queryable.rb', line 45 def query_pairs @query_pairs ||= build_query_pairs(@params) end |
#to_s ⇒ String
Return the string representation of params.
29 30 31 32 33 34 35 36 37 |
# File 'lib/ethon/easy/queryable.rb', line 29 def to_s query_pairs.map{ |pair| return pair if pair.is_a?(String) pair.map{ |e| escape && @easy ? @easy.escape(e.to_s) : e }.join("=") }.join('&') end |