Class: Deeplink::Link
- Inherits:
-
Object
- Object
- Deeplink::Link
- Defined in:
- lib/deeplink/link.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#query ⇒ Object
readonly
To add and remove items to the query string use #add_query and #remove_query.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
Instance Method Summary collapse
-
#add_query(hash) ⇒ Object
Add query parameters to the link.
-
#initialize(uri) ⇒ Link
constructor
A new instance of Link.
-
#query? ⇒ Boolean
(also: #has_query?)
Returns true if the link has a query string or false otherwise.
-
#remove_query(*keys) ⇒ Object
Removes query parameters by its keys.
-
#to_s ⇒ Object
Returns the link as a String.
Constructor Details
#initialize(uri) ⇒ Link
Returns a new instance of Link.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/deeplink/link.rb', line 13 def initialize(uri) return unless uri uri = parse(uri) self.scheme = uri.scheme self.path = uri.path @query = sanitize(parse_query(uri.query)) if uri.query end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/deeplink/link.rb', line 8 def path @path end |
#query ⇒ Object (readonly)
To add and remove items to the query string use #add_query and #remove_query
11 12 13 |
# File 'lib/deeplink/link.rb', line 11 def query @query end |
#scheme ⇒ Object
Returns the value of attribute scheme.
8 9 10 |
# File 'lib/deeplink/link.rb', line 8 def scheme @scheme end |
Instance Method Details
#add_query(hash) ⇒ Object
34 35 36 37 38 |
# File 'lib/deeplink/link.rb', line 34 def add_query(hash) @query ||= {} @query.merge!(sanitize(hash)) end |
#query? ⇒ Boolean Also known as: has_query?
71 72 73 74 75 |
# File 'lib/deeplink/link.rb', line 71 def query? return false unless query !query.empty? end |
#remove_query(*keys) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/deeplink/link.rb', line 50 def remove_query(*keys) return unless query if keys.size > 1 keys.map { |key| query.delete(key.to_sym) } else query.delete(keys.first.to_sym) end end |
#to_s ⇒ Object
Returns the link as a String
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/deeplink/link.rb', line 80 def to_s return '' unless scheme && path uri = "#{scheme}:/#{path}" if query? query_string = query.map { |key, value| "#{key}=#{value}" }.join('&') uri += "?#{query_string}" end uri end |