Class: QingCloudServer::Client::Connector
- Inherits:
-
Object
- Object
- QingCloudServer::Client::Connector
- Defined in:
- lib/client/connector.rb
Constant Summary collapse
- APIAddress =
"https://api.qingcloud.com/iaas/?"
Instance Attribute Summary collapse
-
#public_key ⇒ Object
Returns the value of attribute public_key.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch_server_condition(action, params) ⇒ Object
-
#initialize(public_key, secret_key) ⇒ Connector
constructor
A new instance of Connector.
Constructor Details
#initialize(public_key, secret_key) ⇒ Connector
Returns a new instance of Connector.
14 15 16 17 |
# File 'lib/client/connector.rb', line 14 def initialize(public_key, secret_key) @public_key = public_key @secret_key = secret_key end |
Instance Attribute Details
#public_key ⇒ Object
Returns the value of attribute public_key.
12 13 14 |
# File 'lib/client/connector.rb', line 12 def public_key @public_key end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
13 14 15 |
# File 'lib/client/connector.rb', line 13 def secret_key @secret_key end |
Class Method Details
.init(public_key, secret_key) ⇒ Object
19 20 21 |
# File 'lib/client/connector.rb', line 19 def self.init(public_key, secret_key) Connector.new(public_key, secret_key) end |
.init_with_config ⇒ Object
23 24 25 26 |
# File 'lib/client/connector.rb', line 23 def self.init_with_config config_hash_file = Utility.read_config_file Connector.new(config_hash_file["public_key"], config_hash_file["private_key"]) end |
Instance Method Details
#fetch_server_condition(action, params) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/client/connector.rb', line 28 def fetch_server_condition(action, params) params.update( :action => action, :time_stamp => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"), :access_key_id => self.public_key, :version => 1, :signature_method => "HmacSHA256", :signature_version => 1 ) request_body = params.sort.map do |k, v| # 这里的k是symbol,要先转化成为string,然后再使用 if k.to_s.include?("_N") key_part = k.to_s.split("_")[0] v.map {|e| "#{CGI.escape(key_part)}.#{v.index(e) + 1}=#{CGI.escape(e)}"} else "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" end end.join("&") puts "request_body #{request_body.inspect}" request_url = "GET\n/iaas/\n#{request_body}" sha_256_digest = OpenSSL::Digest.new("sha256") signature = OpenSSL::HMAC.digest(sha_256_digest, self.secret_key, request_url) signature = Base64.encode64(signature).strip puts "signature is #{signature}" uri = URI("#{APIAddress}#{request_body}&signature=#{CGI.escape(signature)}") # Net::HTTP.start(uri.host, uri.port, :user_ssl => uri.scheme == "https") do |http| # req = Net::HTTP::Get.new(uri) # res = http.request(req) # end response_body = Net::HTTP.get_response(uri).body end |