lib/iri.rb in iri-0.4.3 vs lib/iri.rb in iri-0.5.0

- old
+ new

@@ -43,25 +43,33 @@ # # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2019 Yegor Bugayenko # License:: MIT class Iri + # When URI is not valid. + class InvalidURI < StandardError; end + # Makes a new object. # # You can even ignore the argument, which will produce an empty URI. - def initialize(uri = '') - @uri = URI(uri) + # + # By default, this class will never throw any exceptions, even if your URI + # is not valid. It will just assume that the URI is"/". However, + # you can turn this mode off, by specifying safe as FALSE. + def initialize(uri = '', safe: true) + @uri = uri + @safe = safe end # Convert it to a string. def to_s @uri.to_s end # Convert it to an object of class +URI+. def to_uri - @uri.clone + the_uri.clone end # Add a few query arguments. For example: # # Iri.new('https://google.com').add(q: 'test', limit: 10) @@ -173,18 +181,27 @@ end end private + def the_uri + @the_uri ||= URI(@uri) + rescue URI::InvalidURIError => e + raise InvalidURI, e.message unless @safe + @the_uri = URI('/') + end + def modify - c = @uri.clone + c = the_uri.clone yield c Iri.new(c) end def modify_query modify do |c| - params = CGI.parse(@uri.query || '').map { |p, a| [p.to_s, a.clone] }.to_h + params = CGI.parse(the_uri.query || '').map do |p, a| + [p.to_s, a.clone] + end.to_h yield(params) c.query = URI.encode_www_form(params) end end end