非常教程

Ruby 2.4参考手册

URI

URI

Included modules:URI::REGEXP

URI是提供处理统一资源标识符(RFC2396)的类的模块,

特征

  • 统一处理URI的处理
  • 灵活引入自定义URI方案
  • 灵活地有一个可选的URI :: Parser(或者只是不同的模式和正则表达式)基本示例需要'uri'

uri = URI("http://foo.com/posts?id=30&limit=5#time=1305298413") #=> #<URI::HTTP:0x00000000b14880

URL:http://foo.com/posts?id=30&limit=5#time=1305298413>

uri.scheme

#=> "http"

uri.host

#=> "foo.com"

uri.path

#=> "/posts"

uri.query

#=> "id=30&limit=5"

uri.fragment

#=> "time=1305298413"

uri.to_s

#=> "http://foo.com/posts?id=30&limit=5#time=1305298413"Adding custom URIsmodule URI

class RSYNC < Generic

DEFAULT_PORT = 873

end

@@schemes['RSYNC'] = RSYNC

end

#=> URI::RSYNC

URI.scheme_list

#=> {"FTP"=>URI::FTP, "HTTP"=>URI::HTTP, "HTTPS"=>URI::HTTPS, "LDAP"=>URI::LDAP, "LDAPS"=>URI::LDAPS,

"MAILTO"=>URI::MailTo,

"RSYNC"=>URI::RSYNC}

uri = URI("rsync://rsync.foo.com") #=> #<URI::RSYNC:0x00000000f648c8

0以下是所有相关RFC的列表。

  • RFC822
  • RFC1738
  • RFC2255
  • RFC2368
  • RFC2373
  • RFC2396
  • RFC2732
  • RFC3986Class tree
  • URI::Generic (in uri/generic.rb)
-  [URI::FTP](uri/ftp) - (in uri/ftp.rb)

-  [URI::HTTP](uri/http) - (in uri/http.rb)
    -  [URI::HTTPS](uri/https) - (in uri/https.rb)




-  [URI::LDAP](uri/ldap) - (in uri/ldap.rb)
    -  [URI::LDAPS](uri/ldaps) - (in uri/ldaps.rb)




-  [URI::MailTo](uri/mailto) - (in uri/mailto.rb)
  • URI::Parser - (in uri/common.rb)
  • URI::REGEXP - (in uri/common.rb)
-  URI::REGEXP::PATTERN - (in uri/common.rb)URI::Util - (in uri/common.rb)
  • URI::Escape - (in uri/common.rb)
  • URI::Error - (in uri/common.rb)
-  [URI::InvalidURIError](uri/invalidurierror) - (in uri/common.rb)

-  [URI::InvalidComponentError](uri/invalidcomponenterror) - (in uri/common.rb)
-  [URI::BadURIError](uri/badurierror) - (in uri/common.rb)

版权信息

$Id: uri.rb 53141 2015-12-16 05:07:31Z naruse $

常量

DEFAULT_PARSER

URI::Parser.new

Parser REGEXP RFC3986_PARSER

公共类别方法

decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_: false, isindex: false) Show source

解码给定的URL编码表单数据str

这解码application/x-www-form-urlencoded数据并返回键值数组的数组。

ary = URI.decode_www_form("a=1&a=2&b=3")
p ary                  #=> [['a', '1'], ['a', '2'], ['b', '3']]
p ary.assoc('a').last  #=> '1'
p ary.assoc('b').last  #=> '3'
p ary.rassoc('a').last #=> '2'
p Hash[ary]            # => {"a"=>"2", "b"=>"3"}

See ::decode_www_form_component, ::encode_www_form

# File lib/uri/common.rb, line 453
def self.decode_www_form(str, enc=Encoding::UTF_8, separator: '&', use__charset_: false, isindex: false)
  raise ArgumentError, "the input of #{self.name}.#{__method__} must be ASCII only string" unless str.ascii_only?
  ary = []
  return ary if str.empty?
  enc = Encoding.find(enc)
  str.b.each_line(separator) do |string|
    string.chomp!(separator)
    key, sep, val = string.partition('=')
    if isindex
      if sep.empty?
        val = key
        key = ''
      end
      isindex = false
    end

    if use__charset_ and key == '_charset_' and e = get_encoding(val)
      enc = e
      use__charset_ = false
    end

    key.gsub!(/\+|%\h\h/, TBLDECWWWCOMP_)
    if val
      val.gsub!(/\+|%\h\h/, TBLDECWWWCOMP_)
    else
      val = ''
    end

    ary << [key, val]
  end
  ary.each do |k, v|
    k.force_encoding(enc)
    k.scrub!
    v.force_encoding(enc)
    v.scrub!
  end
  ary
end

decode_www_form_component(str, enc=Encoding::UTF_8) Show source

给定strURL编码表单数据的解码。

这解码+到SP。

See ::encode_www_form_component, ::decode_www_form

