Class: CloudStackHelper
- Inherits:
-
Object
- Object
- CloudStackHelper
- Defined in:
- lib/cloudstack_helper.rb
Constant Summary collapse
- CONFIGURABLE_ATTRIBUTES =
[ :response, :secret_key, :api_key, :api_url ]
Instance Method Summary collapse
- #generate_params_str(params) ⇒ Object
-
#generate_signature(params) ⇒ Object
To generate the signature: 1.
- #get(params, api_url = nil) ⇒ Object
-
#initialize(options = {}) ⇒ CloudStackHelper
constructor
A new instance of CloudStackHelper.
- #parameterize(params, escape = true) ⇒ Object
- #request(params, api_url, method = :get) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CloudStackHelper
Returns a new instance of CloudStackHelper.
16 17 18 19 20 |
# File 'lib/cloudstack_helper.rb', line 16 def initialize( = {}) CONFIGURABLE_ATTRIBUTES.each do |attribute_name| self.send("#{attribute_name}=", [attribute_name]) if .has_key?(attribute_name) end end |
Instance Method Details
#generate_params_str(params) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/cloudstack_helper.rb', line 50 def generate_params_str(params) unless params[:response] params[:response] = @response if @response end params[:apikey] = @api_key params[:signature] = generate_signature(params.clone) str = parameterize(params) end |
#generate_signature(params) ⇒ Object
To generate the signature:
-
For each field-value pair (as separated by a ‘&’) in the Command String, URL encode each value so that it can be safely sent via HTTP GET.
NOTE: Make sure all spaces are encoded as “%20” rather than “+”.
-
Lower case the entire Command String and sort it alphabetically via the field for each field-value pair.
-
Take the sorted Command String and run it through the HMAC SHA-1 hashing algorithm with the user’s Secret Key.
-
Base64 encode the resulting byte array in UTF-8 so that it can be safely transmitted via HTTP.
29 30 31 32 33 34 35 36 37 |
# File 'lib/cloudstack_helper.rb', line 29 def generate_signature(params) params.each { |k,v| params[k] = CGI.escape(v.to_s).gsub('+', '%20').downcase } sorted_params = params.sort_by{|key,value| key.to_s} data = parameterize(sorted_params, false) hash = OpenSSL::HMAC.digest('sha1', @secret_key, data) signature = Base64.encode64(hash).chomp end |
#get(params, api_url = nil) ⇒ Object
70 71 72 73 |
# File 'lib/cloudstack_helper.rb', line 70 def get(params, api_url = nil) api_url ||= @api_url request(params, api_url, :get) end |
#parameterize(params, escape = true) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cloudstack_helper.rb', line 39 def parameterize(params, escape=true) params = params.collect do |k,v| if escape "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" else "#{k}=#{v}" end end params.join('&') end |
#request(params, api_url, method = :get) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/cloudstack_helper.rb', line 59 def request(params, api_url, method = :get) case method when :get url = api_url + "?" + generate_params_str(params.clone) response = RestClient.send(method, url) else raise "HTTP method #{method} not supported" end response end |