Module: RSolr::HTTPClient::Util
- Included in:
- Connection::Adapter::Direct, Connection::Adapter::HTTP, Adapter::Curb, Adapter::NetHTTP
- Defined in:
- lib/rsolr/http_client.rb
Instance Method Summary collapse
-
#build_param(k, v) ⇒ Object
converts a key value pair to an escaped string: Example: build_param(:id, 1) == “id=1”.
-
#build_url(url = '', params = {}, string_query = '') ⇒ Object
creates and returns a url as a string “url” is the base url “params” is an optional hash of GET style query params “string_query” is an extra query string that will be appended to the result of “url” and “params”.
-
#escape(s) ⇒ Object
Performs URI escaping so that you can construct proper query strings faster.
-
#hash_to_query(params) ⇒ Object
converts hash into URL query string, keys get an alpha sort if a value is an array, the array values get mapped to the same key: hash_to_query(:q=>‘blah’, :fq=>[‘blah’, ‘blah’], :facet=>‘format_facet’]) returns: ?q=blah&fq=blah&fq=blah&facet.field=location_facet&facet.field=format.facet.
Instance Method Details
#build_param(k, v) ⇒ Object
converts a key value pair to an escaped string: Example: build_param(:id, 1) == “id=1”
125 126 127 |
# File 'lib/rsolr/http_client.rb', line 125 def build_param(k,v) "#{escape(k)}=#{escape(v)}" end |
#build_url(url = '', params = {}, string_query = '') ⇒ Object
creates and returns a url as a string “url” is the base url “params” is an optional hash of GET style query params “string_query” is an extra query string that will be appended to the result of “url” and “params”.
115 116 117 118 119 120 |
# File 'lib/rsolr/http_client.rb', line 115 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 |
#escape(s) ⇒ Object
Performs URI escaping so that you can construct proper query strings faster. Use this rather than the cgi.rb version since it’s faster. (Stolen from Rack).
104 105 106 107 108 |
# File 'lib/rsolr/http_client.rb', line 104 def escape(s) s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) { '%'+$1.unpack('H2'*$1.size).join('%').upcase }.tr(' ', '+') end |
#hash_to_query(params) ⇒ Object
converts hash into URL query string, keys get an alpha sort if a value is an array, the array values get mapped to the same key:
hash_to_query(:q=>'blah', :fq=>['blah', 'blah'], :facet=>{:field=>['location_facet', 'format_facet']})
returns:
?q=blah&fq=blah&fq=blah&facet.field=location_facet&facet.field=format.facet
if a value is empty/nil etc., the key is not added
137 138 139 140 141 142 143 144 145 |
# File 'lib/rsolr/http_client.rb', line 137 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 |