Class: URLSignature::URL
- Inherits:
-
Object
- Object
- URLSignature::URL
- Defined in:
- lib/url_signature/url.rb
Constant Summary collapse
- SEQUENCIAL_PARAMS =
Object.new
Instance Attribute Summary collapse
-
#fragment ⇒ Object
readonly
Returns the value of attribute fragment.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #add_query(key, value, replace: true) ⇒ Object
- #clear_query! ⇒ Object
-
#initialize(url) ⇒ URL
constructor
A new instance of URL.
- #port ⇒ Object
- #query ⇒ Object
- #remove_query(key) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(url) ⇒ URL
Returns a new instance of URL.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/url_signature/url.rb', line 11 def initialize(url) @uri = parse_url(url) @scheme = uri.scheme @host = uri.host @user = uri.user @password = uri.password @path = uri.path.empty? ? "/" : uri.path @params = parse_query(uri.query) @fragment = uri.fragment end |
Instance Attribute Details
#fragment ⇒ Object (readonly)
Returns the value of attribute fragment.
5 6 7 |
# File 'lib/url_signature/url.rb', line 5 def fragment @fragment end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
5 6 7 |
# File 'lib/url_signature/url.rb', line 5 def host @host end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/url_signature/url.rb', line 5 def params @params end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
5 6 7 |
# File 'lib/url_signature/url.rb', line 5 def password @password end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/url_signature/url.rb', line 5 def path @path end |
#scheme ⇒ Object (readonly)
Returns the value of attribute scheme.
5 6 7 |
# File 'lib/url_signature/url.rb', line 5 def scheme @scheme end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
5 6 7 |
# File 'lib/url_signature/url.rb', line 5 def user @user end |
Instance Method Details
#add_query(key, value, replace: true) ⇒ Object
29 30 31 32 33 |
# File 'lib/url_signature/url.rb', line 29 def add_query(key, value, replace: true) params[key] ||= [] params[key] = [] if replace params[key] += [value].flatten.map(&:to_s) end |
#clear_query! ⇒ Object
55 56 57 |
# File 'lib/url_signature/url.rb', line 55 def clear_query! @params = {} end |
#port ⇒ Object
22 23 24 25 26 27 |
# File 'lib/url_signature/url.rb', line 22 def port return if uri.port == 80 && @scheme == "http" return if uri.port == 443 && @scheme == "https" uri.port end |
#query ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/url_signature/url.rb', line 39 def query return if params.empty? query = params.each_with_object([]) do |(param, value), buffer| param = param.to_s if param.include?("[") value.each {|v| buffer << "#{encode(param)}=#{encode(v)}" } else buffer << "#{encode(param)}=#{encode(value.last)}" end end query.sort.join("&") end |
#remove_query(key) ⇒ Object
35 36 37 |
# File 'lib/url_signature/url.rb', line 35 def remove_query(key) params.delete(key) || [] end |
#to_s ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/url_signature/url.rb', line 59 def to_s [ "#{scheme}://#{host}", port ? ":#{port}" : nil, path, query ? "?#{query}" : nil, fragment ? "##{fragment}" : nil ].compact.join end |