Class: StormMQ::URL
- Inherits:
-
Object
- Object
- StormMQ::URL
- Defined in:
- lib/stormmq/url.rb
Class Method Summary collapse
- .canonicalise_query_string(querystring) ⇒ Object
- .default_port_for_scheme(scheme) ⇒ Object
- .escape(string) ⇒ Object
- .hash_to_canonical_querystring(options) ⇒ Object
- .querystring_to_hash(querystring) ⇒ Object
Instance Method Summary collapse
- #add_query_params(params_hash) ⇒ Object
- #add_user_and_version_query_params(user, version = 0) ⇒ Object
- #canonicalise(user, version = 0) ⇒ Object
- #canonicalise_and_sign(user, secret_key, method = 'GET', verison = 0) ⇒ Object
- #compute_signature(secret_key, method = 'GET') ⇒ Object
-
#initialize(url) ⇒ URL
constructor
A new instance of URL.
- #sign(secret_key, method = 'GET') ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(url) ⇒ URL
Returns a new instance of URL.
26 27 28 29 30 31 32 33 |
# File 'lib/stormmq/url.rb', line 26 def initialize(url) if url.is_a?(Hash) @url = URI::Generic.build(url) else @url = URI.parse(url) end raise Error::InvalidURLError, "'#{@url.to_s}' is not a valid URL." unless self.valid? end |
Class Method Details
.canonicalise_query_string(querystring) ⇒ Object
119 120 121 122 |
# File 'lib/stormmq/url.rb', line 119 def self.canonicalise_query_string(querystring) query_hash = StormMQ::URL.querystring_to_hash(querystring) StormMQ::URL.hash_to_canonical_querystring(query_hash) end |
.default_port_for_scheme(scheme) ⇒ Object
102 103 104 |
# File 'lib/stormmq/url.rb', line 102 def self.default_port_for_scheme(scheme) SCHEMES_DEFAULT_PORTS[scheme.downcase] end |
.escape(string) ⇒ Object
97 98 99 100 |
# File 'lib/stormmq/url.rb', line 97 def self.escape(string) return '' if string.nil? URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end |
.hash_to_canonical_querystring(options) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/stormmq/url.rb', line 111 def self.hash_to_canonical_querystring() components = [] .each do |key,values| [values].flatten.each {|v| components << "#{StormMQ::URL.escape(key.to_s)}=#{StormMQ::URL.escape(v.to_s)}" } end components.sort.join('&') end |
.querystring_to_hash(querystring) ⇒ Object
106 107 108 109 |
# File 'lib/stormmq/url.rb', line 106 def self.querystring_to_hash(querystring) return {} if querystring.nil? CGI.parse(querystring) end |
Instance Method Details
#add_query_params(params_hash) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/stormmq/url.rb', line 47 def add_query_params(params_hash) components = to_h query_hash = StormMQ::URL.querystring_to_hash(components[:query]) components[:query] = StormMQ::URL.hash_to_canonical_querystring( query_hash.merge(params_hash) ) self.class.new(components) end |
#add_user_and_version_query_params(user, version = 0) ⇒ Object
56 57 58 |
# File 'lib/stormmq/url.rb', line 56 def add_user_and_version_query_params(user, version=0) add_query_params(:user => [user], :version => [version.to_s]) end |
#canonicalise(user, version = 0) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/stormmq/url.rb', line 60 def canonicalise(user, version=0) components = self.add_user_and_version_query_params(user, version).to_h components[:query] = StormMQ::URL.canonicalise_query_string(components[:query]) unless components[:query].nil? canonical = { :port => StormMQ::URL.default_port_for_scheme(components[:scheme]) }.merge(components) self.class.new(canonical) end |
#canonicalise_and_sign(user, secret_key, method = 'GET', verison = 0) ⇒ Object
77 78 79 |
# File 'lib/stormmq/url.rb', line 77 def canonicalise_and_sign(user, secret_key, method='GET', verison=0) self.canonicalise(user,verison).sign(secret_key, method) end |
#compute_signature(secret_key, method = 'GET') ⇒ Object
71 72 73 74 75 |
# File 'lib/stormmq/url.rb', line 71 def compute_signature(secret_key, method='GET') hmac = HMAC::SHA256.new(secret_key) hmac.update("#{method.upcase}#{self.to_s}") Base64.urlsafe_encode64(hmac.digest).chomp end |
#sign(secret_key, method = 'GET') ⇒ Object
67 68 69 |
# File 'lib/stormmq/url.rb', line 67 def sign(secret_key, method='GET') self.add_query_params('signature' => compute_signature(secret_key, method)) end |
#to_h ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/stormmq/url.rb', line 81 def to_h components = URI.split(@url.to_s) component_hash = { :scheme => components[0], :userinfo => components[1], :host => components[2], :port => components[3], :registry => components[4], :path => components[5], :opaque => components[6], :query => components[7], :fragment => components[8] }.reject {|k,v| v.nil?} component_hash end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/stormmq/url.rb', line 43 def to_s @url.to_s end |
#valid? ⇒ Boolean
35 36 37 38 39 40 41 |
# File 'lib/stormmq/url.rb', line 35 def valid? begin URI.parse(@url.to_s).class != URI::Generic rescue URI::InvalidURIError false end end |