Class: RSolr::Connection::NetHttp
- Inherits:
-
Object
- Object
- RSolr::Connection::NetHttp
show all
- Includes:
- Requestable
- Defined in:
- lib/rsolr/connection/net_http.rb
Overview
Connection for standard HTTP Solr server
Instance Attribute Summary
Attributes included from Requestable
#opts, #uri
Instance Method Summary
collapse
-
#add_signature(http_method, path, params) ⇒ Object
-
#build_url(path, params = {}) ⇒ Object
accepts a path/string and optional hash of query params.
-
#connection ⇒ Object
-
#create_base_string(method, path, params) ⇒ Object
-
#create_http_context(net_http_response, url, path, params, data = nil, headers = {}) ⇒ Object
-
#create_signature(base_string, consumer_secret, token_secret = "") ⇒ Object
-
#escape(value) ⇒ Object
-
#get(path, params = {}) ⇒ Object
-
#normalize_params(params) ⇒ Object
-
#post(path, data, params = {}, headers = {}) ⇒ Object
#initialize, #request
Methods included from Utils
#build_param, #bytesize, #encode_utf8, #hash_to_query
Instance Method Details
#add_signature(http_method, path, params) ⇒ Object
92
93
94
95
96
|
# File 'lib/rsolr/connection/net_http.rb', line 92
def add_signature(http_method, path, params)
base_string = create_base_string(http_method, path, params)
signature = create_signature(base_string, ENV["oauth_secret"])
params.merge!("oauth_signature" => signature)
end
|
#build_url(path, params = {}) ⇒ Object
accepts a path/string and optional hash of query params
46
47
48
49
|
# File 'lib/rsolr/connection/net_http.rb', line 46
def build_url path, params={}
full_path = @uri.path + path
super full_path, params, @uri.query
end
|
#connection ⇒ Object
11
12
13
|
# File 'lib/rsolr/connection/net_http.rb', line 11
def connection
@connection ||= Net::HTTP.new(@uri.host, @uri.port)
end
|
#create_base_string(method, path, params) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/rsolr/connection/net_http.rb', line 67
def create_base_string(method, path, params)
url = "#{@uri.scheme}://#{@uri.host}"
port = @uri.port.to_s
if !port.blank?
if port != "80"
url += ":#{port}"
end
end
url += @uri.path + path
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_http_context(net_http_response, url, path, params, data = nil, headers = {}) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/rsolr/connection/net_http.rb', line 29
def create_http_context net_http_response, url, path, params, data=nil, ={}
full_url = "#{@uri.scheme}://#{@uri.host}"
full_url += @uri.port ? ":#{@uri.port}" : ''
full_url += url
{
:status_code=>net_http_response.code.to_i,
:url=>full_url,
:body=> encode_utf8(net_http_response.body),
:path=>path,
:params=>params,
:data=>data,
:headers=>,
:message => net_http_response.message
}
end
|
#create_signature(base_string, consumer_secret, token_secret = "") ⇒ Object
83
84
85
86
|
# File 'lib/rsolr/connection/net_http.rb', line 83
def create_signature(base_string, consumer_secret, token_secret="")
secret = "#{escape(consumer_secret)}&#{escape(token_secret)}"
Base64.encode64(HMAC::SHA1.digest(secret, base_string)).chomp.gsub(/\n/,'')
end
|
#escape(value) ⇒ Object
88
89
90
|
# File 'lib/rsolr/connection/net_http.rb', line 88
def escape(value)
CGI.escape(value.to_s).gsub("%7E", '~').gsub("+", "%20")
end
|
#get(path, params = {}) ⇒ Object
15
16
17
18
19
|
# File 'lib/rsolr/connection/net_http.rb', line 15
def get path, params={}
url = self.build_url path, params
net_http_response = self.connection.get url
create_http_context net_http_response, url, path, params
end
|
#normalize_params(params) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/rsolr/connection/net_http.rb', line 51
def normalize_params(params)
return_string = ""
params.keys.sort{|x,y| x.to_s <=> y.to_s}.each do |k|
v = params[k]
if v.class == Array
v.sort.each {|v| return_string.concat("#{k.to_s}=#{v}&")}
else
return_string.concat("#{k.to_s}=#{v}&")
end
end
if !return_string.blank?
return_string.chop!
end
return_string
end
|
#post(path, data, params = {}, headers = {}) ⇒ Object
21
22
23
24
25
26
27
|
# File 'lib/rsolr/connection/net_http.rb', line 21
def post path, data, params={}, ={}
add_signature("POST", path, params)
url = self.build_url path, params
net_http_response = self.connection.post url, data,
create_http_context net_http_response, url, path, params, data,
end
|