Module: URI
- Defined in:
- lib/creative_rails_utilities/uri.rb
Class Method Summary collapse
- .build(scheme: "http", domain:, path: "/", query: "", fragment: "") ⇒ Object
-
.merge(uri = "", new_query = "", replace = false) ⇒ Object
use this to merge new query parameters into well-formed URI strings, pass averride as true to replace the query portion with the new one entirely.
Class Method Details
.build(scheme: "http", domain:, path: "/", query: "", fragment: "") ⇒ Object
2 3 4 5 6 7 8 9 10 11 |
# File 'lib/creative_rails_utilities/uri.rb', line 2 def build(scheme: "http", domain:, path: "/", query: "", fragment: "") if !query.is_a?(String) query = query.to_query # handles non-string argument, a Hash, for example end query = query.present? ? "?#{query.gsub(/\A\?/, "")}" : "" fragment = fragment.present? ? "##{fragment.gsub(/\A\#/, "")}" : "" return "#{scheme}://#{domain}#{path}#{query}#{fragment}" end |
.merge(uri = "", new_query = "", replace = false) ⇒ Object
use this to merge new query parameters into well-formed URI strings, pass averride as true to replace the query portion with the new one entirely
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/creative_rails_utilities/uri.rb', line 16 def merge(uri="", new_query="", replace=false) parsed_uri = URI(uri) raise ArgumentError.new(":uri argument is not a well-formed uri string!") if parsed_uri.host.blank? query = !new_query.is_a?(String) ? (new_query.try(:to_query).presence || new_query.to_s) : new_query.to_s = {scheme: parsed_uri.scheme, domain: parsed_uri.host, path: parsed_uri.path, query: query, fragment: parsed_uri.fragment} if !replace query = parsed_uri.query.to_s.to_query_hash.merge(query.to_s.to_query_hash).to_query end new_uri = URI.build(**.merge(query: query)) return new_uri end |