Module: AliyunSDK

Defined in:
lib/rpc_client.rb

Overview

The AliyunSDK module

Defined Under Namespace

Classes: RPCClient

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Class Method Details

.canonicalize(normalized) ⇒ Object



37
38
39
# File 'lib/rpc_client.rb', line 37

def self.canonicalize(normalized)
  normalized.map { |element| "#{element.first}=#{element.last}" }.join('&')
end

.encode(string) ⇒ Object



48
49
50
51
# File 'lib/rpc_client.rb', line 48

def self.encode(string)
  encoded = CGI.escape string
  encoded.gsub(/[\+]/, '%20')
end

.flat_params(params) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rpc_client.rb', line 25

def self.flat_params(params)
  target = {}
  params.each do |key, value|
    if value.instance_of?(Array)
      replace_repeat_list(target, key, value)
    else
      target[key.to_s] = value
    end
  end
  target
end

.normalize(params) ⇒ Object



41
42
43
44
45
46
# File 'lib/rpc_client.rb', line 41

def self.normalize(params)
  flat_params(params)
    .sort
    .to_h
    .map { |key, value| [encode(key), encode(value)] }
end

.replace_repeat_list(target, key, repeat) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rpc_client.rb', line 12

def self.replace_repeat_list(target, key, repeat)
  repeat.each_with_index do |item, index|
    if item&.instance_of?(Hash)
      item.each_key do |k|
        target["#{key}.#{index.next}.#{k}"] = item[k].to_s
      end
    else
      target["#{key}.#{index.next}"] = item.to_s
    end
  end
  target
end