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.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/deeplink/link.rb', line 11 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.
6 7 8 |
# File 'lib/deeplink/link.rb', line 6 def path @path end |
#query ⇒ Object (readonly)
To add and remove items to the query string use #add_query and #remove_query
9 10 11 |
# File 'lib/deeplink/link.rb', line 9 def query @query end |
#scheme ⇒ Object
Returns the value of attribute scheme.
6 7 8 |
# File 'lib/deeplink/link.rb', line 6 def scheme @scheme end |
Instance Method Details
#add_query(hash) ⇒ Object
Add query parameters to the link. You can add one or more parameters since this method receives a hash.
Example
deeplink = Deeplink.parse("link://directions")
deeplink.add_query(lat: 38.7179233, lon: -9.150129)
deeplink.to_s # => "link://directions?lat=38.7179233&lon=-9.150129"
32 33 34 35 36 |
# File 'lib/deeplink/link.rb', line 32 def add_query(hash) @query ||= {} @query.merge!(sanitize(hash)) end |
#query? ⇒ Boolean Also known as: has_query?
Returns true if the link has a query string or false otherwise
Example
deeplink = Deeplink.parse("link://directions")
deeplink.query? # => false
deeplink.add_query(foo: "bar") # => { :foo => "bar" }
deeplink.query? # => true
69 70 71 72 73 |
# File 'lib/deeplink/link.rb', line 69 def query? return false unless query !query.empty? end |
#remove_query(*keys) ⇒ Object
Removes query parameters by its keys. You can remove one or more parameters, sending a list of keys.
Example
deeplink = Deeplink.parse("link://directions?lat=38.7179233&lon=-9.150129&test=true")
deeplink.remove_query(:test) # => "true"
deeplink.remove_query(:lat, :lon) # => [38.7179233, -9.150129]
48 49 50 51 52 53 54 55 56 |
# File 'lib/deeplink/link.rb', line 48 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
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/deeplink/link.rb', line 78 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 |