Giter Site home page Giter Site logo

Comments (6)

sporkmonger avatar sporkmonger commented on July 21, 2024

It's called every time you parse a URI. It'll raise an exception if the URI is invalid.

For example: Addressable::URI.parse('http://example.com:bogus/')

from addressable.

jwarchol avatar jwarchol commented on July 21, 2024

Not if you don't set a port

>> Addressable::URI.parse('not even close to valid')
=> #<Addressable::URI:0x829c96cc URI:not even close to valid>

I guess a better question is why is the hostname part of the validate method checking if port, user, etc are nil?

from addressable.

sporkmonger avatar sporkmonger commented on July 21, 2024

That is, in fact, a valid URI, believe it or not. It just happens to be relative and it's not in normal form, but it is a URI.

from addressable.

sporkmonger avatar sporkmonger commented on July 21, 2024

If you'd like to reject relative URIs, you can just do this:

if uri.relative?
  raise ArgumentError, 'URI must be absolute.'
end

Also, if you're accepting input from humans, please be sure to use Addressable::URI.heuristic_parse, not Addressable::URI.parse.

from addressable.

jwarchol avatar jwarchol commented on July 21, 2024

Thank you for the detailed responses. I'm not clear at all on how that's relative, so it's time for me to RTFM a bit more.

from addressable.

sporkmonger avatar sporkmonger commented on July 21, 2024
>> uri = Addressable::URI.parse('not even close to valid').normalize
=> #<Addressable::URI:0x80ad6a44 URI:not%20even%20close%20to%20valid>
>> uri.path
=> "not%20even%20close%20to%20valid"
>> uri.relative?
=> true

If a URI doesn't have a scheme component, it's relative.

This allows, for example, web pages to place anchors like this:

<a href="not even close to valid.html">elsewhere</a>

In this case, this link would resolve to:

>> uri_of_current_document = 'http://example.com/document.html'
=> "http://example.com/document.html"
>> (Addressable::URI.parse(uri_of_current_document) +
?>   "not even close to valid.html").normalize
=> #<Addressable::URI:0x80ac60cc URI:http://example.com/not%20even%20close%20to%20valid.html>

from addressable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.