Module: RSolr::Uri

Extended by:
Uri
Included in:
Uri
Defined in:
lib/rsolr/uri.rb

Instance Method Summary collapse

Instance Method Details

#build_param(k, v, escape = true) ⇒ Object

Returns a query string param pair as a string. Both key and value are escaped.



11
12
13
14
15
# File 'lib/rsolr/uri.rb', line 11

def build_param(k,v, escape = true)
  escape ? 
    "#{escape_query_value(k)}=#{escape_query_value(v)}" :
    "#{k}=#{v}"
end

#bytesize(string) ⇒ Object



20
21
22
# File 'lib/rsolr/uri.rb', line 20

def bytesize(string)
  string.bytesize
end

#create(url) ⇒ Object



5
6
7
# File 'lib/rsolr/uri.rb', line 5

def create url
  ::URI.parse url[-1] == ?/ ? url : "#{url}/"
end

#escape_query_value(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).



50
51
52
53
54
# File 'lib/rsolr/uri.rb', line 50

def escape_query_value(s)
  s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/u) {
    '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
  }.tr(' ', '+')
end

#params_to_solr(params, escape = true) ⇒ Object

Creates a Solr based query string. Keys that have arrays values are set multiple times:

params_to_solr(:q => 'query', :fq => ['a', 'b'])

is converted to:

?q=query&fq=a&fq=b


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rsolr/uri.rb', line 34

def params_to_solr(params, escape = true)
  mapped = params.map do |k, v|
    next if v.to_s.empty?
    if v.class == Array
      params_to_solr(v.map { |x| [k, x] }, escape)
    else
      build_param k, v, escape
    end
  end
  mapped.compact.join("&")
end