Underscore is not a valid character in a hostname, thus Ruby rejects it.
To allow it, you have to manually add it to the regex in library file
uri/common.rb, line 368. (On my Windows install,
F:\Ruby193\lib\ruby\1.9.1\uri), to looks like this:
if I have the following url: http://auto_diversen.marktplaza.nl
This works fine:
URI::Parser.new(:HOSTNAME => "(?:[_a-zA-Z0-9\\-.]|%\\h\\h)+").parse(url)
But this returns an error:
URI::Parser.new(:HOSTNAME =>
"(?:[_a-zA-Z0-9\\-.]|%\\h\\h)+").parse(url).open
Returns: the scheme http does not accept registry part:
auto_diversen.marktplaza.nl:80 (or bad hostname?)
When I look to the source of 'open', It looks to be that it parses the
URL again, so the error is raised.
The source on: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/Kernel.html#method-i-open
# File open-uri.rb, line 27
def open(name, *rest, &block) # :doc:
if name.respond_to?(:open)
name.open(*rest, &block)
elsif name.respond_to?(:to_str) &&
%{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
(uri = URI.parse(name)).respond_to?(:open)
uri.open(*rest, &block)
else
open_uri_original_open(name, *rest, &block)
end
end
Underscore is not a valid character in a hostname, thus Ruby rejects it.
To allow it, you have to manually add it to the regex in library file
uri/common.rb, line 368. (On my Windows install,
F:\Ruby193\lib\ruby\1.9.1\uri), to looks like this:
I have already explained. Change or monkey-patch the URI library.
···
2012/4/17, Jeroen van Ingen <lists@ruby-forum.com>:
if I have the following url: http://auto_diversen.marktplaza.nl
This works fine:
URI::Parser.new(:HOSTNAME => "(?:[_a-zA-Z0-9\\-.]|%\\h\\h)+").parse(url)
But this returns an error:
URI::Parser.new(:HOSTNAME =>
"(?:[_a-zA-Z0-9\\-.]|%\\h\\h)+").parse(url).open
Returns: the scheme http does not accept registry part:
auto_diversen.marktplaza.nl:80 (or bad hostname?)
When I look to the source of 'open', It looks to be that it parses the
URL again, so the error is raised.
The source on: Module: Kernel (Ruby 1.9.3)
# File open-uri.rb, line 27
def open(name, *rest, &block) # :doc:
if name.respond_to?(:open)
name.open(*rest, &block)
elsif name.respond_to?(:to_str) &&
%{\A[A-Za-z][A-Za-z0-9+\-\.]*://} =~ name &&
(uri = URI.parse(name)).respond_to?(:open)
uri.open(*rest, &block)
else
open_uri_original_open(name, *rest, &block)
end
end
W dniu 30 marca 2012 13:28 użytkownik Bartosz Dziewoński
<matma.rex@gmail.com> napisał:
Underscore is not a valid character in a hostname, thus Ruby rejects it.
To allow it, you have to manually add it to the regex in library file
uri/common.rb, line 368. (On my Windows install,
F:\Ruby193\lib\ruby\1.9.1\uri), to looks like this:
Well, you could also copy the patched URI::Parser#initialize_pattern
method to all your files that need it. (Just make sure to require
'uri' first, and monkeypatch later.) Like this:
require 'uri'
class URI::Parser
def initialize_pattern(opts = {})
<snip...>
end
end
There is no other way to do it, without rewriting the entire URI
library from scratch. (And that would be stupid, even more so since
the way you want it to work is technically invalid.)
W dniu 30 marca 2012 13:28 użytkownik Bartosz Dziewoński
<matma.rex@gmail.com> napisał:
Underscore is not a valid character in a hostname, thus Ruby rejects it.
To allow it, you have to manually add it to the regex in library file
uri/common.rb, line 368. (On my Windows install,
F:\Ruby193\lib\ruby\1.9.1\uri), to looks like this:
Well, you could also copy the patched URI::Parser#initialize_pattern
method to all your files that need it. (Just make sure to require
'uri' first, and monkeypatch later.) Like this:
require 'uri'
class URI::Parser
def initialize_pattern(opts = {})
<snip...>
end
end
There is no other way to do it, without rewriting the entire URI
library from scratch. (And that would be stupid, even more so since
the way you want it to work is technically invalid.)