Class: Larrow::Qingcloud::Connection
- Inherits:
-
Object
- Object
- Larrow::Qingcloud::Connection
- Includes:
- Logger
- Defined in:
- lib/larrow/qingcloud/connection.rb
Overview
Connection delegator for Qingcloud
Constant Summary collapse
- URL_TEMPLATE =
'https://api.qingcloud.com/iaas/?%s&signature=%s'
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#zone_id ⇒ Object
Returns the value of attribute zone_id.
Instance Method Summary collapse
- #get(action, params = {}) ⇒ Object
-
#initialize(access_key, secret_key, zone_id) ⇒ Connection
constructor
A new instance of Connection.
- #service(method, action, params = {}) ⇒ Object
Methods included from Logger
Constructor Details
#initialize(access_key, secret_key, zone_id) ⇒ Connection
Returns a new instance of Connection.
15 16 17 18 19 |
# File 'lib/larrow/qingcloud/connection.rb', line 15 def initialize(access_key, secret_key, zone_id) self.access_key = access_key self.secret_key = secret_key self.zone_id = zone_id end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
13 14 15 |
# File 'lib/larrow/qingcloud/connection.rb', line 13 def access_key @access_key end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
13 14 15 |
# File 'lib/larrow/qingcloud/connection.rb', line 13 def secret_key @secret_key end |
#zone_id ⇒ Object
Returns the value of attribute zone_id.
13 14 15 |
# File 'lib/larrow/qingcloud/connection.rb', line 13 def zone_id @zone_id end |
Instance Method Details
#get(action, params = {}) ⇒ Object
56 57 58 |
# File 'lib/larrow/qingcloud/connection.rb', line 56 def get action, params = {} service 'get', action, params end |
#service(method, action, params = {}) ⇒ Object
21 22 23 24 25 26 27 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 |
# File 'lib/larrow/qingcloud/connection.rb', line 21 def service(method, action, params = {}) # Time.new.iso8601 cannot be recognized time_stamp = Time.new.utc.strftime '%Y-%m-%dT%TZ' params.update( zone: zone_id, action: action, time_stamp: time_stamp, access_key_id: access_key, version: 1, signature_method: 'HmacSHA256', signature_version: 1 ) request_str = params.keys.sort.map do |k| "#{CGI.escape k.to_s}=#{CGI.escape params[k].to_s}" end.join('&') signed_text = format "%s\n/iaas/\n%s", method.upcase, request_str signature = Base64.encode64(OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha256'), secret_key||'', signed_text )).strip url = format URL_TEMPLATE, request_str, CGI.escape(signature) resp = Faraday.send(method.to_sym, url) debug "API #{action} #{request_str}" JSON.parse(resp.body).tap do |obj| if obj['ret_code'] != 0 debug "Service Error(#{obj['ret_code']}): #{obj['message']}" fail ServiceError, obj['message'] end end end |