URL parser: Difference between revisions

Added Crystal implementation
(Ada version)
(Added Crystal implementation)
Line 494:
}
</lang>
 
=={{header|Crystal}}==
 
This example demonstrates use of the Crystal standard library's <code>URI</code> class.
 
<lang crystal>require "uri"
 
examples = ["foo://example.com:8042/over/there?name=ferret#nose",
"urn:example:animal:ferret:nose",
"jdbc:mysql://test_user:ouupppssss@localhost:3306/sakila?profileSQL=true",
"ftp://ftp.is.co.za/rfc/rfc1808.txt",
"http://www.ietf.org/rfc/rfc2396.txt#header1",
"ldap://[2001:db8::7]/c=GB?objectClass=one&objectClass=two",
"mailto:John.Doe@example.com",
"news:comp.infosystems.www.servers.unix",
"tel:+1-816-555-1212",
"telnet://192.0.2.16:80/",
"urn:oasis:names:specification:docbook:dtd:xml:4.1.2",
"https://bob:password@[::1]/place?a=1&b=2%202"]
 
examples.each do |example|
puts "Parsing \"#{example}\":"
url = URI.parse example
{% for name in ["scheme", "host", "hostname", "port", "path", "userinfo",
"user", "password", "fragment", "query"] %}
unless url.{{name.id}}.nil?
puts " {{name.id}}: \"#{url.{{name.id}}}\""
end
{% end %}
unless url.query_params.empty?
puts " query_params:"
url.query_params.each do |k, v|
puts " #{k}: \"#{v}\""
end
end
puts
end</lang>
 
{{out}}
<pre>Parsing "foo://example.com:8042/over/there?name=ferret#nose":
scheme: "foo"
host: "example.com"
hostname: "example.com"
port: "8042"
path: "/over/there"
fragment: "nose"
query: "name=ferret"
query_params:
name: "ferret"
 
Parsing "urn:example:animal:ferret:nose":
scheme: "urn"
path: "example:animal:ferret:nose"
 
Parsing "jdbc:mysql://test_user:ouupppssss@localhost:3306/sakila?profileSQL=true":
scheme: "jdbc"
path: "mysql://test_user:ouupppssss@localhost:3306/sakila"
query: "profileSQL=true"
query_params:
profileSQL: "true"
 
Parsing "ftp://ftp.is.co.za/rfc/rfc1808.txt":
scheme: "ftp"
host: "ftp.is.co.za"
hostname: "ftp.is.co.za"
path: "/rfc/rfc1808.txt"
 
Parsing "http://www.ietf.org/rfc/rfc2396.txt#header1":
scheme: "http"
host: "www.ietf.org"
hostname: "www.ietf.org"
path: "/rfc/rfc2396.txt"
fragment: "header1"
 
Parsing "ldap://[2001:db8::7]/c=GB?objectClass=one&objectClass=two":
scheme: "ldap"
host: "[2001:db8::7]"
hostname: "2001:db8::7"
path: "/c=GB"
query: "objectClass=one&objectClass=two"
query_params:
objectClass: "one"
objectClass: "two"
 
Parsing "mailto:John.Doe@example.com":
scheme: "mailto"
path: "John.Doe@example.com"
 
Parsing "news:comp.infosystems.www.servers.unix":
scheme: "news"
path: "comp.infosystems.www.servers.unix"
 
Parsing "tel:+1-816-555-1212":
scheme: "tel"
path: "+1-816-555-1212"
 
Parsing "telnet://192.0.2.16:80/":
scheme: "telnet"
host: "192.0.2.16"
hostname: "192.0.2.16"
port: "80"
path: "/"
 
Parsing "urn:oasis:names:specification:docbook:dtd:xml:4.1.2":
scheme: "urn"
path: "oasis:names:specification:docbook:dtd:xml:4.1.2"
 
Parsing "https://bob:password@[::1]/place?a=1&b=2%202":
scheme: "https"
host: "[::1]"
hostname: "::1"
path: "/place"
userinfo: "bob:password"
user: "bob"
password: "password"
query: "a=1&b=2%202"
query_params:
a: "1"
b: "2 2"</pre>
 
=={{header|Elixir}}==
Anonymous user