Class: AliyunOpenSearch::Base
- Inherits:
-
Object
- Object
- AliyunOpenSearch::Base
show all
- Defined in:
- lib/aliyun_open_search/base.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
12
13
14
15
16
|
# File 'lib/aliyun_open_search/base.rb', line 12
def initialize
@basic_params = Base.basic_params
@base_url = "#{ENV["OPEN_SEARCH_HOST"]}/search"
end
|
Class Attribute Details
.request_method ⇒ Object
Returns the value of attribute request_method.
9
10
11
|
# File 'lib/aliyun_open_search/base.rb', line 9
def request_method
@request_method
end
|
Instance Attribute Details
#base_url ⇒ Object
Returns the value of attribute base_url.
6
7
8
|
# File 'lib/aliyun_open_search/base.rb', line 6
def base_url
@base_url
end
|
#basic_params ⇒ Object
Returns the value of attribute basic_params.
6
7
8
|
# File 'lib/aliyun_open_search/base.rb', line 6
def basic_params
@basic_params
end
|
Class Method Details
.basic_params ⇒ Object
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/aliyun_open_search/base.rb', line 54
def self.basic_params
{
"Version" => "v2",
"AccessKeyId" => ENV.fetch("ACCESS_KEY_ID"),
"SignatureMethod" => "HMAC-SHA1",
"Timestamp" => Time.now.utc.iso8601,
"SignatureVersion" => "1.0",
"SignatureNonce" => signature_nonce
}
end
|
.escape(str) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/aliyun_open_search/base.rb', line 82
def self.escape(str)
CGI.escape(str).gsub(/\!/, "%21")
.gsub(/\'/, "%27")
.gsub(/\(/, "%28")
.gsub(/\)/, "%29")
.gsub(/\*/, "%2A")
.gsub(/\+/, "%20")
.gsub(/%7E/, "~")
end
|
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/aliyun_open_search/base.rb', line 70
def self.format_params(method = :get, params)
{}.tap do |hash|
params.map do |key, value|
hash[key.to_s] = if value.is_a?(Array)
method == :get ? value.join("&&") : JSON.generate(value)
else
value.to_s
end
end
end
end
|
.signature(params) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/aliyun_open_search/base.rb', line 33
def self.signature(params)
params = params.sort_by { |k, _v| k.to_s }
.map do |arr|
str = if arr[1].is_a?(String) || arr[1].is_a?(Fixnum)
arr[1].to_s
else
arr[1].to_json
end
"#{arr[0]}=#{Base.escape(str)}"
end.join("&")
Base64.encode64(
OpenSSL::HMAC.digest(
OpenSSL::Digest.new("sha1"),
"#{ENV["ACCESS_KEY_SECRET"]}&",
request_method + "&" + CGI.escape("/") + "&" + Base.escape(params)
)
).strip
end
|
.signature_nonce ⇒ Object
65
66
67
68
|
# File 'lib/aliyun_open_search/base.rb', line 65
def self.signature_nonce
(Time.now.to_f.round(3) * 1000).to_i.to_s + (1000..9999).to_a.sample.to_s
end
|
Instance Method Details
#uri(special_base_url = nil, params) ⇒ Object
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/aliyun_open_search/base.rb', line 18
def uri(special_base_url = nil, params)
encoded_params = params.inject([]) do |arr, (k, v)|
arr << "#{k}=#{Base.escape(v)}"
end.join("&")
url = (special_base_url || base_url) + "?" + encoded_params
Rails.logger.info url if defined?(Rails)
URI(url)
end
|