# File lib/uri/common.rb, line 385
def self.decode_www_form_component(str, enc=Encoding::UTF_8)
  raise ArgumentError, "invalid %-encoding (#{str})" if /%(?!\h\h)/ =~ str
  str.b.gsub(/\+|%\h\h/, TBLDECWWWCOMP_).force_encoding(enc)
end

encode_www_form(enum, enc=nil) Show source

从给定enum生成URL编码的表单数据。

这将从给定的Enumerable对象生成HTML5中定义的application / x-www-form-urlencoded数据。

这在内部使用::encode_www_form_component。

此方法不会转换给定项目的编码,因此如果您希望以非原始编码或混合编码数据发送数据,请在调用此方法之前转换它们。(以HTML5 ASCII不兼容编码编码的字符串转换为UTF-8。)

This method doesn't handle files. When you send a file, use multipart/form-data.

URI.encode_www_form([["q", "ruby"], ["lang", "en"]])
#=> "q=ruby&lang=en"
URI.encode_www_form("q" => "ruby", "lang" => "en")
#=> "q=ruby&lang=en"
URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en")
#=> "q=ruby&q=perl&lang=en"
URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]])
#=> "q=ruby&q=perl&lang=en"

See ::encode_www_form_component, ::decode_www_form

# File lib/uri/common.rb, line 417
def self.encode_www_form(enum, enc=nil)
  enum.map do |k,v|
    if v.nil?
      encode_www_form_component(k, enc)
    elsif v.respond_to?(:to_ary)
      v.to_ary.map do |w|
        str = encode_www_form_component(k, enc)
        unless w.nil?
          str << '='
          str << encode_www_form_component(w, enc)
        end
      end.join('&')
    else
      str = encode_www_form_component(k, enc)
      str << '='
      str << encode_www_form_component(v, enc)
    end
  end.join('&')
end

encode_www_form_component(str, enc=nil) Show source

编码给strURL0编码的表单数据。

此方法不转换*, -, ., 0-9, AZ, _, az,但将SP(ASCII空间)转换为+并将其他转换为%XX。

如果enc给出,则str在编码百分比前转换为编码。

See ::decode_www_form_component, ::encode_www_form

# File lib/uri/common.rb, line 367
def self.encode_www_form_component(str, enc=nil)
  str = str.to_s.dup
  if str.encoding != Encoding::ASCII_8BIT
    if enc && enc != Encoding::ASCII_8BIT
      str.encode!(Encoding::UTF_8, invalid: :replace, undef: :replace)
      str.encode!(enc, fallback: ->(x){"&#{x.ord};"})
    end
    str.force_encoding(Encoding::ASCII_8BIT)
  end
  str.gsub!(/[^*\-.0-9A-Z_a-z]/, TBLENCWWWCOMP_)
  str.force_encoding(Encoding::US_ASCII)
end

extract(str, schemes = nil, &block) Show source

概要

URI::extract(str[, schemes][,&blk])

Args

str

从中提取URI的字符串。

schemes

将URI匹配限制为特定方案。

描述

从字符串中提取URI。如果给出的块,遍历所有匹配的URI。如果给定块或匹配数组,则返回nil。

Usage

require "uri"

URI.extract("text here http://foo.example.org/bla and here mailto:test@example.com and here also.")
# => ["http://foo.example.com/bla", "mailto:test@example.com"]
# File lib/uri/common.rb, line 295
def self.extract(str, schemes = nil, &block)
  warn "#{caller(1)[0]}: warning: URI.extract is obsolete" if $VERBOSE
  DEFAULT_PARSER.extract(str, schemes, &block)
end

join(*str) Show source

概要

URI::join(str[, str, ...])

Args

str

在合并之前,要使用的字符串将被转换为RFC3986 URI。

描述

加入URI。

用法

require 'uri'

p URI.join("http://example.com/","main.rbx")
# => #<URI::HTTP:0x2022ac02 URL:http://example.com/main.rbx>

p URI.join('http://example.com', 'foo')
# => #<URI::HTTP:0x01ab80a0 URL:http://example.com/foo>

p URI.join('http://example.com', '/foo', '/bar')
# => #<URI::HTTP:0x01aaf0b0 URL:http://example.com/bar>

p URI.join('http://example.com', '/foo', 'bar')
# => #<URI::HTTP:0x801a92af0 URL:http://example.com/bar>

p URI.join('http://example.com', '/foo/', 'bar')
# => #<URI::HTTP:0x80135a3a0 URL:http://example.com/foo/bar>
# File lib/uri/common.rb, line 267
def self.join(*str)
  RFC3986_PARSER.join(*str)
end

parse(uri) Show source

概要

URI::parse(uri_str)

Args

uri_str

带URI的字符串。

描述

从字符串中创建一个URI的子类实例。

Raises

URI::InvalidURIError

Raised if URI given is not a correct one.

用法

require 'uri'

