Module: WAZ::Storage::SharedKeyCoreService
- Included in:
- Queues::Service
- Defined in:
- lib/waz/storage/core_service.rb
Instance Attribute Summary collapse
-
#account_key ⇒ Object
Returns the value of attribute account_key.
-
#account_name ⇒ Object
Returns the value of attribute account_name.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
Instance Method Summary collapse
- #canonicalize_headers(headers) ⇒ Object
- #canonicalize_message(url) ⇒ Object
- #generate_request(verb, url, headers = {}, payload = nil) ⇒ Object
- #generate_request_uri(operation = nil, path = nil, options = {}) ⇒ Object
- #generate_signature(request) ⇒ Object
- #initialize(account_name, account_key, use_ssl = false, base_url = "queue.core.windows.net") ⇒ Object
Instance Attribute Details
#account_key ⇒ Object
Returns the value of attribute account_key.
4 5 6 |
# File 'lib/waz/storage/core_service.rb', line 4 def account_key @account_key end |
#account_name ⇒ Object
Returns the value of attribute account_name.
4 5 6 |
# File 'lib/waz/storage/core_service.rb', line 4 def account_name @account_name end |
#base_url ⇒ Object
Returns the value of attribute base_url.
4 5 6 |
# File 'lib/waz/storage/core_service.rb', line 4 def base_url @base_url end |
#use_ssl ⇒ Object
Returns the value of attribute use_ssl.
4 5 6 |
# File 'lib/waz/storage/core_service.rb', line 4 def use_ssl @use_ssl end |
Instance Method Details
#canonicalize_headers(headers) ⇒ Object
31 32 33 34 |
# File 'lib/waz/storage/core_service.rb', line 31 def canonicalize_headers(headers) cannonicalized_headers = headers.keys.select {|h| h.to_s.start_with? 'x-ms'}.map{ |h| "#{h.downcase.strip}:#{headers[h].strip}" }.sort{ |a, b| a <=> b }.join("\x0A") return cannonicalized_headers end |
#canonicalize_message(url) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/waz/storage/core_service.rb', line 36 def (url) uri_component = url.gsub(/https?:\/\/[^\/]+\//i, '').gsub(/\?.*/i, '') comp_component = url.scan(/(comp=[^&]+)/i).first() uri_component << "?#{comp_component}" if comp_component = "/#{self.account_name}/#{uri_component}" return end |
#generate_request(verb, url, headers = {}, payload = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/waz/storage/core_service.rb', line 13 def generate_request(verb, url, headers = {}, payload = nil) http_headers = {} headers.each{ |k, v| http_headers[k.to_s.gsub(/_/, '-')] = v} unless headers.nil? request = RestClient::Request.new(:method => verb.downcase.to_sym, :url => url, :headers => http_headers, :payload => payload) request.headers["x-ms-Date"] = Time.new.httpdate request.headers["Content-Length"] = (request.payload or "").length request.headers["Authorization"] = "SharedKey #{account_name}:#{generate_signature(request)}" return request end |
#generate_request_uri(operation = nil, path = nil, options = {}) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/waz/storage/core_service.rb', line 23 def generate_request_uri(operation = nil, path = nil, = {}) protocol = use_ssl ? "https" : "http" query_params = .keys.sort{ |a, b| a.to_s <=> b.to_s}.map{ |k| "#{k.to_s.gsub(/_/, '')}=#{[k]}"}.join("&") unless .empty? uri = "#{protocol}://#{account_name}.#{base_url}#{(path or "").start_with?("/") ? "" : "/"}#{(path or "")}#{operation ? "?comp=" + operation : ""}" uri << "#{operation ? "&" : "?"}#{query_params}" if query_params return uri end |
#generate_signature(request) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/waz/storage/core_service.rb', line 44 def generate_signature(request) signature = request.method.to_s.upcase + "\x0A" + (request.headers["Content-MD5"] or "") + "\x0A" + (request.headers["Content-Type"] or "") + "\x0A" + (request.headers["Date"] or "")+ "\x0A" + canonicalize_headers(request.headers) + "\x0A" + (request.url) return Base64.encode64(HMAC::SHA256.new(Base64.decode64(self.account_key)).update(signature.toutf8).digest) end |
#initialize(account_name, account_key, use_ssl = false, base_url = "queue.core.windows.net") ⇒ Object
6 7 8 9 10 11 |
# File 'lib/waz/storage/core_service.rb', line 6 def initialize(account_name, account_key, use_ssl = false, base_url = "queue.core.windows.net" ) self.account_name = account_name self.account_key = account_key self.use_ssl = use_ssl self.base_url = base_url end |