aliyun-sdk-core 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bcff9e1860909a7be4ef7ae81b3a97459819f943
4
+ data.tar.gz: a398a0d9124742039f45b6a551b36924b5e87ebe
5
+ SHA512:
6
+ metadata.gz: 01050aef81640e7a7b85a356b8c0c7d8fdd255897554c3d455ee292007b9b672e3ac52e062cece9c7261c3142bdf223c7ad45b01489b7ac8bc62c52b782d899e
7
+ data.tar.gz: 25cb854114bd044352bbb4177a5bc0da56927c122382bd2229be977b937f7e0e1c87b712a36f4df2b01decad39a561d3c39d0cef7ae6141969b7fb0620df7aa0
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ .ruby-version
47
+ .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aliyun-api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Strikingly.com
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ # Aliyun API Ruby SDK
2
+
3
+ This is the base class of all Aliyun Ruby SDKs for different services.
4
+
5
+ Currently we support:
6
+ - [Aliyun CDN API](https://github.com/strikingly/aliyun-api-cdn)
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aliyun_sdk/core/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.name = "aliyun-sdk-core"
9
+ spec.version = AliyunSDK::Core::VERSION
10
+ spec.authors = ["Daniel Gong"]
11
+ spec.email = ["daniel@strikingly.com"]
12
+ spec.summary = %q{The core gem for all Aliyun API gems}
13
+ spec.description = %q{The core gem for all Aliyun API gems. Do not use it directly.}
14
+ spec.homepage = "https://github.com/strikingly/aliyun-sdk-core"
15
+ spec.license = "MIT"
16
+
17
+ spec.required_ruby_version = '>= 2.0.0'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.3', '>= 1.3.0'
23
+ spec.add_development_dependency 'rake', '~> 0.8', '>= 0.8.7'
24
+
25
+ spec.add_dependency "ruby-hmac", "~> 0.4.0"
26
+ end
@@ -0,0 +1,14 @@
1
+ require 'ostruct'
2
+
3
+ require 'aliyun/errors'
4
+
5
+ module Aliyun
6
+ class << self
7
+ def configure
8
+ yield config
9
+ end
10
+ def config
11
+ @config ||= OpenStruct.new
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ require 'aliyun/errors/base'
2
+
3
+ module Aliyun
4
+ module Errors
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Aliyun
2
+ module Errors
3
+ class Base < RuntimeError; end
4
+ end
5
+ end
@@ -0,0 +1,171 @@
1
+ require 'net/http'
2
+ require 'time'
3
+ require 'securerandom'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'logger'
7
+
8
+ require 'aliyun'
9
+ require 'aliyun/utils'
10
+ require 'aliyun/signature_methods'
11
+
12
+ module Aliyun
13
+ module Services
14
+ class Base
15
+ attr_accessor :access_key_id, :access_key_secret
16
+
17
+ def initialize(options)
18
+ self.access_key_id = options[:access_key_id]
19
+ self.access_key_secret = options[:access_key_secret]
20
+ end
21
+
22
+ def method_missing(method, *args, &block)
23
+ if service_actions.include?(action = method.to_sym)
24
+ params = args[0].nil? ? {} : args[0]
25
+ send_request(action, params)
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ def base_url
32
+ "#{request_schema}://#{request_host}"
33
+ end
34
+
35
+ def secure?
36
+ request_schema == 'https'
37
+ end
38
+
39
+ def name
40
+ self.class::NAME
41
+ end
42
+
43
+ def request_host
44
+ service_definition[:host]
45
+ end
46
+
47
+ def request_schema
48
+ service_definition[:schema] || Aliyun.config.default_schema || 'https'
49
+ end
50
+
51
+ def response_format
52
+ service_definition[:format] || Aliyun.config.default_format || 'JSON'
53
+ end
54
+
55
+ def api_version
56
+ service_definition[:version]
57
+ end
58
+
59
+ def separator
60
+ "&"
61
+ end
62
+
63
+ def http_method
64
+ 'GET'
65
+ end
66
+
67
+ def signature_method
68
+ Aliyun::SignatureMethods[service_definition[:signature_method]]
69
+ end
70
+
71
+ private
72
+
73
+ def send_request(action, params)
74
+ uri = URI(base_url)
75
+ uri.query = URI.encode_www_form(generate_request_params(action, params))
76
+
77
+ http = Net::HTTP.new(uri.host, uri.port)
78
+ http.use_ssl = secure?
79
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
80
+
81
+ request = Net::HTTP::Get.new(uri.request_uri)
82
+ logger.debug uri.request_uri
83
+ response = http.request(request)
84
+
85
+ case response
86
+ when Net::HTTPSuccess
87
+ return process_data(action, response.body)
88
+ else
89
+ raise process_error(action, response.code, response.body)
90
+ end
91
+ end
92
+
93
+ def generate_request_params(action, params)
94
+ params = process_params(action, params)
95
+ logger.debug params
96
+ params = params.merge(default_params(action))
97
+ logger.debug params
98
+ params['Signature'] = signature_method.generate(params, self)
99
+ logger.debug params
100
+ params
101
+ end
102
+
103
+ def process_params(action, params)
104
+ param_definitions = service_action_definitions[action][:parameters]
105
+ params.reduce({}) do |new_params, (k,v)|
106
+ unless param_definitions[k].nil?
107
+ new_params[param_definitions[k][:parameter]] = v
108
+ end
109
+ new_params
110
+ end
111
+ end
112
+
113
+ def process_data(action, data)
114
+ JSON.parse(data)
115
+ end
116
+
117
+ def process_error(action, error_code, error_message)
118
+ Aliyun::Errors::Base.new("Response code: #{error_code}, message: #{error_message}")
119
+ end
120
+
121
+ def default_params(action)
122
+ {
123
+ 'Format' => response_format,
124
+ 'Version' => api_version,
125
+ 'Action' => service_action_definitions[action][:action],
126
+ 'AccessKeyId' => access_key_id,
127
+ 'Timestamp' => Time.now.utc.iso8601,
128
+ 'SignatureMethod' => signature_method.name,
129
+ 'SignatureVersion' => signature_method.version,
130
+ 'SignatureNonce' => SecureRandom.uuid
131
+ }
132
+ end
133
+
134
+ def service_definition
135
+ self.class.load_service_definition(name)
136
+ end
137
+
138
+ def service_actions
139
+ service_action_definitions.keys
140
+ end
141
+
142
+ def service_action_definitions
143
+ self.class.load_service_action_definitions(name)
144
+ end
145
+
146
+ def logger
147
+ Aliyun.config.logger || Logger.new(STDOUT)
148
+ end
149
+
150
+ def self.load_service_definition(service_name)
151
+ @@service_definition ||= {}
152
+ @@service_definition[service_name] ||= Aliyun::Utils.symbolize_hash_keys(
153
+ YAML::load_file("#{api_definition_dir}/#{service_name}.yml")
154
+ )
155
+ end
156
+
157
+
158
+ def self.load_service_action_definitions(service_name)
159
+ @@service_action_definitions ||= {}
160
+ @@service_action_definitions[service_name] ||= Dir["#{api_definition_dir}/#{service_name}/*.yml"].map do |file|
161
+ File.basename(file, '.yml')
162
+ end.reduce({}) do |actions, action_name|
163
+ actions[action_name.to_sym] = Aliyun::Utils.symbolize_hash_keys(
164
+ YAML::load_file("#{api_definition_dir}/#{service_name}/#{action_name}.yml")
165
+ )
166
+ actions
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,13 @@
1
+ require 'aliyun/signature_methods/hmac_sha1'
2
+
3
+ module Aliyun
4
+ module SignatureMethods
5
+ METHODS = {
6
+ 'HMAC-SHA1' => Aliyun::SignatureMethods::HmacSha1.instance
7
+ }
8
+
9
+ def self.[](name)
10
+ METHODS[name]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Aliyun
2
+ module SignatureMethods
3
+ class Base
4
+ include Singleton
5
+
6
+ def name
7
+ self.class::NAME
8
+ end
9
+
10
+ def version
11
+ self.class::VERSION
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,40 @@
1
+ require 'uri'
2
+ require 'base64'
3
+ require 'hmac-sha1'
4
+
5
+ require 'aliyun/signature_methods/base'
6
+
7
+ module Aliyun
8
+ module SignatureMethods
9
+ class HmacSha1 < Base
10
+ NAME = "HMAC-SHA1".freeze
11
+ VERSION = "1.0".freeze
12
+
13
+ def generate(params, service)
14
+ calculate_signature("#{service.access_key_secret}&", string_to_sign(params, service))
15
+ end
16
+
17
+ private
18
+
19
+ def calculate_signature(key, string_to_sign)
20
+ hmac = HMAC::SHA1.new(key)
21
+ hmac.update(string_to_sign)
22
+ Base64.encode64(hmac.digest).gsub("\n", '')
23
+ end
24
+
25
+ def string_to_sign(params, service)
26
+ "#{service.http_method}#{service.separator}#{safe_encode('/')}#{service.separator}#{safe_encode(canonicalized_query_string(params, service))}"
27
+ end
28
+
29
+ def canonicalized_query_string(params, service)
30
+ params.keys.sort.map do |k|
31
+ "%s=%s" % [safe_encode(k.to_s), safe_encode(params[k.to_s])]
32
+ end.join(service.separator)
33
+ end
34
+
35
+ def safe_encode(s)
36
+ URI.encode_www_form_component(s).gsub(/\+/,'%20').gsub(/\*/,'%2A').gsub(/%7E/,'~')
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ module Aliyun
2
+ module Utils
3
+ def symbolize_hash_keys(object)
4
+ if object.is_a? Hash
5
+ object.keys.each { |k| object[(k.to_sym rescue k) || k] = symbolize_hash_keys(object.delete(k)) }
6
+ elsif object.is_a? Array
7
+ object.each { |e| symbolize_hash_keys(e) }
8
+ end
9
+ object
10
+ end
11
+
12
+ module_function :symbolize_hash_keys
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module AliyunSDK
2
+ module Core
3
+ VERSION = "0.1.3"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aliyun-sdk-core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Gong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-02 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.3'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.3.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.3.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.8'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.8.7
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.8'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.8.7
53
+ - !ruby/object:Gem::Dependency
54
+ name: ruby-hmac
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 0.4.0
60
+ type: :runtime
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: 0.4.0
67
+ description: The core gem for all Aliyun API gems. Do not use it directly.
68
+ email:
69
+ - daniel@strikingly.com
70
+ executables: []
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - ".gitignore"
75
+ - Gemfile
76
+ - LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - aliyun-sdk-core.gemspec
80
+ - lib/aliyun.rb
81
+ - lib/aliyun/errors.rb
82
+ - lib/aliyun/errors/base.rb
83
+ - lib/aliyun/services/base.rb
84
+ - lib/aliyun/signature_methods.rb
85
+ - lib/aliyun/signature_methods/base.rb
86
+ - lib/aliyun/signature_methods/hmac_sha1.rb
87
+ - lib/aliyun/utils.rb
88
+ - lib/aliyun_sdk/core/version.rb
89
+ homepage: https://github.com/strikingly/aliyun-sdk-core
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 2.0.0
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: The core gem for all Aliyun API gems
113
+ test_files: []