uri = URI.parse("http://www.ruby-lang.org/")
p uri
# => #<URI::HTTP:0x202281be URL:http://www.ruby-lang.org/>
p uri.scheme
# => "http"
p uri.host
# => "www.ruby-lang.org"

uri_str如果有任何无效的URI字符,建议首先uri_str::提供所提供的内容。

# File lib/uri/common.rb, line 229
def self.parse(uri)
  RFC3986_PARSER.parse(uri)
end

regexp(schemes = nil) Show source

概要

URI::regexp([match_schemes])

Args

match_schemes

一系列计划。如果给定,那么导致正则表达式匹配其方案是match_schemes之一的URI。

描述

返回一个匹配类似URI的字符串的Regexp对象。此方法返回的Regexp对象包含任意数量的捕获组(括号)。永远不要依赖它的号码。

用法

require 'uri'

# extract first URI from html_string
html_string.slice(URI.regexp)

# remove ftp URIs
html_string.sub(URI.regexp(['ftp'])

# You should not rely on the number of parentheses
html_string.scan(URI.regexp) do |*matches|
  p $&
end
# File lib/uri/common.rb, line 331
def self.regexp(schemes = nil)
  warn "#{caller(1)[0]}: warning: URI.regexp is obsolete" if $VERBOSE
  DEFAULT_PARSER.make_regexp(schemes)
end

scheme_list() Show source

返回已定义方案的哈希值

# File lib/uri/common.rb, line 139
def self.scheme_list
  @@schemes
end

split(uri) Show source

概要

URI::split(uri)

Args

uri

带URI的字符串。

描述

将字符串拆分为以下几部分,并返回带有结果的数组:

* Scheme
* Userinfo
* Host
* Port
* Registry
* Path
* Opaque
* Query
* Fragment

用法

require 'uri'

p URI.split("http://www.ruby-lang.org/")
# => ["http", nil, "www.ruby-lang.org", nil, nil, "/", nil, nil, nil]
# File lib/uri/common.rb, line 191
def self.split(uri)
  RFC3986_PARSER.split(uri)
end
URI
URI::BadURIError 详细
URI::Error 详细
URI::Escape 详细
URI::FTP 详细
URI::Generic 详细
URI::HTTP 详细
URI::HTTPS 详细
URI::InvalidComponentError 详细
URI::InvalidURIError 详细
URI::LDAP 详细
URI::LDAPS 详细
URI::MailTo 详细
URI::RFC2396_Parser 详细
URI::RFC2396_REGEXP 详细
URI::RFC2396_REGEXP::PATTERN 详细
Ruby 2.4

Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言,是世界上最优美而巧妙的语言。

主页 https://www.ruby-lang.org/
源码 https://github.com/ruby/ruby
版本 2.4
发布版本 2.4.1

Ruby 2.4目录

1.缩略 | Abbrev
2.ARGF
3.数组 | Array
4.Base64
5.基本对象 | BasicObject
6.基准测试 | Benchmark
7.BigDecimal
8.绑定 | Binding
9.CGI
10.类 | Class
11.比较 | Comparable
12.负责 | Complex
13.计算续体 | Continuation
14.覆盖 | Coverage
15.CSV
16.日期 | Date
17.日期时间 | DateTime
18.DBM
19.代理 | Delegator
20.摘要 | Digest
21.Dir
22.DRb
23.编码 | Encoding
24.枚举 | Enumerable
25.枚举 | Enumerator
26.ENV
27.ERB
28.错误 | Errors
29.Etc
30.期望值 | Exception
31.错误类 | FalseClass
32.Fiber
33.Fiddle
34.文件 | File
35.文件实用程序 | FileUtils
36.查找 | Find
37.浮点 | Float
38.Forwardable
39.GC
40.GDBM
41.GetoptLong
42.Hash
43.Integer
44.IO
45.IPAddr
46.JSON
47.Kernel
48.语言 | 3Language
49.记录 | Logger
50.编排 | Marshal
51.MatchData
52.数学 | Math
53.矩阵 | Matrix
54.方法 | Method
55.模型 | Module
56.监控 | Monitor
57. 互斥 | Mutex
58.Net
59.Net::FTP
60.Net::HTTP
61.Net::IMAP
62.Net::SMTP
63.NilClass
64.数字 | Numeric
65.对象 | Object
66.ObjectSpace
67.Observable
68.Open3
69.OpenSSL
70.OpenStruct
71.OpenURI
72.OptionParser
73.路径名 | Pathname
74.完整输出 | PrettyPrint
75.Prime
76.Proc
77.过程 | Process
78.PStore
79.PTY
80.队列 | Queue
81.随机 | Random
82.范围 | Range
83.合理的 | Rational
84.Readline
85.Regexp
86.Resolv
87.Ripper
88.RubyVM
89.Scanf
90.SDBM
91.SecureRandom
92.Set
93.Shell
94.信号 | Signal
95.Singleton
96.套接字 | Socket
97.字符串 | String
98.StringIO
99.StringScanner
100.结构 | Struct