qcloud_ruby 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e915b09303dd0d98a6e458390648e58926933cf
4
+ data.tar.gz: bd881c7c31a9e75d775125cce700a72659c59d4b
5
+ SHA512:
6
+ metadata.gz: 475d18739db7712ee554e5bc6567069d366e0aad16c3dd23c019775291799a736636a5714ed522b29cd6ac0a7b13b060d81797a3deb5ee0eab3761d3d8a08f3a
7
+ data.tar.gz: 6ef8a00d4ea1551d6e6ae7a79381b664ab2163023c7caa1bd3d1b3318fb1b88fc9fc8ecaf73b1a58c7fad9aae63915f9a191b307522c8b3def33e9f1e74b6794
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in qcloud_ruby.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 aiasfina
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ An unofficial Tencent Qcloud API SDK for Ruby
2
+ ==================================
3
+
4
+ 安装
5
+ ----
6
+
7
+ ```bash
8
+ gem install qcloud_ruby
9
+ ```
10
+
11
+ 使用
12
+ ----
13
+
14
+ 1. 使用之前请在腾讯云的[云 API 控制台](https://console.qcloud.com/capi)中创建自己的安全凭证(SecretId 和 SecretKey). SecretKey 要严格保管, 避免泄露.
15
+ 2. 安装并引入本 SDK.
16
+
17
+ 示例
18
+ ----
19
+
20
+ ```ruby
21
+ ::QcloudRuby.configure do |config|
22
+ config.secret_id = 'Secret id'
23
+ config.secret_key = 'Secret key'
24
+ end
25
+
26
+ resp = ::QcloudRuby.post(
27
+ service_type: ::QcloudRuby::ServiceType::ACCOUNT,
28
+ region: 'gz',
29
+ action: 'DescribeProject'
30
+ )
31
+
32
+ resp = ::QcloudRuby.get(
33
+ service_type: ::QcloudRuby::ServiceType::CVM,
34
+ region: 'gz',
35
+ action: 'DescribeInstances'
36
+ )
37
+ ```
38
+
39
+ SDK API
40
+ -------
41
+
42
+ 请访问 [api.md](./api.md)
43
+
44
+ 资源
45
+ ----
46
+
47
+ - [API 列表](https://www.qcloud.com/doc/api)
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "qcloud_ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ module QcloudRuby
2
+ class << self
3
+ attr_accessor :configuration
4
+ end
5
+
6
+ def self.configure
7
+ @configuration ||= ::QcloudRuby::Configuration.new
8
+ yield @configuration
9
+ end
10
+
11
+ def self.get(service_type: nil, **params)
12
+ client = ::QcloudRuby::Client.new do |c|
13
+ c.service_type = service_type
14
+ end
15
+
16
+ client.request(method: 'GET', **params)
17
+ end
18
+
19
+ def self.post(service_type: nil, **params)
20
+ client = ::QcloudRuby::Client.new do |c|
21
+ c.service_type = service_type
22
+ end
23
+
24
+ client.request(method: 'POST', **params)
25
+ end
26
+ end
27
+
28
+ require "qcloud_ruby/service_type"
29
+ require "qcloud_ruby/configuration"
30
+ require "qcloud_ruby/client"
31
+ require "qcloud_ruby/version"
@@ -0,0 +1,89 @@
1
+ require 'forwardable'
2
+ require 'uri'
3
+ require 'openssl'
4
+ require 'base64'
5
+ require 'net/http'
6
+
7
+ module QcloudRuby
8
+ class Client
9
+ extend Forwardable
10
+
11
+ attr_accessor :service_type
12
+
13
+ def initialize(&block)
14
+ self.instance_eval(&block)
15
+ end
16
+
17
+ def default_params(region, action)
18
+ {
19
+ Region: region,
20
+ Action: action,
21
+ SecretId: secret_id,
22
+ Timestamp: timestamp,
23
+ Nonce: nonce,
24
+ RequestClient: identity
25
+ }
26
+ end
27
+
28
+ def gen_data(method, region, action, other_params)
29
+ params = default_params(region, action)
30
+ .merge!(other_params)
31
+ .sort
32
+ .to_h
33
+
34
+ query_str = URI.encode_www_form(params)
35
+
36
+ params.merge!(Signature: sign(method, query_str))
37
+ end
38
+
39
+ def request(method: 'POST', region: nil, action: nil, **other_params)
40
+ data = gen_data(method, region, action, other_params)
41
+ uri = URI(url_with_protocol)
42
+
43
+ resp = if method == 'GET'
44
+ uri.query = URI.encode_www_form(data)
45
+ Net::HTTP.get_response(uri)
46
+ else
47
+ Net::HTTP.post_form(uri, data)
48
+ end
49
+
50
+ resp
51
+ end
52
+
53
+ def timestamp
54
+ Time.now.to_i
55
+ end
56
+
57
+ def nonce
58
+ (rand() * 65535).round
59
+ end
60
+
61
+ def identity
62
+ "SDK_RUBY_#{::QcloudRuby::VERSION}"
63
+ end
64
+
65
+ def host
66
+ "#{service_type}.#{base_host}"
67
+ end
68
+
69
+ def url
70
+ "#{host}#{path}"
71
+ end
72
+
73
+ def url_with_protocol
74
+ "#{protocol}://#{url}"
75
+ end
76
+
77
+ def sign(method, query)
78
+ source = method + url + '?' + query
79
+
80
+ Base64.encode64(OpenSSL::HMAC.digest(
81
+ OpenSSL::Digest.new('sha1'),
82
+ secret_key, source))
83
+ .strip
84
+ end
85
+
86
+ def_delegators :'QcloudRuby.configuration',
87
+ :protocol, :secret_id, :secret_key, :base_host, :path
88
+ end
89
+ end
@@ -0,0 +1,20 @@
1
+ require 'digest/sha1'
2
+ require 'base64'
3
+
4
+ module QcloudRuby
5
+ class Configuration
6
+ attr_accessor :base_host, :ssl, :path, :secret_id, :secret_key
7
+
8
+ def initialize
9
+ @base_host = 'api.qcloud.com'
10
+ @path = '/v2/index.php'
11
+ @ssl = true
12
+ end
13
+
14
+ def protocol
15
+ ssl? ? 'https' : 'http'
16
+ end
17
+
18
+ alias_method :ssl?, :ssl
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module QcloudRuby
2
+ module ServiceType
3
+ CVM = 'cvm'.freeze
4
+ VPC = 'vpc'.freeze
5
+ DFW = 'dfw'.freeze
6
+ LB = 'lb'.freeze
7
+ ACCOUNT = 'account'.freeze
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module QcloudRuby
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'qcloud_ruby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "qcloud_ruby"
8
+ spec.version = QcloudRuby::VERSION
9
+ spec.authors = ["aiasfina"]
10
+ spec.email = ["aiasfina@gmail.com"]
11
+
12
+ spec.summary = %q{An unofficial Tencent Qcloud API SDK for Ruby.}
13
+ spec.description = %q{An unofficial Tencent Qcloud API SDK for Ruby.}
14
+ spec.homepage = "https://github.com/aiasfina/qcloud_ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.14"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ class QcloudRubyTest < Minitest::Test
5
+ def setup
6
+ ::QcloudRuby.configure do |config|
7
+ config.secret_id = 'Secret id'
8
+ config.secret_key = 'Secret key'
9
+ end
10
+ end
11
+
12
+ def test_url
13
+ client = ::QcloudRuby::Client.new do |c|
14
+ c.service_type = ::QcloudRuby::ServiceType::CVM
15
+ end
16
+
17
+ assert_equal 'cvm.api.qcloud.com/v2/index.php', client.url
18
+ end
19
+
20
+ def test_post_should_be_work
21
+ resp = ::QcloudRuby.post(
22
+ service_type: ::QcloudRuby::ServiceType::ACCOUNT,
23
+ region: 'gz',
24
+ action: 'DescribeProject'
25
+ )
26
+
27
+ assert_equal 0, JSON.parse(resp.body).fetch('code')
28
+ end
29
+
30
+ def test_get_should_be_work
31
+ resp = ::QcloudRuby.get(
32
+ service_type: ::QcloudRuby::ServiceType::CVM,
33
+ region: 'gz',
34
+ action: 'DescribeInstances'
35
+ )
36
+
37
+ assert_equal 0, JSON.parse(resp.body).fetch('code')
38
+ end
39
+
40
+ def test_with_unknow_region
41
+ resp = ::QcloudRuby.post(
42
+ service_type: ::QcloudRuby::ServiceType::CVM,
43
+ region: 'unknow',
44
+ action: 'DescribeInstances'
45
+ )
46
+
47
+ refute_equal 0, JSON.parse(resp.body).fetch('code')
48
+ end
49
+
50
+ def test_with_unknow_service_type
51
+ resp = ::QcloudRuby.post(
52
+ service_type: ::QcloudRuby::ServiceType::LB,
53
+ region: 'gz',
54
+ action: 'DescribeInstances'
55
+ )
56
+
57
+ refute_equal 0, JSON.parse(resp.body).fetch('code')
58
+ end
59
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'qcloud_ruby'
3
+
4
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qcloud_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - aiasfina
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: An unofficial Tencent Qcloud API SDK for Ruby.
56
+ email:
57
+ - aiasfina@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/qcloud_ruby.rb
71
+ - lib/qcloud_ruby/client.rb
72
+ - lib/qcloud_ruby/configuration.rb
73
+ - lib/qcloud_ruby/service_type.rb
74
+ - lib/qcloud_ruby/version.rb
75
+ - qcloud_ruby.gemspec
76
+ - test/qcloud_ruby_test.rb
77
+ - test/test_helper.rb
78
+ homepage: https://github.com/aiasfina/qcloud_ruby
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.11
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: An unofficial Tencent Qcloud API SDK for Ruby.
102
+ test_files: []