Class: StormMQ::URL

Inherits:
Object
  • Object
show all
Defined in:
lib/stormmq/url.rb

Class Method Summary collapse

Instance Method Summary collapse

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



118
119
120
121
# File 'lib/stormmq/url.rb', line 118

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



101
102
103
# File 'lib/stormmq/url.rb', line 101

def self.default_port_for_scheme(scheme)
  SCHEMES_DEFAULT_PORTS[scheme.downcase]
end

.escape(string) ⇒ Object



96
97
98
99
# File 'lib/stormmq/url.rb', line 96

def self.escape(string)
  return '' if string.nil?
  URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end

.hash_to_canonical_querystring(options) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/stormmq/url.rb', line 110

def self.hash_to_canonical_querystring(options)
  components = []
  options.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



105
106
107
108
# File 'lib/stormmq/url.rb', line 105

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_hObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/stormmq/url.rb', line 81

def to_h
  components = URI.split(@url.to_s)
  {
    :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?}
end

#to_sObject



43
44
45
# File 'lib/stormmq/url.rb', line 43

def to_s
  @url.to_s
end

#valid?Boolean

Returns:

  • (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