Ruby/Network/URI
Материал из Wiki.crossplatform.ru
Содержание |
Create a URL with URI::HTTP.build
require "uri" URI::HTTP.build(:host => "example.ru", :path => "/file.html", :fragment => "section_3").to_s # => "http://example.ru/a/file.html#section_3"
Dump a URL
require "uri" class URI::Generic def dump component.each do |m| puts "#{m}: #{send(m).inspect}" end end end url = "http://l:pw@www.subdomain.example.ru:6060" + "/cgi-bin/mycgi.cgi?key1=val1#anchor" URI.parse(url).dump URI::parse("ftp://l:password@ftp.example.ru/file.txt").dump
Extracting All the URLs from an HTML Document
require "uri" text = %{"test <a href="http://www.example.ru/">http://www.example.ru/</a>, and be sure to check out my weblog at http://www.example.ru/blog/. Email me at <a href="mailto:b@e.ru">b@e.ru</a>.} p URI.extract(text)
Get only http and https based url
require "uri" text = %{"test <a href="http://www.example.ru/">http://www.example.ru/</a>, and be sure to check out my weblog at http://www.example.ru/blog/. Email me at <a href="mailto:bob@example.ru">b@e.ru</a>.} URI.extract(text, ["http", "https"])
mailto URI parse
require "uri" uri = URI::parse("mailto:l@a.ru?Subject=Hello&body=Hi!") uri.dump puts uri.to_mailtext
Parsing URLs
require "uri" puts URI.parse("https://www.example.ru").scheme # => "https" puts URI.parse("http://www.example.ru/").host # => "www.example.ru" puts URI.parse("http://www.example.ru:6060/").port # => 6060 puts URI.parse("http://example.ru/a/file.html").path # => "/a/file.html"
Split a URL
require "uri" puts URI.split("http://example.ru/a/file.html") # => ["http", nil, "example.ru", nil, nil, "/a/file.html", nil, nil, nil]
URI::HTTP.component
require "uri" puts URI::HTTP.ruponent # => [:scheme, :userinfo, :host, :port, :path, :query, :fragment]
URI::MailTo.component
require "uri" puts URI::MailTo.ruponent # => [:scheme, :to, :headers]
URI parse
require "uri" uri = URI.parse("tag:a.ru,2006,my-tag") uri.scheme uri.opaque