Class: QcloudRuby::Client
- Inherits:
-
Object
- Object
- QcloudRuby::Client
- Extended by:
- Forwardable
- Defined in:
- lib/qcloud_ruby/client.rb
Instance Attribute Summary collapse
-
#service_type ⇒ Object
Returns the value of attribute service_type.
Instance Method Summary collapse
- #default_params(region, action) ⇒ Object
- #gen_data(method, region, action, other_params) ⇒ Object
- #host ⇒ Object
- #identity ⇒ Object
-
#initialize(&block) ⇒ Client
constructor
A new instance of Client.
- #nonce ⇒ Object
- #request(method: 'POST', region: nil, action: nil, **other_params) ⇒ Object
- #sign(method, query) ⇒ Object
- #timestamp ⇒ Object
- #url ⇒ Object
- #url_with_protocol ⇒ Object
Constructor Details
#initialize(&block) ⇒ Client
Returns a new instance of Client.
13 14 15 |
# File 'lib/qcloud_ruby/client.rb', line 13 def initialize(&block) self.instance_eval(&block) end |
Instance Attribute Details
#service_type ⇒ Object
Returns the value of attribute service_type.
11 12 13 |
# File 'lib/qcloud_ruby/client.rb', line 11 def service_type @service_type end |
Instance Method Details
#default_params(region, action) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/qcloud_ruby/client.rb', line 17 def default_params(region, action) { Region: region, Action: action, SecretId: secret_id, Timestamp: , Nonce: nonce, RequestClient: identity } end |
#gen_data(method, region, action, other_params) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/qcloud_ruby/client.rb', line 28 def gen_data(method, region, action, other_params) params = default_params(region, action) .merge!(other_params) .sort .to_h query_str = URI.encode_www_form(params) params.merge!(Signature: sign(method, query_str)) end |
#host ⇒ Object
65 66 67 |
# File 'lib/qcloud_ruby/client.rb', line 65 def host "#{service_type}.#{base_host}" end |
#identity ⇒ Object
61 62 63 |
# File 'lib/qcloud_ruby/client.rb', line 61 def identity "SDK_RUBY_#{::QcloudRuby::VERSION}" end |
#nonce ⇒ Object
57 58 59 |
# File 'lib/qcloud_ruby/client.rb', line 57 def nonce (rand() * 65535).round end |
#request(method: 'POST', region: nil, action: nil, **other_params) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/qcloud_ruby/client.rb', line 39 def request(method: 'POST', region: nil, action: nil, **other_params) data = gen_data(method, region, action, other_params) uri = URI(url_with_protocol) resp = if method == 'GET' uri.query = URI.encode_www_form(data) Net::HTTP.get_response(uri) else Net::HTTP.post_form(uri, data) end resp end |
#sign(method, query) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/qcloud_ruby/client.rb', line 77 def sign(method, query) source = method + url + '?' + query Base64.encode64(OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha1'), secret_key, source)) .strip end |
#timestamp ⇒ Object
53 54 55 |
# File 'lib/qcloud_ruby/client.rb', line 53 def Time.now.to_i end |
#url ⇒ Object
69 70 71 |
# File 'lib/qcloud_ruby/client.rb', line 69 def url "#{host}#{path}" end |
#url_with_protocol ⇒ Object
73 74 75 |
# File 'lib/qcloud_ruby/client.rb', line 73 def url_with_protocol "#{protocol}://#{url}" end |