module RSolr::HTTPClient::Util
def build_param(k,v)
Example:
converts a key value pair to an escaped string:
def build_param(k,v) "#{escape(k)}=#{escape(v)}" end
def build_url(url='', params={}, string_query='')
"string_query" is an extra query string that will be appended to the
"params" is an optional hash of GET style query params
"url" is the base url
creates and returns a url as a string
def build_url(url='', params={}, string_query='') queries = [string_query, hash_to_query(params)] queries.delete_if{|i| i.to_s.empty?} url += "?#{queries.join('&')}" unless queries.empty? url end
def escape(s)
query strings faster. Use this rather than the cgi.rb
Performs URI escaping so that you can construct proper
def escape(s) s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) { '%'+$1.unpack('H2'*$1.size).join('%').upcase }.tr(' ', '+') end
def hash_to_query(params)
?q=blah&fq=blah&fq=blah&facet.field=location_facet&facet.field=format.facet
returns:
hash_to_query(:q=>'blah', :fq=>['blah', 'blah'], :facet=>{:field=>['location_facet', 'format_facet']})
if a value is an array, the array values get mapped to the same key:
converts hash into URL query string, keys get an alpha sort
def hash_to_query(params) params.map { |k, v| if v.class == Array hash_to_query(v.map { |x| [k, x] }) else build_param k, v end }.join("&") end