Module: RSolr::Connection::Utils

Included in:
Requestable
Defined in:
lib/rsolr/connection/utils.rb

Overview

Helpful utility methods for building queries to a Solr server This includes helpers that the Direct connection can use.

Instance Method Summary collapse

Instance Method Details

#build_param(k, v) ⇒ Object

converts a key value pair to an escaped string: Example: build_param(:id, 1) == “id=1”



49
50
51
# File 'lib/rsolr/connection/utils.rb', line 49

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”.



39
40
41
42
43
44
# File 'lib/rsolr/connection/utils.rb', line 39

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

#bytesize(string) ⇒ Object



25
26
27
# File 'lib/rsolr/connection/utils.rb', line 25

def bytesize(string)
  string.bytesize
end

#create_base_string(method, url, params) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/rsolr/connection/utils.rb', line 89

def create_base_string(method, url, params)
  signature_base = method.upcase
  signature_base.concat("&")
  signature_base.concat(CGI::escape(url))
  signature_base.concat("&")
  signature_base.concat(CGI::escape(normalize_params(params)))
end

#create_signature(base_string, consumer_secret, token_secret = "") ⇒ Object



97
98
99
100
101
# File 'lib/rsolr/connection/utils.rb', line 97

def create_signature(base_string, consumer_secret, token_secret="")
  require "hmac-sha1"
  secret = "#{escape(consumer_secret)}&#{escape(token_secret)}" 
  Base64.encode64(HMAC::SHA1.digest(secret, base_string)).chomp.gsub(/\n/,'') 
end

#encode_utf8(string) ⇒ Object

encodes the string as utf-8 in Ruby 1.9 returns the unaltered string in Ruby 1.8



17
18
19
20
# File 'lib/rsolr/connection/utils.rb', line 17

def encode_utf8 string
  (string.respond_to?(:force_encoding) and string.respond_to?(:encoding)) ?
    string.force_encoding(Encoding::UTF_8) : string
end

#escape(value) ⇒ 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).



8
9
10
11
12
13
# File 'lib/rsolr/connection/utils.rb', line 8

def escape(s)
  s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
    #'%'+$1.unpack('H2'*$1.size).join('%').upcase
    '%'+$1.unpack('H2'*bytesize($1)).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., it is not added



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rsolr/connection/utils.rb', line 61

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

#normalize_params(params) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rsolr/connection/utils.rb', line 73

def normalize_params(params)
  return_string = ""
  params.keys.sort.each do |k|
    v = params[k]
    if v.class == Array
      v.sort.each {|v| return_string.concat("#{k}=#{v}&")}
    else
      return_string.concat("#{k}=#{v}&")
    end 
  end
  if !return_string.blank?
    return_string.chop!
  end
  return_string
end